Skip to content

Phase 4: Test, build, and publish with GitHub Actions

Purpose

Turn a Git push into a tested, traceable container image. This is the CI part of the lab. GitHub Actions does not connect to Kubernetes; it produces the artifact and records the new image tag in Git for Argo CD to find later.

1. Files used by CI

The workflow depends on:

File Use
.github/workflows/ci.yml Workflow triggers, job permissions, test, build, publish, and manifest update
tests/test-app.sh Small source-level checks for the static app
Dockerfile Builds the Nginx application image
app/index.html User-facing page and visible version
app/healthz HTTP endpoint used by the smoke test and Kubernetes probes
k8s/deployment.yaml Receives the immutable image SHA after a successful publish

The application files are intentionally simple. That keeps a failed source test, failed image build, failed registry push, and failed Kubernetes rollout easy to distinguish.

2. Workflow trigger and permissions

The workflow runs when application or build-related content is pushed to main. Deployment-only paths such as k8s/, argocd/, and README.md are ignored so the workflow's own manifest commit does not start an endless second build.

The job needs these repository-token permissions:

  • contents: write to commit the new image reference
  • packages: write to publish the image to GHCR

GITHUB_TOKEN is created for the workflow run. No personal access token or Kubernetes kubeconfig was added to the repository.

3. Test job

The test job checks out the repository and runs:

bash tests/test-app.sh

It verifies that the expected application files and version text are present. A real software project would run its unit tests, linter, dependency checks, and perhaps integration tests here. For a static page, these small assertions are enough to demonstrate that a failed check prevents publication.

4. Build and smoke-test the container

After source tests pass, the workflow builds the image from Dockerfile, starts it temporarily, and calls the health endpoint. The underlying operations are equivalent to:

docker build -t demo-app:test .
docker run -d --rm --name demo-app-test -p 8080:80 demo-app:test
curl --retry 10 --retry-delay 1 --fail http://127.0.0.1:8080/healthz
docker stop demo-app-test

The workflow owns container cleanup even when the health check fails. This test proves more than checking for files: Nginx must start and serve the endpoint from the built image.

5. Publish to GHCR

After the smoke test, the workflow signs in to ghcr.io with the repository's GITHUB_TOKEN and publishes two tags:

ghcr.io/aaronarphaxad/k8s-cicd-lab:<full-git-commit-sha>
ghcr.io/aaronarphaxad/k8s-cicd-lab:main

The SHA tag is the deployment record. It is immutable in normal use and maps the running artifact to a source revision. main is convenient for humans but moves on every successful release.

The first completed workflow showed:

  • Test job: passed
  • Publish job: passed
  • Source commit short SHA: 76dac6a

6. Find the GHCR package and make the lab image public

From GitHub:

  1. Open the user profile for the package owner.
  2. Open Packages.
  3. Select k8s-cicd-lab.
  4. Open Package settings.
  5. Under the danger-zone visibility control, change the package to Public and confirm the package name when prompted.

The repository being public does not always mean a newly created package has the expected visibility, so I checked the package itself.

Public visibility was a lab convenience. It let K3s pull anonymously without creating an imagePullSecret. A private application should keep the package private and provide narrowly scoped registry credentials through a Kubernetes Secret or an external secret manager.

7. Verify anonymous pull from a worker

Read the full image tag from k8s/deployment.yaml, then on either worker run:

sudo /usr/local/bin/k3s crictl pull \
  ghcr.io/aaronarphaxad/k8s-cicd-lab:<full-commit-sha>

If this fails:

curl -I https://ghcr.io/v2/
sudo journalctl -u k3s-agent -n 100 --no-pager
sudo /usr/local/bin/k3s crictl images

Check package visibility, the spelling and lowercase image path, the SHA tag, DNS, OPNsense outbound rules, and the worker's internet route.

8. Update the deployment manifest automatically

Only after the image is published does the workflow replace the image reference in k8s/deployment.yaml with the full SHA tag. It then commits and pushes that single desired-state change.

The order matters:

test
  -> build
  -> HTTP smoke test
  -> publish SHA image
  -> update k8s/deployment.yaml
  -> commit and push manifest

If publishing fails, Git must not claim that the unavailable image is desired. If the manifest commit fails, the image can exist harmlessly in GHCR while the cluster remains on the previous Git version.

Verify the result from the Debian VM:

git pull --ff-only
git log --oneline -n 5
git diff HEAD~1 -- k8s/deployment.yaml
grep -n 'image:' k8s/deployment.yaml

The image line should contain the full commit SHA from the successful workflow.

9. Follow and troubleshoot a workflow run

In GitHub > repository > Actions, open the run caused by the application commit and inspect jobs in order. The useful question at each stage is:

Stage Evidence
Trigger The expected source commit started the workflow
Test tests/test-app.sh exited successfully
Build Docker built Dockerfile without error
Smoke test /healthz returned success from the temporary container
Login GHCR authentication used GITHUB_TOKEN
Publish The full SHA tag exists in Packages
Manifest update A bot commit changed only the intended image line

Common causes of failure include a workflow path filter excluding the changed file, insufficient contents or packages permission, a package not linked to the repository, a protected branch rejecting the bot commit, or a test string that was not updated with the page version.

10. If application and deployment repositories are separate

The application can live in one repository and the Kubernetes manifests in a second GitOps repository. CI still tests and publishes the image from the application repository. After publication it updates the GitOps repository, usually by opening a pull request that changes the image tag.

Argo CD watches only the GitOps repository. This gives deployment configuration different permissions, reviews, and environment promotion rules. The combined repository used here is easier to learn and demonstrate, but the testing step does not depend on the manifests being beside the application.

Completion checks

  • Source test passes.
  • Container builds and serves /healthz.
  • GHCR contains the image tagged with the full source SHA.
  • Package visibility is public for this lab.
  • A worker can pull the image anonymously.
  • The workflow commits the exact image SHA to k8s/deployment.yaml.
  • The generated manifest commit does not start a recursive build.

Official references

Next: install Argo CD and register the application.