Docker lab/version note: use supported Docker and Node.js tools and patched images. The JavaScript examples have local tests; Docker, Nginx and hosted CI steps were not tested live.
What this guide covers
A GitHub Actions workflow can test source code, build a Docker image and publish it to GitHub Container Registry. Keep pull-request testing separate from the job that can publish images.
Before you start
Use the application and production Dockerfile from the Nginx guide. Enable GitHub Actions and package publishing for the repository. This example publishes one Linux platform, not a multi-platform image.
Step 1: Create the workflow
Save this as .github/workflows/web-image.yml:
name: web application image
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
defaults:
run:
shell: bash
jobs:
verify:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '24'
- run: node --test src/handler.test.mjs
- name: Build and exercise pull-request image
run: |
set -euo pipefail
docker build --pull -t web-ci:test .
trap 'docker rm -f web-ci >/dev/null 2>&1 || true' EXIT
docker run -d --name web-ci -p 127.0.0.1:3210:3210 web-ci:test
curl --fail --retry 15 --retry-all-errors --retry-delay 1 --retry-max-time 45 \
--connect-timeout 2 --max-time 4 http://127.0.0.1:3210/healthz
curl --fail http://127.0.0.1:3210/api/message
publish:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: verify
runs-on: ubuntu-24.04
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '24'
- run: node --test src/handler.test.mjs
- name: Build, verify and push this release image
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
IMAGE="ghcr.io/${GITHUB_REPOSITORY,,}/web-app:${GITHUB_SHA}"
printf '%s' "$GH_TOKEN" | docker login ghcr.io -u "$GITHUB_ACTOR" --password-stdin
trap 'docker rm -f web-release >/dev/null 2>&1 || true; docker logout ghcr.io >/dev/null 2>&1 || true' EXIT
docker build --pull -t "$IMAGE" .
docker run -d --name web-release -p 127.0.0.1:3210:3210 "$IMAGE"
curl --fail --retry 15 --retry-all-errors --retry-delay 1 --retry-max-time 45 \
--connect-timeout 2 --max-time 4 http://127.0.0.1:3210/healthz
curl --fail http://127.0.0.1:3210/api/message
docker push "$IMAGE"
Use reviewed, supported actions and pin approved full commit SHAs before using this for a production release. The visible major-version tags are readable examples.
Step 2: Run a pull-request test
Open a pull request and confirm the verification job runs its tests and image build. It must not receive package-write permissions or registry credentials. Do not change the trigger to execute untrusted pull-request code in a privileged job.
Step 3: Publish from the main branch
After review, a push to main runs the publishing job. It signs in with the job’s short-lived GitHub token, builds the image, starts that image for an HTTP check, and pushes it only after the check succeeds.
Step 4: Record the published image
Find the image tag and digest in the registry output. A commit-based tag helps identify source, but deployment should use the recorded digest when exact image contents matter. Publishing an image does not deploy it.
Check the result
Tests and the image HTTP check should fail the workflow if broken. Confirm pull-request jobs cannot publish and the registry image belongs to the expected commit.
Conversation
0 commentsNo comments yet. Start the conversation below.