diff --git a/.gitea/workflows/build-push.yml b/.gitea/workflows/build-push.yml index f12efe6..08b1fc4 100644 --- a/.gitea/workflows/build-push.yml +++ b/.gitea/workflows/build-push.yml @@ -1,18 +1,43 @@ name: Build and Push Distroless Flood on: + schedule: + - cron: '17 9 * * 1' + push: + tags: + - 'v*' workflow_dispatch: inputs: flood_version: - description: 'jesec/flood release tag' + description: 'jesec/flood release tag, or auto to check for a new stable release' required: true - default: 'v4.16.2' + default: 'auto' type: string jobs: + release: + runs-on: kube + if: ((github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' || github.event.schedule != '') && github.ref == 'refs/heads/main') || (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) + outputs: + version: ${{ steps.release.outputs.version }} + should_build: ${{ steps.release.outputs.should_build }} + steps: + - name: Checkout code + uses: https://github.com/actions/checkout@v4 + with: + persist-credentials: false + + - name: Select Flood release + id: release + env: + SOURCE_REF: ${{ github.ref }} + FLOOD_VERSION: ${{ github.event.inputs.flood_version }} + run: node build/check-release.cjs + build: + needs: release + if: needs.release.outputs.should_build == 'true' runs-on: kube - if: github.ref == 'refs/heads/main' env: DOCKER_BUILD_RECORD_UPLOAD: 'false' DOCKER_BUILD_SUMMARY: 'false' @@ -24,7 +49,7 @@ jobs: - name: Fetch Flood release env: - FLOOD_VERSION: ${{ github.event.inputs.flood_version }} + FLOOD_VERSION: ${{ needs.release.outputs.version }} run: | test -n "$FLOOD_VERSION" || { echo "flood_version is required" >&2; exit 1; } git check-ref-format "refs/tags/$FLOOD_VERSION" @@ -65,7 +90,10 @@ jobs: pull: true push: true load: false - tags: docker.io/k2patel/floodui:latest + tags: | + docker.io/k2patel/floodui:latest + docker.io/k2patel/floodui:${{ needs.release.outputs.version }} labels: | org.opencontainers.image.source=https://git.k2patel.in/k2patel/docker-flood-client org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.version=${{ needs.release.outputs.version }} diff --git a/README.md b/README.md index 08351e4..75bdbb3 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,34 @@ Source: https://git.k2patel.in/k2patel/docker-flood-client -`docker.io/k2patel/floodui:latest` is the only maintained image variant and is -an Alpine/musl distroless runtime. The separate `:distroless` tag is retired. +`docker.io/k2patel/floodui:latest` is the maintained image variant and is +an Alpine/musl distroless runtime. Version tags such as `:v4.16.4` preserve each +published release. The separate `:distroless` tag is retired. It runs the Flood web UI/API and connects to an external torrent service; it does not bundle or start rTorrent. ## Build and publication -Gitea Actions publishes only on manual runs. Select `main` in Actions → -**Build and Push Distroless Flood** → Run workflow and set the required -`flood_version` release tag (default `v4.16.2`). Pushes do not trigger builds. +Every Monday at 09:17 UTC, Gitea Actions checks the latest stable release at +https://github.com/jesec/flood/releases. Drafts and prereleases are excluded. +If its version tag is missing from Docker Hub, the workflow builds the +client-only image, runs the existing startup validation, and publishes both +`:latest` and the upstream version tag. An existing version skips the build. +API/registry errors fail the check; failed builds are retried on the next weekly +run because no version image was published. No repository tag or release is +needed for these automatic builds. + +Gitea Actions also publishes when a `v*` tag is created in this repository, using that +tag as the upstream `jesec/flood` release version (for example, `v4.16.4`). Create +tags on commits containing this workflow. Ordinary branch pushes do not trigger +builds. Each successful build publishes `docker.io/k2patel/floodui:latest` and +the matching version tag. + +For a manual build, select `main` in Actions → **Build and Push Distroless Flood** +→ Run workflow. Leave `flood_version` as `auto` to check now, or enter an explicit +release tag such as `v4.16.4` to force a rebuild. Use this option for tags created +before tag-triggered builds were restored; changing the workflow does not +trigger a build for an existing tag. The upstream pnpm lockfile builds the server and UI; switching release families may require changes to the build tooling. @@ -26,6 +44,10 @@ mediainfo/df, and absence of shell/package-manager binaries before publication. Builds use the Kubernetes runner for `linux/amd64`, pushing directly to Docker Hub; temporary builders and caches are removed afterward. Configure Actions secrets `DOCKER_USERNAME` and `DOCKER_TOKEN` with write access to `k2patel/floodui`. +The weekly schedule becomes active once the workflow is on the default branch +(`main`). The release check uses public GitHub and Docker Hub APIs and requires +no additional secrets. Run `node --test tests/check-release.cjs` to test release +selection and skip/error handling locally. ## Updating an existing deployment diff --git a/build/check-release.cjs b/build/check-release.cjs new file mode 100644 index 0000000..b2c4085 --- /dev/null +++ b/build/check-release.cjs @@ -0,0 +1,60 @@ +const fs = require('node:fs'); + +const image = 'k2patel/floodui'; + +async function selectRelease({ref = '', requested = ''} = {}, request = fetch) { + const automatic = !ref.startsWith('refs/tags/') && (!requested || requested === 'auto'); + const get = (url, options = {}) => request(url, { + ...options, + signal: AbortSignal.timeout(30000), + }); + let version = ref.startsWith('refs/tags/') ? ref.slice('refs/tags/'.length) : requested; + + if (automatic) { + const response = await get('https://api.github.com/repos/jesec/flood/releases/latest', { + headers: {Accept: 'application/vnd.github+json', 'User-Agent': 'docker-flood-client'}, + }); + if (!response.ok) throw new Error(`GitHub release check failed: HTTP ${response.status}`); + const release = await response.json(); + if (release.draft || release.prerelease) throw new Error('Expected a stable published release'); + version = release.tag_name; + } + + // Also keeps the workflow output and Docker tag safe from upstream metadata. + if (typeof version !== 'string' || !/^v\d+\.\d+\.\d+(?:-[a-zA-Z0-9][a-zA-Z0-9.-]*)?$/.test(version)) { + throw new Error(`Unsupported Flood release tag: ${JSON.stringify(version)}`); + } + + if (automatic) { + const auth = await get(`https://auth.docker.io/token?service=registry.docker.io&scope=repository:${image}:pull`); + if (!auth.ok) throw new Error(`Docker Hub token request failed: HTTP ${auth.status}`); + const {token} = await auth.json(); + if (!token) throw new Error('Docker Hub returned no pull token'); + const manifest = await get(`https://registry-1.docker.io/v2/${image}/manifests/${version}`, { + method: 'HEAD', + headers: { + Authorization: `Bearer ${token}`, + Accept: 'application/vnd.oci.image.index.v1+json, application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json', + }, + }); + if (manifest.ok) return {version, shouldBuild: false}; + if (manifest.status !== 404) throw new Error(`Docker Hub image check failed: HTTP ${manifest.status}`); + } + return {version, shouldBuild: true}; +} + +if (require.main === module) { + selectRelease({ref: process.env.SOURCE_REF, requested: process.env.FLOOD_VERSION}) + .then(({version, shouldBuild}) => { + console.log(`${version}: ${shouldBuild ? 'build and publish client image' : 'already published; skipping'}`); + if (process.env.GITHUB_OUTPUT) { + fs.appendFileSync(process.env.GITHUB_OUTPUT, `version=${version}\nshould_build=${shouldBuild}\n`); + } + }) + .catch(error => { + console.error(error.message); + process.exitCode = 1; + }); +} + +module.exports = {selectRelease}; diff --git a/tests/check-release.cjs b/tests/check-release.cjs new file mode 100644 index 0000000..ae3cc97 --- /dev/null +++ b/tests/check-release.cjs @@ -0,0 +1,74 @@ +const {test} = require('node:test'); +const assert = require('node:assert/strict'); +const {selectRelease} = require('../build/check-release.cjs'); + +function responses(status, release = {tag_name: 'v4.16.4', draft: false, prerelease: false}) { + const calls = []; + const request = async (url, options) => { + calls.push({url, options}); + if (calls.length === 1) return {ok: true, json: async () => release}; + if (calls.length === 2) return {ok: true, json: async () => ({token: 'test-token'})}; + return {ok: status === 200, status}; + }; + return {calls, request}; +} + +test('a new stable release is built only after confirming the image is missing', async () => { + const {request, calls} = responses(404); + assert.deepEqual(await selectRelease({}, request), {version: 'v4.16.4', shouldBuild: true}); + assert.equal(calls[0].url, 'https://api.github.com/repos/jesec/flood/releases/latest'); + assert.equal(calls[2].url, 'https://registry-1.docker.io/v2/k2patel/floodui/manifests/v4.16.4'); + assert.equal(calls[2].options.method, 'HEAD'); + assert.equal(calls[2].options.headers.Authorization, 'Bearer test-token'); +}); + +test('an already published release skips the build', async () => { + const {request} = responses(200); + assert.deepEqual(await selectRelease({ref: 'refs/heads/main', requested: 'auto'}, request), { + version: 'v4.16.4', shouldBuild: false, + }); +}); + +test('registry failures fail the check instead of publishing', async () => { + for (const status of [401, 429, 500]) { + const {request} = responses(status); + await assert.rejects(selectRelease({}, request), new RegExp(`HTTP ${status}`)); + } +}); + +test('GitHub failure prevents publication', async () => { + await assert.rejects(selectRelease({}, async () => ({ok: false, status: 503})), /GitHub.*503/); +}); + +test('drafts and prereleases are rejected by automatic checks', async () => { + for (const flags of [{draft: true}, {prerelease: true}]) { + const {request} = responses(404, {tag_name: 'v5.0.0', ...flags}); + await assert.rejects(selectRelease({}, request), /stable published release/); + } +}); + +test('invalid upstream tags never reach Docker Hub or workflow outputs', async () => { + for (const tag_name of ['v4.16.4\nshould_build=true', '../main', null, 'latest']) { + const {request, calls} = responses(404, {tag_name}); + await assert.rejects(selectRelease({}, request), /Unsupported Flood release tag/); + assert.equal(calls.length, 1); + } +}); + +test('explicit manual and tag builds can rebuild without the automatic skip', async () => { + const noNetwork = () => { throw new Error('Unexpected network call'); }; + assert.deepEqual(await selectRelease({requested: 'v4.16.4'}, noNetwork), { + version: 'v4.16.4', shouldBuild: true, + }); + assert.deepEqual(await selectRelease({ref: 'refs/tags/v4.16.3', requested: 'auto'}, noNetwork), { + version: 'v4.16.3', shouldBuild: true, + }); +}); + +test('a missing registry token fails the check', async () => { + let calls = 0; + await assert.rejects(selectRelease({}, async () => ({ + ok: true, + json: async () => ++calls === 1 ? {tag_name: 'v4.16.4'} : {}, + })), /no pull token/); +});