fix(ci): harden the release publish workflow after the PR review
PR Check / Security scan (Snyk) (pull_request) Skipped
PR Check / Lint + typecheck (pull_request) Successful in 1m5s
PR Check / Secret scan (gitleaks) (pull_request) Successful in 6s
PR Check / Dependency audit (pull_request) Successful in 8s
PR Check / Dockerfile lint (droast) (pull_request) Successful in 5s
PR Check / AI code review (opencode) (pull_request) Successful in 48s
PR Check / Security scan (Snyk) (pull_request) Skipped
PR Check / Lint + typecheck (pull_request) Successful in 1m5s
PR Check / Secret scan (gitleaks) (pull_request) Successful in 6s
PR Check / Dependency audit (pull_request) Successful in 8s
PR Check / Dockerfile lint (droast) (pull_request) Successful in 5s
PR Check / AI code review (opencode) (pull_request) Successful in 48s
Applied the findings that held up against the code: - strict vMAJOR.MINOR.PATCH guard; v[0-9]* accepted v1, v1-foo and prereleases, all valid registry tags - latest now follows the newest release, so replaying an older tag cannot drag it backwards; skipped with a warning if the tag list is unavailable - smoke test starts the built image and requires 2xx on / before pushing - timeout-minutes: 20 on the job, so a hung build cannot hold the capacity-1 runner indefinitely - the decision doc records the socket's blast radius: root-equivalent, runner-wide, only on a label PR jobs cannot use Rejected with evidence: the .dockerignore leak (.env*, *.pem, .git are excluded) and the Docker client/daemon API mismatch (20.10.24 client against a 29.9.2 daemon builds over the socket).
This commit is contained in:
1 parent
f3d3e196d0
commit
435be63ae0
4 files changed
+132
-25
No files matched your search
@@ -26,6 +26,9 @@ jobs:
|
||||
publish:
|
||||
name: Build and push image
|
||||
runs-on: ubuntu-latest
|
||||
# A hung build would hold the capacity-1 runner and, through it, every
|
||||
# other job on this server. Gitea's instance default is not a contract.
|
||||
timeout-minutes: 20
|
||||
# Same base image as the other jobs. The build runs on the runner's
|
||||
# Docker daemon (not in a nested daemon), so this container only needs
|
||||
# the CLI - see OKF_Nuxt/decisions/registry-publishing.md.
|
||||
@@ -47,6 +50,12 @@ jobs:
|
||||
# An unreachable daemon means the runner was started without the
|
||||
# host socket mounted: set `docker_host: automount` in the
|
||||
# act_runner config and restart the service.
|
||||
#
|
||||
# The socket is root-equivalent on the host, and automount is a
|
||||
# runner-wide setting - it applies to every job on that runner,
|
||||
# including PR jobs. Only enable it on a runner label that
|
||||
# PR-triggered jobs cannot use; see the trust boundary in
|
||||
# OKF_Nuxt/decisions/registry-publishing.md.
|
||||
run: |
|
||||
echo "DOCKER_HOST=${DOCKER_HOST:-<unset - the default socket path>}"
|
||||
if ! docker info > /dev/null 2>&1; then
|
||||
@@ -60,32 +69,43 @@ jobs:
|
||||
# GITHUB_REF_NAME is the tag on a tag push. The git fallback keeps
|
||||
# this working if a runner version does not set it.
|
||||
version="${GITHUB_REF_NAME:-$(git describe --tags --exact-match HEAD)}"
|
||||
case "$version" in
|
||||
v[0-9]*) ;;
|
||||
*) echo "::error::expected a vX.Y.Z tag, got '${version}'"; exit 1 ;;
|
||||
esac
|
||||
# Strict semver, not a `v[0-9]*` prefix match: that would accept
|
||||
# `v1`, `v1-anything` and prereleases, all valid registry tags.
|
||||
if ! printf '%s' "$version" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
|
||||
echo "::error::expected a vMAJOR.MINOR.PATCH tag, got '${version}'"
|
||||
exit 1
|
||||
fi
|
||||
sha="$(git rev-parse --short=12 HEAD)"
|
||||
|
||||
# `latest` follows the newest release, not the newest build. A
|
||||
# re-run of an older tag (Gitea can replay any run) must not drag
|
||||
# `latest` backwards, while a re-run of the newest tag may set it:
|
||||
# that release had not produced an image yet.
|
||||
# A tag comparison needs the other tags, which a depth-1 tag
|
||||
# checkout does not have. If the fetch fails, skip `latest` and
|
||||
# say so - a wrong tag is worse than a missing one.
|
||||
if git fetch --tags --force --quiet origin 2>/dev/null; then
|
||||
newest="$(git tag -l | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)"
|
||||
if [ "$version" = "$newest" ]; then
|
||||
latest=true
|
||||
else
|
||||
latest=false
|
||||
echo "::warning::${version} is not the newest release (${newest}) - not moving :latest"
|
||||
fi
|
||||
else
|
||||
latest=false
|
||||
echo "::warning::could not list tags - not moving :latest"
|
||||
fi
|
||||
|
||||
# Short SHA tag alongside the version: OKF_CV_Website/architecture/
|
||||
# delivery.md asks for SHA-tagged images so every deploy is
|
||||
# traceable to a commit even after a tag is moved.
|
||||
sha="$(git rev-parse --short=12 HEAD)"
|
||||
{
|
||||
echo "version=${version}"
|
||||
echo "sha=${sha}"
|
||||
echo "latest=${latest}"
|
||||
} >> "$GITHUB_OUTPUT"
|
||||
echo "Building ${version} from ${sha}"
|
||||
|
||||
- name: Log in to the registry
|
||||
env:
|
||||
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
||||
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
run: |
|
||||
# Missing secrets must fail loudly: an anonymous `docker push`
|
||||
# would answer 401 with a message most people misread as a
|
||||
# missing repository.
|
||||
[ -n "$REGISTRY_USERNAME" ] || { echo "::error::secret REGISTRY_USERNAME is empty"; exit 1; }
|
||||
[ -n "$REGISTRY_PASSWORD" ] || { echo "::error::secret REGISTRY_PASSWORD is empty"; exit 1; }
|
||||
printf '%s' "$REGISTRY_PASSWORD" \
|
||||
| docker login registry.noelsabia.dev -u "$REGISTRY_USERNAME" --password-stdin
|
||||
echo "Building ${version} from ${sha} (latest: ${latest})"
|
||||
|
||||
- name: Build image
|
||||
env:
|
||||
@@ -101,17 +121,67 @@ jobs:
|
||||
--label "org.opencontainers.image.revision=${SHA}" \
|
||||
-t "${IMAGE}:${VERSION}" \
|
||||
-t "${IMAGE}:sha-${SHA}" \
|
||||
-t "${IMAGE}:latest" \
|
||||
"${APP_DIR}"
|
||||
|
||||
- name: Smoke-test the image
|
||||
env:
|
||||
VERSION: ${{ steps.version.outputs.version }}
|
||||
run: |
|
||||
# Start the real server inside the built image and require a 2xx
|
||||
# from /. Cheaper than publishing a broken release, and the only
|
||||
# step that proves the Nitro output actually runs. Done inside the
|
||||
# container because the app's port is not reachable from this job
|
||||
# container - the daemon publishes on the host, not here.
|
||||
docker run --rm "${IMAGE}:${VERSION}" sh -c '
|
||||
node .output/server/index.mjs &
|
||||
server=$!
|
||||
i=0
|
||||
while [ "$i" -lt 30 ]; do
|
||||
if node -e "fetch(\"http://127.0.0.1:3000/\").then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"; then
|
||||
echo "server answered 2xx after ${i}s"
|
||||
kill "$server"
|
||||
exit 0
|
||||
fi
|
||||
i=$((i + 1))
|
||||
sleep 1
|
||||
done
|
||||
echo "server did not answer 2xx within 30s"
|
||||
kill "$server"
|
||||
exit 1
|
||||
'
|
||||
|
||||
- name: Log in to the registry
|
||||
env:
|
||||
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
||||
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
run: |
|
||||
# Missing secrets must fail loudly: an anonymous `docker push`
|
||||
# would answer 401 with a message most people misread as a
|
||||
# missing repository.
|
||||
[ -n "$REGISTRY_USERNAME" ] || { echo "::error::secret REGISTRY_USERNAME is empty"; exit 1; }
|
||||
[ -n "$REGISTRY_PASSWORD" ] || { echo "::error::secret REGISTRY_PASSWORD is empty"; exit 1; }
|
||||
printf '%s' "$REGISTRY_PASSWORD" \
|
||||
| docker login registry.noelsabia.dev -u "$REGISTRY_USERNAME" --password-stdin
|
||||
|
||||
- name: Tag latest
|
||||
if: steps.version.outputs.latest == 'true'
|
||||
env:
|
||||
VERSION: ${{ steps.version.outputs.version }}
|
||||
run: docker tag "${IMAGE}:${VERSION}" "${IMAGE}:latest"
|
||||
|
||||
- name: Push image
|
||||
env:
|
||||
VERSION: ${{ steps.version.outputs.version }}
|
||||
SHA: ${{ steps.version.outputs.sha }}
|
||||
LATEST: ${{ steps.version.outputs.latest }}
|
||||
run: |
|
||||
# Explicit per tag, not `--all-tags`: --all-tags would also push
|
||||
# stale local tags left on the shared daemon by earlier runs.
|
||||
docker push "${IMAGE}:${VERSION}"
|
||||
docker push "${IMAGE}:sha-${SHA}"
|
||||
docker push "${IMAGE}:latest"
|
||||
echo "Pushed ${IMAGE}:${VERSION}, :sha-${SHA}, :latest"
|
||||
if [ "$LATEST" = "true" ]; then
|
||||
docker push "${IMAGE}:latest"
|
||||
echo "Pushed ${IMAGE}:${VERSION}, :sha-${SHA}, :latest"
|
||||
else
|
||||
echo "Pushed ${IMAGE}:${VERSION}, :sha-${SHA} (:latest untouched)"
|
||||
fi
|
||||
Reference in new issue
Block a user