AI Hints
Home›CI/CD with GitHub Actions Tutorial›Projects: CI and Delivery
CI/CD with GitHub Actions · Lesson 120 of 138

Project: Docker Delivery

Test, then ship an image.

⏱ 2 min read▶ Video at 2:20:08🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Third project. A delivery pipeline. On every push to main, or a version tag, test the code, then build a Docker image and push it to the registry.

Project: Docker Delivery
From the video: Project: Docker Delivery
  • Push to main or a tag
  • Test first
  • Then build and push

delivery.yml

It runs on main and on version tags. The workflow can write packages. The test job runs first. The image job needs test, logs in to GHCR with the GitHub token, and builds and pushes an image tagged with the commit SHA.

.github/workflows/delivery.ymlyaml
name: Delivery
on:
  push: { branches: [main], tags: ["v*"] }
permissions: { contents: read, packages: write }
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: pip install -q -r requirements.txt && pytest -q
  image:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: docker/login-action@v3
        with: { registry: ghcr.io, username: "${{ github.actor }}", password: "${{ secrets.GITHUB_TOKEN }}" }
      - uses: docker/build-push-action@v6
        with: { push: true, tags: "ghcr.io/sam/shop:sha-${{ github.sha }}" }

Running it

Our helper runs the test job, which passes. Then the image job starts, because its needs are met. Its login and build steps only run on GitHub, so here they're marked GitHub only.

Terminal
run-steps .github/workflows/delivery.yml | grep -vE "^Workflow|100%|passed in"
Job: test
  ↷ uses actions/checkout@v4  (GitHub only)
  ✓ Run pip install -q -r requirements.txt && pytest -q
  Job succeeded
Job: image
  ↷ uses actions/checkout@v4  (GitHub only)
  ↷ uses docker/login-action@v3  (GitHub only)
  ↷ uses docker/build-push-action@v6  (GitHub only)
  Job succeeded

Summary

Test, then build and push, with the SHA tag. That image is now ready for any environment. Next, automating releases.

Key takeaways
  • needs: test
  • login + build-push
  • sha- tags
Next up: Project: Release Automation — tags to releases