1. Overview
CI/CD stands for:- CI: Continuous Integration
- CD: Continuous Delivery / Continuous Deployment
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
3. Continuous Delivery / Deployment
Continuous Delivery (CD) extends CI by preparing or deploying the application. For a Docker application: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:main branch.
5. GitHub Actions Workflow
A GitHub Actions workflow is normally stored inside:6. Workflow Syntax
A basic workflow:7. name
8. on
The on section defines when the workflow runs.
Example:
main.
Another example:
main.
Multiple triggers:
9. jobs
Jobs define the tasks that GitHub Actions performs.
10. runs-on
11. Steps
A job consists of multiple steps.12. uses
uses executes a reusable GitHub Action.
Example:
13. run
run executes shell commands.
Example:
14. Checkout Repository
The first step is usually:15. Setting Up Python
For a Python application:16. Installing Dependencies
Example:17. Automated Testing
Testing can be performed usingpytest.
Example:
18. Example Unit Test
tests/test_api.py
19. Docker Image Build
GitHub Actions can build the Docker image using:20. Image Tags
Docker images normally use tags. Example:21. GitHub Container Registry
GHCR stands for GitHub Container Registry. It allows Docker/OCI container images to be stored alongside GitHub repositories. Flow: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:
23. Login to GHCR
Docker can authenticate to GHCR using:24. Docker Build and Push Action
Instead of manually running separate Docker commands, the Docker GitHub Actions can build and push the image. Example:25. Complete ci.yml
For a FastAPI application whose Dockerfile is in the repository root:
26. Understanding needs
The second job contains:
needs:
27. Complete CI/CD Pipeline
28. Docker Image Naming
The expression:29. Important GitHub Actions Variables
GitHub provides predefined variables called contexts. Examples:30. Better Docker Image Tags
Instead of only using:latestpoints to the latest build.- Commit SHA identifies an exact version.
31. GitHub Actions Secrets
Secrets should be used for sensitive credentials. Examples:GITHUB_TOKEN, a separate registry password secret is generally unnecessary.
32. Workflow Status
GitHub displays the workflow status.33. Pull Request Workflow
CI is especially useful with pull requests.34. Important CI/CD Best Practices
Run tests before building
Pin important versions
Example:Use image tags
Avoid relying only on:Never hardcode secrets
Use:Keep workflows focused
Separate workflows can be created for:35. CI/CD with Docker Compose
For a Docker Compose application, the workflow can also validate the Compose configuration. Example:36. Local vs GitHub Actions
Local Development
CI/CD
37. Key Takeaways
- GitHub Actions automates development workflows.
- A workflow is stored in
.github/workflows/. ondefines when a workflow runs.jobsdefines tasks.stepsdefine individual operations.usesruns reusable GitHub Actions.runexecutes shell commands.pytestcan automate unit testing.- Docker images can be built automatically.
- GHCR stores Docker images.
GITHUB_TOKENcan authenticate workflows with GitHub services.permissionscontrols what the workflow can access.needscontrols job order.- Tests should pass before an image is pushed.
- Commit-based image tags provide version traceability.