-
Notifications
You must be signed in to change notification settings - Fork 53
Introduce Container‑based GitHub Action #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ascheman
wants to merge
20
commits into
develop
Choose a base branch
from
feature/369-gh-action
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a24ee72
#343 Make Baeldung exclusion a real regex
ascheman c4a25d2
#369 Add shadow Jar for CLI (aka. Fat Jar)
ascheman c58fd0b
#369 Add Docker build and GitHub action
ascheman de5d1b5
#369 Add integration test for Docker
ascheman a7d11b0
#369 Restrict Integration test for Docker
ascheman 4103350
#369 Use Docker "latest" only on "main" branch
ascheman 690cef7
#369 Run Docker integration test on local image
ascheman ba8fc1a
#369 Skip Docker integration test on Windows
ascheman 61d0fbd
#369 Fix Docker build by pushing anyway
ascheman f7ea603
#369 Add documentation for Docker run
ascheman f658832
#369 Enable fail-on-error for CLI
ascheman a81384f
#369 Test GitHub action
ascheman 1bb0eb3
#369 Build Docker locally and fail on errors
ascheman 7aacf0c
#369 Separate Docker build(s)
ascheman ce446c3
#369 Enable additional Docker tags
ascheman 19a95a3
#369 Add housekeeping for timestamped images
ascheman 09784cf
#369 Clean up untagged packages also
ascheman 89d686c
Fix GH Caching key YAML
ascheman 675d6d7
#369 Use Git SHA as unique Docker tag
ascheman 689d304
#369 Use SHA for Docker tags if no branch detected
ascheman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| name: Clean up old GHCR images | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: '0 2 * * *' # every night at 02:00 UTC | ||
| workflow_dispatch: | ||
| inputs: | ||
| retention_days: | ||
| description: 'Delete images older than this many days' | ||
| required: false | ||
| default: '14' | ||
| dry_run: | ||
| description: 'If true, only print which versions would be deleted (no deletion performed)' | ||
| required: false | ||
| type: boolean | ||
| default: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: write | ||
|
|
||
| jobs: | ||
| cleanup-ghcr: | ||
| name: Remove timestamped images older than 14 days | ||
| runs-on: ubuntu-latest | ||
| if: ${{ github.event_name != 'schedule' || github.ref == 'refs/heads/main' }} | ||
| steps: | ||
| - name: Clean up old container versions | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const org = 'aim42'; | ||
| const package_type = 'container'; | ||
| const package_name = 'hsc'; | ||
| const per_page = 100; | ||
| const tsTagRegex = /^\d{14}$/; // yyyyMMddHHmmss | ||
| const sha256Regex = /^[a-f0-9]{64}$/i; // sha256 digest-like tag | ||
|
|
||
| const daysInput = (context.payload && context.payload.inputs && context.payload.inputs.retention_days) || '14'; | ||
| const daysParsed = parseInt(daysInput, 10); | ||
| const retentionDays = Number.isFinite(daysParsed) && daysParsed > 0 ? daysParsed : 14; | ||
| const cutoff = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000); // retentionDays days ago | ||
|
|
||
| const dryRunInput = context.payload && context.payload.inputs ? context.payload.inputs.dry_run : undefined; | ||
| const dryRun = (typeof dryRunInput === 'boolean') ? dryRunInput | ||
| : (dryRunInput === undefined ? true : String(dryRunInput).toLowerCase() === 'true'); | ||
|
|
||
| core.info(`Using retention period: ${retentionDays} day(s). Dry-run: ${dryRun}.`); | ||
|
|
||
| function parseTimestampTag(tag) { | ||
| // tag format: yyyyMMddHHmmss, interpreted as UTC | ||
| const y = parseInt(tag.slice(0, 4), 10); | ||
| const m = parseInt(tag.slice(4, 6), 10) - 1; | ||
| const d = parseInt(tag.slice(6, 8), 10); | ||
| const hh = parseInt(tag.slice(8, 10), 10); | ||
| const mm = parseInt(tag.slice(10, 12), 10); | ||
| const ss = parseInt(tag.slice(12, 14), 10); | ||
| return new Date(Date.UTC(y, m, d, hh, mm, ss)); | ||
| } | ||
|
|
||
| let page = 1; | ||
| let totalDeleted = 0; | ||
| let wouldDelete = 0; | ||
| let scanned = 0; | ||
|
|
||
| while (true) { | ||
| const { data: versions } = await github.request( | ||
| 'GET /orgs/{org}/packages/{package_type}/{package_name}/versions', | ||
| { org, package_type, package_name, per_page, page } | ||
| ); | ||
|
|
||
| if (!versions || versions.length === 0) break; | ||
|
|
||
| for (const v of versions) { | ||
| scanned++; | ||
| const tags = (v.metadata && v.metadata.container && v.metadata.container.tags) || []; | ||
| const createdAt = new Date(v.created_at || v.updated_at || 0); | ||
|
|
||
| core.debug (`Checking version '${v.id}' (${v.name}) of '${createdAt}' with tags: '${tags.join(', ')}'`); | ||
|
|
||
| // Skip protected tags to avoid removing latest or release tags that share the same version | ||
| const isProtected = tags.some(t => t === 'latest' || /^v\d[\.\d]*/.test(t)); | ||
| if (isProtected) { | ||
| core.info(`Skipping protected version ${v.id} with tags: ${tags.join(', ')}`); | ||
| continue; | ||
| } | ||
|
|
||
| const tsTags = tags.filter(t => tsTagRegex.test(t)); | ||
| const shaTags = tags.filter(t => sha256Regex.test(t)); | ||
| const nonShaTags = tags.filter(t => !sha256Regex.test(t)); | ||
|
|
||
| let shouldDelete = false; | ||
|
|
||
| // If any timestamp tag is older than cutoff, delete the entire version | ||
| if (tsTags.length > 0) { | ||
| shouldDelete = tsTags.some(t => parseTimestampTag(t) < cutoff); | ||
| } | ||
|
|
||
| // Additionally handle versions that are tagged only by sha256 values. | ||
| // Delete if older than retention (by created_at/updated_at) unless there is any non-sha256 tag. | ||
| if (!shouldDelete && shaTags.length > 0 && nonShaTags.length === 0) { | ||
| if (createdAt instanceof Date && !isNaN(createdAt) && createdAt < cutoff) { | ||
| shouldDelete = true; | ||
| } | ||
| } | ||
|
|
||
| // Handle versions with no tags at all: delete if older than retention by created/updated timestamp | ||
| if (!shouldDelete && (!tags || tags.length === 0)) { | ||
| if (createdAt instanceof Date && !isNaN(createdAt) && createdAt < cutoff) { | ||
| shouldDelete = true; | ||
| } | ||
| } | ||
|
|
||
| if (shouldDelete) { | ||
| if (dryRun) { | ||
| wouldDelete++; | ||
| core.info(`[DRY-RUN] Would delete version '${v.id}' (${v.name}) of '${createdAt}' with tags: '${tags.join(', ')}'`); | ||
| } else { | ||
| try { | ||
| await github.request( | ||
| 'DELETE /orgs/{org}/packages/{package_type}/{package_name}/versions/{package_version_id}', | ||
| { | ||
| org, | ||
| package_type, | ||
| package_name, | ||
| package_version_id: v.id, | ||
| } | ||
| ); | ||
| totalDeleted++; | ||
| core.info(`Deleted version '${v.id}' (${v.name}) of '${createdAt}' with tags: ${tags.join(', ')}`); | ||
| } catch (err) { | ||
| core.warning(`Failed to delete version ${v.id}: ${err.message}`); | ||
| } | ||
| } | ||
| } else { | ||
| core.debug (`Not deleting '${v.id}' (${v.name}) of '${createdAt}' with tags: '${tags.join(', ')}'`); | ||
| } | ||
| } | ||
|
|
||
| page++; | ||
| } | ||
|
|
||
| if (dryRun) { | ||
| core.info(`Scanned versions: ${scanned}. Would delete versions: ${wouldDelete}.`); | ||
| } else { | ||
| core.info(`Scanned versions: ${scanned}. Deleted versions: ${totalDeleted}.`); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| name: 'hsc' | ||
| description: | | ||
| HSC (HTML Sanity Check) is a fast and lightweight tool for checking HTML, links, and accessibility issues. | ||
| It helps ensure clean, error-free web content and integrates seamlessly into CI/CD workflows. | ||
| inputs: | ||
| args: | ||
| description: 'CLI arguments (cf. https://hsc.aim42.org/manual/20_cli.html)' | ||
| required: false | ||
| runs: | ||
| using: 'docker' | ||
| # If the image tag changes, e.g., to v3, the action in the test workflow (workflows/gradle-build.yml) must be adjusted accordingly | ||
| image: 'docker://ghcr.io/aim42/hsc:v2' | ||
| entrypoint: '/hsc.sh' | ||
| args: | ||
| - ${{ inputs.args }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| FROM eclipse-temurin:21-jre-alpine | ||
|
|
||
| ARG DESCRIPTION='HSC (HTML Sanity Check) is a fast and lightweight tool for checking HTML, links, and accessibility issues.' | ||
| ARG VERSION=Unknown | ||
|
|
||
| LABEL version=${VERSION} | ||
| LABEL org.opencontainers.image.description=${DESCRIPTION} | ||
|
|
||
| COPY hsc.sh /hsc.sh | ||
| RUN chmod 755 /hsc.sh | ||
|
|
||
| COPY build/libs/htmlSanityCheck-cli-${VERSION}-all.jar /hsc.jar | ||
|
|
||
| ENTRYPOINT ["/hsc.sh"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.