Skip to main content

1. Overview

CI/CD stands for:
  • CI: Continuous Integration
  • CD: Continuous Delivery / Continuous Deployment
GitHub Actions can automate the software development workflow. A typical workflow for a Dockerized FastAPI application is:

2. Continuous Integration

Continuous Integration (CI) automatically checks new code whenever changes are pushed. Typical CI tasks:
  • Install dependencies
  • Run linting
  • Run unit tests
  • Check code quality
  • Build the application
Example:
If tests fail, the workflow stops.

3. Continuous Delivery / Deployment

Continuous Delivery (CD) extends CI by preparing or deploying the application. For a Docker application:
In this challenge, the CD part will push the image to GitHub Container Registry (GHCR).

4. What is GitHub Actions?

GitHub Actions is an automation platform integrated into GitHub. It can automatically execute tasks when events occur in a repository. Examples:
Example:
This means the workflow runs whenever code is pushed to the main branch.

5. GitHub Actions Workflow

A GitHub Actions workflow is normally stored inside:
Example project:

6. Workflow Syntax

A basic workflow:
A workflow contains:

7. name

Defines the name displayed in GitHub Actions. Example:

8. on

The on section defines when the workflow runs. Example:
The workflow runs when code is pushed to main. Another example:
The workflow runs when a pull request targets main. Multiple triggers:

9. jobs

Jobs define the tasks that GitHub Actions performs.
This creates two jobs:

10. runs-on

Specifies the operating system used by the GitHub-hosted runner. Common options include:
For Docker-based applications, Ubuntu is commonly used.

11. Steps

A job consists of multiple steps.
Each step performs one task.

12. uses

uses executes a reusable GitHub Action. Example:
This checks the repository code into the GitHub Actions runner.

13. run

run executes shell commands. Example:
Multiple commands:

14. Checkout Repository

The first step is usually:
Without checkout, the runner does not have the repository files available for testing or Docker builds. Flow:

15. Setting Up Python

For a Python application:
This installs/configures Python 3.11 on the runner.

16. Installing Dependencies

Example:
The runner now has the packages required to execute tests.

17. Automated Testing

Testing can be performed using pytest. Example:
Possible result:
If a test fails:
The workflow fails.

18. Example Unit Test

tests/test_api.py

Tests verify that the FastAPI endpoints work correctly.

19. Docker Image Build

GitHub Actions can build the Docker image using:
Flow:

20. Image Tags

Docker images normally use tags. Example:
For GHCR:
Example structure:
A tag can also represent a commit:
Using commit-based tags makes it possible to identify exactly which source version produced an image.

21. GitHub Container Registry

GHCR stands for GitHub Container Registry. It allows Docker/OCI container images to be stored alongside GitHub repositories. Flow:
Image format:

22. GITHUB_TOKEN

GitHub Actions automatically provides a GITHUB_TOKEN for workflows. It can be used to authenticate with GitHub services, including GHCR, when the workflow has appropriate permissions. Example:
Important:
Allows the workflow to read repository contents.
Allows the workflow to publish packages/images to GHCR.

23. Login to GHCR

Docker can authenticate to GHCR using:
The workflow now has permission to push the image.

24. Docker Build and Push Action

Instead of manually running separate Docker commands, the Docker GitHub Actions can build and push the image. Example:
Flow:

25. Complete ci.yml

For a FastAPI application whose Dockerfile is in the repository root:

26. Understanding needs

The second job contains:
This creates a dependency. Without needs:
With:
the flow becomes:
If tests fail:
The Docker image is not pushed.

27. Complete CI/CD Pipeline


28. Docker Image Naming

The expression:
automatically produces:
Therefore:
becomes something similar to:
This avoids hardcoding the GitHub username and repository name.

29. Important GitHub Actions Variables

GitHub provides predefined variables called contexts. Examples:
The user who triggered the workflow.
Repository name.
Commit SHA.
Git reference that triggered the workflow.

30. Better Docker Image Tags

Instead of only using:
images can also be tagged using the commit SHA. Example:
This produces:
Benefits:
  • latest points to the latest build.
  • Commit SHA identifies an exact version.

31. GitHub Actions Secrets

Secrets should be used for sensitive credentials. Examples:
Access syntax:
Do not write credentials directly in:
or inside application source code. For GHCR authentication with the built-in GITHUB_TOKEN, a separate registry password secret is generally unnecessary.

32. Workflow Status

GitHub displays the workflow status.
If something fails:
This provides immediate feedback after a code change.

33. Pull Request Workflow

CI is especially useful with pull requests.
Flow:
This prevents broken code from being merged into the main branch.

34. Important CI/CD Best Practices

Run tests before building

This prevents publishing an image from code that does not pass tests.

Pin important versions

Example:
and use stable base images where appropriate.

Use image tags

Avoid relying only on:
Use commit-based or release-based tags.

Never hardcode secrets

Use:
or:
where appropriate.

Keep workflows focused

Separate workflows can be created for:
when the application becomes more complex.

35. CI/CD with Docker Compose

For a Docker Compose application, the workflow can also validate the Compose configuration. Example:
This checks whether the Compose configuration can be parsed successfully. A more complete pipeline could be:

36. Local vs GitHub Actions

Local Development

CI/CD

The goal of CI/CD is to automate tasks that would otherwise be performed manually.

37. Key Takeaways

  1. GitHub Actions automates development workflows.
  2. A workflow is stored in .github/workflows/.
  3. on defines when a workflow runs.
  4. jobs defines tasks.
  5. steps define individual operations.
  6. uses runs reusable GitHub Actions.
  7. run executes shell commands.
  8. pytest can automate unit testing.
  9. Docker images can be built automatically.
  10. GHCR stores Docker images.
  11. GITHUB_TOKEN can authenticate workflows with GitHub services.
  12. permissions controls what the workflow can access.
  13. needs controls job order.
  14. Tests should pass before an image is pushed.
  15. Commit-based image tags provide version traceability.

Final CI/CD Architecture