CI Pipelines (GitHub Actions / GitLab CI)
Overview
Continuous Integration (CI) automates the building, testing, and validation of code changes. This tutorial covers the most popular CI tools: GitHub Actions, GitLab CI, and Jenkins. You'll learn to write CI pipelines that catch bugs early, enforce code quality, and accelerate development cycles.
1. Introduction to CI
Continuous Integration is the practice of automatically building and testing every code change that is pushed to the repository.
Key benefits
- Early bug detection: Issues are caught before they reach production.
- Faster feedback: Developers know within minutes if their changes break anything.
- Reduced manual work: Automated builds and tests replace manual steps.
- Consistent builds: Every build runs in a clean environment.
CI Pipeline Stages
- Checkout: Clone the repository.
- Install: Install dependencies.
- Lint: Check code style and quality.
- Test: Run unit and integration tests.
- Build: Compile or package the application.
- Deploy: (Optional, often part of CD).
2. GitHub Actions
GitHub Actions is a CI/CD platform integrated with GitHub repositories.
Key concepts
- Workflow: A YAML file defining the CI pipeline.
- Job: A set of steps that run on a runner.
- Step: A single action or shell command.
- Action: Reusable pieces of code (official or community).
- Runner: The machine where jobs run (GitHub‑hosted or self‑hosted).
3. GitLab CI
GitLab CI is integrated with GitLab repositories and offers a powerful pipeline system.
Key concepts
- Stages: Define the order of pipeline execution.
- Jobs: Tasks that run within stages.
- Runners: Agents that execute jobs.
- Artifacts: Files passed between jobs.
- Environment variables: Configure jobs dynamically.
4. Jenkins
Jenkins is a self‑hosted CI/CD server with extensive plugin support.
- GitHub Actions: Integrated with GitHub, easy to use, extensive marketplace.
- GitLab CI: Integrated with GitLab, powerful, self‑hosted option.
- Jenkins: Self‑hosted, highly customisable, mature ecosystem.
5. CI Best Practices
- Keep pipelines fast: Optimise build times (caching, parallel jobs).
- Fail fast: Run linting and quick tests before expensive builds.
- Use caching: Cache dependencies to speed up builds.
- Run on pull requests: Test every PR before merging.
- Secure secrets: Use environment variables or secret managers.
- Monitor builds: Set up notifications for failures.
Quiz
Question 1
What is the primary purpose of Continuous Integration?
- To deploy to production automatically
- To automatically build and test every code change
- To monitor application performance
- To manage team permissions
Show answer
Question 2
Which file defines a GitHub Actions workflow?
- .github/workflows/ci.yml
- .gitlab-ci.yml
- Jenkinsfile
- docker-compose.yml
Show answer
Question 3
What is a Jenkinsfile used for?
- Defining a Docker container
- Defining a Jenkins pipeline as code
- Configuring GitHub Actions
- Writing tests
Show answer
Exercises
Exercise 1
Write a GitHub Actions workflow that lints, tests, and builds a React application on every push to main.
Sample answer
Exercise 2
Convert the GitHub Actions workflow above to GitLab CI format.
Sample answer
Homework
Homework 1
Set up a CI pipeline for your capstone project using GitHub Actions or GitLab CI. Include linting, testing, and building. Document the workflow file and any challenges you encountered.
Sample outline
- Tool: GitHub Actions.
- Stages: Checkout, Install, Lint, Test, Build.
- Challenges: Environment variables, caching, test coverage.
Mini‑Project
CI Pipeline for Full‑Stack App
Build a comprehensive CI pipeline for a full‑stack application that:
- Runs linting and tests for both frontend and backend
- Builds both applications
- Runs integration tests
- Checks test coverage and fails if below threshold
- Uploads build artifacts for later use
- Runs on both push and pull request
Sample outline
- Frontend: React app with Jest tests.
- Backend: Node.js API with Jest tests.
- Pipeline: Install → Lint → Test → Build → Upload artifacts.
- Coverage: Set threshold of 80%.
Tutorial Summary
You learned the fundamentals of Continuous Integration using GitHub Actions, GitLab CI, and Jenkins. You can now write CI pipelines that automatically lint, test, and build your code, giving you fast feedback and ensuring code quality.
Key takeaway: CI is the first step towards automated software delivery. It reduces bugs, improves code quality, and frees developers from manual tasks.