Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(ci): authorize manual releases and avoid duplicate publishing

HoshinoSuzumib96a504b02a7
diff --git a/.github/workflows/publish-images.yml b/.github/workflows/publish-images.yml
index 883aa6a0..3132bfaa 100644
--- a/.github/workflows/publish-images.yml
+++ b/.github/workflows/publish-images.yml
@@ -48,16 +48,52 @@ jobs:
changelog: ${{ steps.changelog.outputs.changelog }}
commit: ${{ steps.source.outputs.commit }}
tag: ${{ steps.version.outputs.tag }}
+ publish: ${{ steps.trigger.outputs.publish }}
steps:
+ - name: Validate manual release credential
+ if: github.event_name == 'workflow_dispatch'
+ env:
+ RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
+ run: |
+ if [[ -z "$RELEASE_TOKEN" ]]; then
+ echo "::error::Configure RELEASE_TOKEN with repository Contents read/write and an administrator identity allowed to bypass branch and release-tag rules."
+ exit 1
+ fi
+
- name: Checkout repository
uses: actions/checkout@v7
with:
ref: ${{ github.event_name == 'workflow_dispatch' && format('refs/heads/{0}', inputs.branch) || github.ref }}
fetch-depth: 0
fetch-tags: true
+ token: ${{ github.event_name == 'workflow_dispatch' && secrets.RELEASE_TOKEN || github.token }}
+
+ - name: Select publishing run
+ id: trigger
+ env:
+ RELEASE_EVENT: ${{ github.event_name }}
+ run: |
+ node --input-type=module <<'NODE'
+ import fs from 'node:fs'
+ import { execFileSync } from 'node:child_process'
+ const git = (...args) => execFileSync('git', args, { encoding: 'utf8' }).trim()
+ let owner
+ if (process.env.RELEASE_EVENT === 'push' && git('cat-file', '-t', process.env.GITHUB_REF) === 'tag') {
+ owner = git('for-each-ref', '--format=%(contents)', process.env.GITHUB_REF)
+ .match(/^ChronoFrame-Publish-Images-Run: ([0-9]+)$/m)?.[1]
+ }
+ if (process.env.RELEASE_EVENT === 'workflow_dispatch' && !fs.readFileSync('.github/workflows/publish-images.yml', 'utf8').includes('ChronoFrame-Publish-Images-Run:')) {
+ throw new Error('The release source branch must include the current publication workflow; backport it before releasing this branch.')
+ }
+ fs.appendFileSync(process.env.GITHUB_OUTPUT, `publish=${!owner}\n`)
+ if (owner) {
+ fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, `Publishing is owned by the [manual release run](https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${owner}); this tag-triggered duplicate is skipped.\n`)
+ }
+ NODE
- name: Validate release version and targets
id: version
+ if: steps.trigger.outputs.publish == 'true'
env:
RELEASE_VERSION: ${{ inputs.version }}
RELEASE_BRANCH: ${{ inputs.branch }}
@@ -92,33 +128,47 @@ jobs:
RELEASE_TAG: ${{ steps.version.outputs.tag }}
RELEASE_BRANCH: ${{ inputs.branch }}
run: |
- set -euo pipefail
- if git show-ref --verify --quiet "refs/tags/$RELEASE_TAG"; then
- echo "::error::Release tag already exists; refusing to overwrite."
- exit 1
- fi
node --input-type=module <<'NODE'
import fs from 'node:fs'
- const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'))
- pkg.version = process.env.RELEASE_VERSION
- fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n')
+ import { execFileSync, spawnSync } from 'node:child_process'
+ const git = (...args) => execFileSync('git', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit'] }).trim()
+ const { RELEASE_VERSION: version, RELEASE_TAG: tag, RELEASE_BRANCH: branch, GITHUB_RUN_ID: runId } = process.env
+ if (!/^[0-9]+$/.test(runId)) throw new Error('Invalid release run ID')
+ const ref = `refs/tags/${tag}`
+ const marker = `ChronoFrame-Publish-Images-Run: ${runId}`
+ const exists = spawnSync('git', ['show-ref', '--verify', '--quiet', ref]).status
+ if (exists === 0) {
+ const owned = git('cat-file', '-t', ref) === 'tag' && git('for-each-ref', '--format=%(contents)', ref).split('\n').includes(marker)
+ if (!owned) throw new Error('Release tag already exists; refusing to overwrite.')
+ const pkg = JSON.parse(git('show', `${ref}:package.json`))
+ if (pkg.version !== version) throw new Error('Existing release tag has a different version')
+ git('checkout', '--detach', `${ref}^{commit}`)
+ console.log(`Resuming ${tag} at its original immutable commit`)
+ } else {
+ if (exists !== 1) throw new Error('Unable to inspect release tag')
+ const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'))
+ pkg.version = version
+ fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n')
+ git('config', 'user.name', 'HoshinoSuzumi')
+ git('config', 'user.email', '[email protected]')
+ git('add', '--', 'package.json')
+ const changed = spawnSync('git', ['diff', '--cached', '--quiet']).status
+ if (changed === 1) git('commit', '-m', `chore(release): ${tag}`)
+ else if (changed !== 0) throw new Error('Unable to inspect version changes')
+ git('tag', '-a', tag, '-m', `Release ${tag}`, '-m', marker)
+ // Reject concurrent branch updates without leaving a partial release tag.
+ git('push', '--atomic', 'origin', `HEAD:refs/heads/${branch}`, ref)
+ }
NODE
- git config user.name 'HoshinoSuzumiBot'
- git config user.email '[email protected]'
- git add package.json
- if ! git diff --cached --quiet; then
- git commit -m "chore(release): $RELEASE_TAG"
- fi
- git tag "$RELEASE_TAG"
- # Both refs advance together; a concurrent branch update rejects the push.
- git push --atomic origin "HEAD:refs/heads/$RELEASE_BRANCH" "refs/tags/$RELEASE_TAG"
- name: Resolve immutable release commit
id: source
+ if: steps.trigger.outputs.publish == 'true'
run: echo "commit=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
- name: 📝 Generate changelog
id: changelog
+ if: steps.trigger.outputs.publish == 'true'
env:
GITHUB_TOKEN: ${{ github.token }}
RELEASE_TAG: ${{ steps.version.outputs.tag }}
@@ -162,6 +212,7 @@ jobs:
build-and-push:
needs: prepare
+ if: needs.prepare.outputs.publish == 'true'
strategy:
matrix:
include:
diff --git a/tests/publish-images-workflow.test.ts b/tests/publish-images-workflow.test.ts
new file mode 100644
index 00000000..5a287afc
--- /dev/null
+++ b/tests/publish-images-workflow.test.ts
@@ -0,0 +1,272 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { mkdtemp, mkdir, readFile, writeFile, rm } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join, dirname } from 'node:path'
+import { execFileSync, spawnSync } from 'node:child_process'
+import { createRequire } from 'node:module'
+const require = createRequire(import.meta.url)
+const { parse } = createRequire(require.resolve('wrangler/package.json'))(
+ 'yaml',
+)
+const source = await readFile(
+ new URL('../.github/workflows/publish-images.yml', import.meta.url),
+ 'utf8',
+)
+const workflow = parse(source)
+const steps = workflow.jobs.prepare.steps
+const environment = {
+ PATH: dirname(process.execPath) + ':' + process.env.PATH,
+ GIT_CONFIG_GLOBAL: '/dev/null',
+ GIT_CONFIG_NOSYSTEM: '1',
+ RELEASE_EVENT: 'workflow_dispatch',
+ MANUAL: 'true',
+ RELEASE_BRANCH: 'main',
+ RELEASE_VERSION: '1.1.0',
+ RELEASE_TAG: 'v1.1.0',
+ GITHUB_RUN_ID: '123456',
+ GITHUB_REPOSITORY: 'HoshinoSuzumi/chronoframe',
+ GITHUB_REF: 'refs/heads/main',
+ PUBLISH_GHCR: 'true',
+ PUBLISH_DOCKERHUB: 'false',
+}
+const git = (cwd: string, ...args: string[]) =>
+ execFileSync('git', args, {
+ cwd,
+ env: environment,
+ encoding: 'utf8',
+ stdio: ['ignore', 'pipe', 'pipe'],
+ }).trim()
+function runStep(name: string, cwd: string, env: Record<string, string> = {}) {
+ const step = steps.find((s: any) => s.name === name)
+ assert(step?.run, name)
+ return spawnSync('bash', ['-e', '-o', 'pipefail', '-c', step.run], {
+ cwd,
+ env: {
+ ...environment,
+ GITHUB_OUTPUT: join(cwd, 'output'),
+ GITHUB_STEP_SUMMARY: join(cwd, 'summary'),
+ ...env,
+ },
+ encoding: 'utf8',
+ })
+}
+function success(result: ReturnType<typeof runStep>) {
+ assert.equal(result.status, 0, result.stderr + result.stdout)
+}
+async function fixture() {
+ const root = await mkdtemp(join(tmpdir(), 'chronoframe-release-'))
+ const remote = join(root, 'origin.git'),
+ work = join(root, 'work')
+ git(root, 'init', '--bare', '--initial-branch=main', remote)
+ git(root, 'clone', remote, work)
+ git(work, 'config', 'user.name', 'Fixture')
+ git(work, 'config', 'user.email', '[email protected]')
+ await mkdir(join(work, '.github/workflows'), { recursive: true })
+ await writeFile(join(work, '.github/workflows/publish-images.yml'), source)
+ await writeFile(
+ join(work, 'package.json'),
+ JSON.stringify({ name: 'fixture', version: '1.0.2' }, null, 2) + '\n',
+ )
+ git(work, 'add', '.')
+ git(work, 'commit', '-m', 'Initial fixture')
+ git(work, 'switch', '-c', 'feature')
+ await writeFile(join(work, 'feature.txt'), 'feature')
+ git(work, 'add', '.')
+ git(work, 'commit', '-m', 'Feature')
+ git(work, 'switch', 'main')
+ git(work, 'merge', '--no-ff', 'feature', '-m', 'Merge feature')
+ git(work, 'push', 'origin', 'main')
+ return { root, remote, work }
+}
+test('manual releases use a scoped credential and only the originating run publishes', () => {
+ const checkout = steps.find((s: any) => s.name === 'Checkout repository')
+ assert.match(checkout.with.token, /secrets\.RELEASE_TOKEN/)
+ assert.match(checkout.with.token, /github\.token/)
+ assert.match(
+ workflow.jobs['build-and-push'].if,
+ /needs\.prepare\.outputs\.publish == 'true'/,
+ )
+ assert(workflow.jobs.manifest.needs.includes('build-and-push'))
+ assert(workflow.jobs.release.needs.includes('manifest'))
+})
+test('manual preparation keeps merge history, tags atomically, uses HoshinoSuzumi and resumes only its immutable tag', async () => {
+ const f = await fixture()
+ try {
+ const base = git(f.work, 'rev-parse', 'HEAD')
+ assert.equal(
+ git(f.work, 'rev-list', '--parents', '-n', '1', base).split(' ').length,
+ 3,
+ )
+ const missing = runStep('Validate manual release credential', f.work, {
+ RELEASE_TOKEN: '',
+ })
+ assert.notEqual(missing.status, 0)
+ assert.match(missing.stdout, /Configure RELEASE_TOKEN/)
+ success(
+ runStep('Validate manual release credential', f.work, {
+ RELEASE_TOKEN: 'fixture-only',
+ }),
+ )
+ success(runStep('Select publishing run', f.work))
+ success(runStep('Validate release version and targets', f.work))
+ success(runStep('Commit version and create release tag', f.work))
+ const commit = git(f.remote, 'rev-parse', 'refs/tags/v1.1.0^{commit}')
+ assert.equal(git(f.remote, 'rev-parse', 'refs/heads/main'), commit)
+ assert.equal(git(f.work, 'rev-parse', 'HEAD^'), base)
+ assert.equal(
+ JSON.parse(git(f.remote, 'show', `${commit}:package.json`)).version,
+ '1.1.0',
+ )
+ assert.equal(
+ git(f.work, 'show', '-s', '--format=%an <%ae>', commit),
+ 'HoshinoSuzumi <[email protected]>',
+ )
+ assert.equal(git(f.work, 'cat-file', '-t', 'refs/tags/v1.1.0'), 'tag')
+ success(
+ runStep('Select publishing run', f.work, {
+ RELEASE_EVENT: 'push',
+ GITHUB_REF: 'refs/tags/v1.1.0',
+ }),
+ )
+ assert.match(
+ await readFile(join(f.work, 'output'), 'utf8'),
+ /publish=false/,
+ )
+ assert.match(
+ await readFile(join(f.work, 'summary'), 'utf8'),
+ /actions\/runs\/123456/,
+ )
+ await writeFile(join(f.work, 'later.txt'), 'later')
+ git(f.work, 'add', '.')
+ git(f.work, 'commit', '-m', 'Later source change')
+ git(f.work, 'push', 'origin', 'main')
+ const later = git(f.remote, 'rev-parse', 'refs/heads/main')
+ success(runStep('Commit version and create release tag', f.work))
+ assert.equal(git(f.work, 'rev-parse', 'HEAD'), commit)
+ assert.equal(git(f.remote, 'rev-parse', 'refs/heads/main'), later)
+ assert.equal(
+ git(f.remote, 'rev-parse', 'refs/tags/v1.1.0^{commit}'),
+ commit,
+ )
+ const foreign = runStep('Commit version and create release tag', f.work, {
+ GITHUB_RUN_ID: '999999',
+ })
+ assert.notEqual(foreign.status, 0)
+ assert.match(foreign.stderr, /refusing to overwrite/)
+ git(f.work, 'tag', 'v1.0.2', base)
+ git(f.work, 'tag', '-a', 'v1.0.3', '-m', 'Ordinary external release', base)
+ git(
+ f.work,
+ 'commit',
+ '--allow-empty',
+ '-m',
+ 'External release',
+ '-m',
+ 'ChronoFrame-Publish-Images-Run: 123456',
+ )
+ git(f.work, 'tag', 'v1.0.4')
+ for (const tag of ['v1.0.2', 'v1.0.3', 'v1.0.4']) {
+ await writeFile(join(f.work, 'output'), '')
+ success(
+ runStep('Select publishing run', f.work, {
+ RELEASE_EVENT: 'push',
+ GITHUB_REF: `refs/tags/${tag}`,
+ }),
+ )
+ assert.equal(
+ await readFile(join(f.work, 'output'), 'utf8'),
+ 'publish=true\n',
+ )
+ }
+ } finally {
+ await rm(f.root, { recursive: true, force: true })
+ }
+})
+test('concurrent source changes reject the entire push without leaving a release tag', async () => {
+ const f = await fixture()
+ try {
+ const other = join(f.root, 'other')
+ git(f.root, 'clone', f.remote, other)
+ git(other, 'config', 'user.name', 'Fixture')
+ git(other, 'config', 'user.email', '[email protected]')
+ await writeFile(join(other, 'concurrent.txt'), 'concurrent')
+ git(other, 'add', '.')
+ git(other, 'commit', '-m', 'Concurrent update')
+ git(other, 'push', 'origin', 'main')
+ const latest = git(f.remote, 'rev-parse', 'refs/heads/main')
+ const result = runStep('Commit version and create release tag', f.work)
+ assert.notEqual(result.status, 0)
+ assert.equal(git(f.remote, 'rev-parse', 'refs/heads/main'), latest)
+ assert.equal(
+ spawnSync(
+ 'git',
+ ['show-ref', '--verify', '--quiet', 'refs/tags/v1.1.0'],
+ { cwd: f.remote, env: environment },
+ ).status,
+ 1,
+ )
+ } finally {
+ await rm(f.root, { recursive: true, force: true })
+ }
+})
+test('invalid versions, empty targets and outdated release-source workflows fail before publishing', async () => {
+ const f = await fixture()
+ try {
+ for (const value of ['1.1.0+build', '1.01.0', '1.1.0;echo bad'])
+ assert.notEqual(
+ runStep('Validate release version and targets', f.work, {
+ RELEASE_VERSION: value,
+ }).status,
+ 0,
+ )
+ assert.notEqual(
+ runStep('Validate release version and targets', f.work, {
+ PUBLISH_GHCR: 'false',
+ PUBLISH_DOCKERHUB: 'false',
+ }).status,
+ 0,
+ )
+ await writeFile(
+ join(f.work, '.github/workflows/publish-images.yml'),
+ 'name: Legacy publish\n',
+ )
+ assert.notEqual(runStep('Select publishing run', f.work).status, 0)
+ assert.equal(
+ git(f.remote, 'rev-parse', 'refs/heads/main'),
+ git(f.work, 'rev-parse', 'HEAD'),
+ )
+ } finally {
+ await rm(f.root, { recursive: true, force: true })
+ }
+})
+
+test('a release whose package version is already correct still marks its annotated tag without an empty commit', async () => {
+ const f = await fixture()
+ try {
+ const original = git(f.work, 'rev-parse', 'HEAD')
+ success(
+ runStep('Commit version and create release tag', f.work, {
+ RELEASE_VERSION: '1.0.2',
+ RELEASE_TAG: 'v1.0.2',
+ }),
+ )
+ assert.equal(git(f.remote, 'rev-parse', 'refs/heads/main'), original)
+ assert.equal(
+ git(f.remote, 'rev-parse', 'refs/tags/v1.0.2^{commit}'),
+ original,
+ )
+ success(
+ runStep('Select publishing run', f.work, {
+ RELEASE_EVENT: 'push',
+ GITHUB_REF: 'refs/tags/v1.0.2',
+ }),
+ )
+ assert.equal(
+ await readFile(join(f.work, 'output'), 'utf8'),
+ 'publish=false\n',
+ )
+ } finally {
+ await rm(f.root, { recursive: true, force: true })
+ }
+})