Lab 3 of 8 · CI/CD with GitHub Actions
Test and publish your image on every push
Create a pipeline that runs your tests and, on the main branch, publishes your Docker image to GitHub Container Registry.
- Time
- 60 minutes
- Cost
- Free for public repositories.
What you learn
Continuous integration runs your tests on every change; continuous delivery builds and publishes what passed. Teams ship many times a day because a pipeline checks every change, not a person.
- Workflows, jobs and steps
- Running tests automatically on every push and pull request
- Publishing an image to GitHub Container Registry
- Branches and pull requests
In the AI era: When AI writes more of the code, automated checks matter even more: the pipeline is what stops a confident but wrong change from reaching users.
Before you start
Finish labs 1 and 2.
Step by step
1 Create the workflow file
GitHub runs any workflow it finds in
.github/workflows. This one tests every push and pull request, then builds the image and pushes it only for commits onmain. Image names must be lowercase, so the second step lowercases your username.name: CI on: push: branches: [main] pull_request: permissions: contents: read packages: write jobs: test-and-build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: "3.12" - run: pip install -r requirements.txt - run: python -m pytest -q - name: Image name run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" - name: Log in to GitHub Container Registry if: github.event_name == 'push' uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push uses: docker/build-push-action@v6 with: context: . push: ${{ github.event_name == 'push' }} tags: ${{ env.IMAGE }}:${{ github.sha }},${{ env.IMAGE }}:latest2 Push and watch it run
Open your repository on GitHub and click the Actions tab.
mkdir -p .github/workflows # (save the file above as .github/workflows/ci.yml first) git add .github git commit -m "Add CI pipeline" git pushYou should see:A green tick next to "Add CI pipeline". Your image appears under Packages on your GitHub profile.
3 Break it on purpose, on a branch
Real teams change code on a branch and open a pull request. Change the test so it expects
"broken"instead of"ok".git switch -c try-failing-test sed -i.bak 's/{"status": "ok"}$/{"status": "broken"}/' tests/test_main.py && rm tests/test_main.py.bak git commit -am "Break the test on purpose" git push -u origin try-failing-testYou should see:GitHub offers "Compare & pull request". Open the pull request: the check turns red, and the image is not published.
4 Fix it and merge
Undo the change, push again, and watch the pull request turn green. Then click "Merge pull request" on GitHub.
git revert --no-edit HEAD git push # after merging on GitHub: git switch main git pullYou should see:The pull request check is green, and after the merge
mainpublishes a new image.
You are done when
The Actions tab shows a green run on
main.Your profile's Packages tab lists
ai-status-app.You opened, fixed and merged a pull request whose check failed first.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.