Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<!-- Every PR must reference an existing issue. Example: Fixes #123 -->
Fixes #

<!-- To build a test image for this PR, use the `/create-test-image` command in a PR comment. -->

## What changed

<!-- Short description of the change -->
Expand Down
61 changes: 61 additions & 0 deletions .github/workflows/build-image-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Build a test image from a PR code

on:
issue_comment:
types: [created]

jobs:
build-images:
if: |
github.event.issue.pull_request && contains(github.event.comment.body, '/create-test-image') &&
(
github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'CONTRIBUTOR' ||
github.event.comment.author_association == 'COLLABORATOR' ||
github.event.comment.author_association == 'FIRST_TIME_CONTRIBUTOR'
)
runs-on:
ubuntu-latest

permissions:
contents: read
pull-requests: write

steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0

- name: Get Pull Requests Informations
id: pr
uses: actions/github-script@v7

Check failure on line 30 in .github/workflows/build-image-pr.yml

View workflow job for this annotation

GitHub Actions / Check pinned GitHub Actions

Unpinned action: uses: actions/github-script@v7
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});

core.setOutput('sha', pr.data.head.sha);
core.setOutput('ref', pr.data.head.ref);

- name: Checkout Last Commits
uses: actions/checkout@v4

Check failure on line 43 in .github/workflows/build-image-pr.yml

View workflow job for this annotation

GitHub Actions / Check pinned GitHub Actions

Unpinned action: uses: actions/checkout@v4
with:
ref: ${{ steps.pr.outputs.sha}}

- name: Log in to GitHub Container Registry
uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_GITHUB_TOKEN }}

- name: Build & Push Docker image
run: |
VERSION=${pr-${{ github.event.issue.number }}-${{ steps.pr.outputs.sha }}}
echo "Building Multi-Arch Docker image with version $VERSION"
make docker-build-publish VERSION=$VERSION REPO_OWNER=${{ github.repository_owner }}



98 changes: 98 additions & 0 deletions .github/workflows/cleanup-pr-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Cleanup PR test images

on:
pull_request:
types: [closed]

schedule:
- cron: "0 3 1 * *" # Tous les 1er du mois à 03:00 UTC

permissions:
packages: write

jobs:
cleanup-closed-pr:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest

steps:
- name: Delete closed PR images
uses: actions/github-script@v7

Check failure on line 20 in .github/workflows/cleanup-pr-image.yml

View workflow job for this annotation

GitHub Actions / Check pinned GitHub Actions

Unpinned action: uses: actions/github-script@v7
with:
script: |
const owner = context.repo.owner;
const packageName = context.repo.repo.toLowerCase();
const prefix = `pr-${context.payload.pull_request.number}-`;

const versions = await github.paginate(
github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg,
{
org: owner,
package_type: "container",
package_name: packageName,
per_page: 100,
}
);

for (const version of versions) {
const tags = version.metadata?.container?.tags ?? [];

if (!tags.some(tag => tag.startsWith(prefix))) {
continue;
}

core.info(`Deleting ${tags.join(", ")}`);

await github.rest.packages.deletePackageVersionForOrg({
org: owner,
package_type: "container",
package_name: packageName,
package_version_id: version.id,
});
}

cleanup-old-images:
if: github.event_name == 'schedule'
runs-on: ubuntu-latest

steps:
- name: Delete old PR images
uses: actions/github-script@v7

Check failure on line 60 in .github/workflows/cleanup-pr-image.yml

View workflow job for this annotation

GitHub Actions / Check pinned GitHub Actions

Unpinned action: uses: actions/github-script@v7
with:
script: |
const owner = context.repo.owner;
const packageName = context.repo.repo.toLowerCase();
const retentionDays = 30;

const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - retentionDays);

const versions = await github.paginate(
github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg,
{
org: owner,
package_type: "container",
package_name: packageName,
per_page: 100,
}
);

for (const version of versions) {
const tags = version.metadata?.container?.tags ?? [];

const isPrImage = tags.some(tag => tag.startsWith("pr-"));
const createdAt = new Date(version.created_at);

if (!isPrImage || createdAt > cutoff) {
continue;
}

core.info(`Deleting old image ${tags.join(", ")}`);

await github.rest.packages.deletePackageVersionForOrg({
org: owner,
package_type: "container",
package_name: packageName,
package_version_id: version.id,
});
}
Loading