This tutorial aims to introduce you to the concept of Continuous Integration (CI) and show you how this practice can significantly improve your software development process.
By the end of this tutorial, you will have a solid understanding of:
Basic knowledge of Git and version control systems is needed for this tutorial. Familiarity with a programming language and testing frameworks will also be beneficial.
Continuous Integration (CI) is a development practice where developers integrate their changes into a shared repository frequently. This results in multiple integrations per day. Each integration is then automatically tested and verified. By doing so, we can detect and address integration errors as quickly as possible.
The main advantages of CI are:
Here's a simple way to set up a CI workflow using GitHub Actions:
The workflow file (.github/workflows/ci.yml
) should look something like this:
name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: |
make test
This simple workflow runs on every push to the main
branch and automatically runs the make test
command.
Let's enhance our CI workflow to include a job that checks the code style using a linter:
name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
In this workflow, we're adding a step to set up Python, install dependencies, and then lint our code using flake8
. Finally, we run our tests using pytest
.
We've covered the basic concepts of Continuous Integration and how to set up a simple CI workflow using GitHub Actions. We've also looked at how to automatically test your code and check your code style every time you make a commit.
Next, you can explore more complex workflows, such as deploying your application to a staging environment, or running different types of tests in parallel.
Exercise 1: Create a simple CI workflow that runs unit tests for your application every time you push to the main
branch.
Exercise 2: Modify the workflow from Exercise 1 to also check your code style using a linter of your choice.
Exercise 3: Create a workflow that runs different types of tests (unit, integration, end-to-end) in parallel.
Remember, the more you practice, the more comfortable you'll become with setting up and managing CI workflows. Happy coding!