Deploy with GitHub Actions: Continuous Delivery Lab
Your pipeline tests every commit and builds an image, yet someone still copies that image to the server by hand, and afterwards nobody can say which version went out. In this lab you deploy with GitHub Actions instead: the image goes to a registry, then to staging, waits for a person's approval, and only then reaches production, with every deploy on record.
The pipeline shape
To deploy is to make a server run a new version. This lab's deploy.yml has four jobs: test, build (which also pushes the image to a registry), staging, and production, which waits for approval. Each is a gate, as on lesson 4's assembly line; because a person approves the last one, this is continuous delivery, not continuous deployment.
The image is built once, tagged with the commit SHA, and the same bytes reach both environments: the build-once rule of lesson 6. In the flowchart, follow the pink arrow: whichever gate fails, the run stops there and the author sees a red X on the commit.
Lab step 1: a deploy job that waits for a reviewer
The folder course-labs/ci-cd-and-devops-fundamentals/04-deploy holds lesson 13's sample app, now a small web server on port 8080, plus deploy.yml and compose.yaml. Here is its production job (trimmed):
production:
needs: staging
environment: production
concurrency:
group: production
cancel-in-progress: false
steps:
- name: Deploy with Docker Compose
working-directory: ci-cd-and-devops-fundamentals/04-deploy
env:
APP_VERSION: ${{ github.sha }}
run: |
export IMAGE_OWNER="${GITHUB_REPOSITORY_OWNER,,}"
docker compose pull
docker compose up -d --wait
needs: staging makes it wait for a green staging job, and environment: production for a reviewer, once you add that rule. Docker Compose runs the containers a YAML file describes; --wait returns once the app passes its health check, a request Docker repeats every few seconds. ${GITHUB_REPOSITORY_OWNER,,} is bash for your user name in lowercase, as image names must be.
GitHub's runners cannot reach your laptop, so the job's own runner stands in for the server. A real pipeline would run these commands on a server, or hand over to GitOps (lesson 11).
Create the rule before you push. In your fork, open Settings, Environments, New environment, type production and click Configure environment. Tick Required reviewers, add yourself, leave Prevent self-review unticked and click Save protection rules. As lesson 14 warned, naming a missing environment creates it with no rules. Then, from the top of your clone:
mkdir -p .github/workflows
cp ci-cd-and-devops-fundamentals/04-deploy/deploy.yml .github/workflows/
git add .github/workflows/deploy.yml
git commit -m "add deploy workflow"
git push
In the Actions tab, open Deploy and the run add deploy workflow. You should see four jobs in a line: test, build and staging turn green, and production waits. If it never waits, the rule was missing at the start. The diagram shows what happens next.
Click Review deployments, tick production and click Approve and deploy. You should see production start and turn green; Reject would fail the run.
Lab step 2: push to ghcr.io, run it with Docker Compose
The build job pushes the image to GitHub Container Registry, from lesson 7, with GITHUB_TOKEN, the token GitHub creates for each job. It asks for packages: write and logs in only on a push, so pull requests build but never publish, and staging's if: github.event_name == 'push' stops them deploying. The job, trimmed:
permissions:
contents: read
packages: write
steps:
- name: Log in to GitHub Container Registry
if: github.event_name == 'push'
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push the image
uses: docker/build-push-action@v7
with:
platforms: linux/amd64,linux/arm64
build-args: APP_VERSION=${{ github.sha }}
tags: ${{ steps.image.outputs.name }}:${{ github.sha }}
push: ${{ github.event_name == 'push' }}
build-args bakes the commit SHA into the image, and the app answers with it, so any server can tell you its version. platforms builds for x86-64 and arm64, with QEMU, an emulator, so the image also runs natively on Apple silicon Macs.
A new package starts private. One pushed from a workflow is linked to its repository, so the deploy jobs' packages: read token can still pull it, but your laptop cannot. Make it public: on your fork's main page, click sample-app under Packages, then Package settings, and under Danger Zone, Change visibility to Public. This cannot be undone.
The compose file is the "server". It runs exactly one commit's image, never latest, and publishes port 8080 on 127.0.0.1 only:
services:
app:
image: ghcr.io/${IMAGE_OWNER:-set_image_owner}/sample-app:${APP_VERSION:-set_app_version}
ports:
- "127.0.0.1:8080:8080"
The two variables say whose image and which commit. If you forget one, the name keeps a stand-in such as set_app_version and the pull fails rather than guessing.
Safety nets: one at a time, a smoke test, a way back
Concurrency. A concurrency group is a name that only one running job may hold. With group: production, two production deploys never overlap: a newer one waits as pending. If a third arrives, GitHub cancels the pending one, so production skips to the newest change. cancel-in-progress: false means a running deploy is never stopped halfway.
Smoke test. A smoke test, from lesson 8, is a quick check that the app works. After deploying, the job fails unless the app answers with the SHA it meant to deploy:
reply=$(curl --fail --silent --show-error http://localhost:8080/)
echo "$reply"
if ! grep -qx "version: $APP_VERSION" <<< "$reply"; then
echo "Expected version: $APP_VERSION"
exit 1
fi
--fail turns an HTTP error into a failed step, and grep -qx checks that one whole line reads version: plus the SHA, so a green test proves the right version is up. Its log shows Hello from the sample app, then version: and your 40-character SHA.
Rollback. Edit app/message.txt, keeping one line, then commit, push and approve; the new run deploys your greeting. Now redeploy the previous artifact, lesson 10's fastest rollback: open the older run add deploy workflow, click the re-run icon next to production under Jobs, then Re-run jobs.
A re-run uses the original run's commit SHA, so it pulls the old image, still in the registry, and deploys it after a fresh approval. Re-runs work for 30 days. In a real team, make on-call engineers reviewers so a rollback never waits, and revert the commit on main, or the next push ships it again.
Reading the deployment history
Each job that names an environment is recorded as a deployment: a version sent to that environment. On your fork's main page, click Deployments in the right sidebar, then production under Environments. You should see three: the first commit, your change and the re-run, each with its status, its commit and a View logs link to its run.
That is the register at work: the rollback erased nothing. Who approved each deploy is kept with its run; this prints it, where RUN_ID is the number at the end of the run's web address:
gh api repos/your-name/course-labs/actions/runs/RUN_ID/approvals --jq '.[] | .user.login + " " + .state'
You should see one line per approval, such as your-name approved. Not Found means a wrong run ID or user name.
Check yourself
You pushed deploy.yml before creating production, and it deployed without asking. Why?
The workflow named an environment that did not exist, so GitHub created it with no protection rules. Add the reviewer rule, and the next run waits.
Why does the smoke test check the version: line, not just that curl gets an answer?
An old container or the wrong image can answer too; only the SHA proves the intended version is the one running.
You rolled back by re-running run 1's production job. Next day a teammate pushes an unrelated fix. What reaches production?
The fix and the rolled-back greeting change, because main still holds it: a rollback changes what runs, not the code on main.