CircleCI integration and configuration file setup.
CircleCI Setup
- Login with GitHub account to CircleCI
- Select “Add Project”
- Ensure that GitHub project has a Circle CI configuration file
.circleci/config.yaml
- Click “Start Building”
config.yaml
Documentation for setting up Circle CI 2.0 configuration files can be found here. The workflow section will define which jobs you would like to run.
The overall structure is as follows. You can define a number of jobs
and then specify which jobs to run in the workflows
section. An example can be found below.
version: 2 # use version 2.0 of CircleCI
workflows:
version:2
test: # Define jobs that are to be ran here
jobs:
- test-3.7
jobs: # Define all jobs here
test-3.7:
# EXAMPLE JOB
Docker file
Each job should consist of a Docker image to use hosted on Docker Hub. CirlceCI provides their own docker images here. Custom images can also be uploaded to Docker Hub and used.
Cache Dependencies
Dependencies can be cached with save_cache
step and can be saved in different locations for each jobs (for example using different versions of python requires different dependencies). The dependencies can then be loaded in step restore_cache
whilst specifying the key for the dependencies you would like to load. The checksum is to check if there are any additional dependencies to be install specified in requirements.txt
.
Running the tests
After activating the environment needed for testing, tests can be ran with pytest
whilst specifying an output in any format supported by pytest
.
Example job
Below is an example script for a job called test-3.7
.
test-3.7:
docker:
- image: circleci/python:3.7 # Docker image that CircleCI update
working_directory: ~/repo # Change working directory to repo directory
steps:
- checkout
- restore_cache: # Load previously cached dependencies
keys:
- v1-py37-dependencies-{{ checksum "requirements.txt" }}
- v1-py37-dependencies-
- run:
name: install dependencies
command: |
python -m venv venv
. venv/bin/activate
pip install -e .
- save_cache:
paths:
- ./venv
key: v1-py37-dependencies-{{ checksum "requirements.txt" }}
- run:
name: run tests
command: |
. venv/bin/activate
py.test --junit-xml=./fatf/tests/report37.xml
- store_test_results:
path: ./fatf/tests
Template Jobs
Template jobs can be used where only a few lines change per job. Below is an example of defining a default job called test-3.7
. Another job is defined as test-3.6
that uses the template but overrides the docker image to a version of python 3.6. However, in this example the dependency directories and virtual environments remind unchanged.
version: 2
workflows:
version:2
test:
jobs:
- test-3.7
- test-3.6
jobs:
test-3.7: &test-template # Define this template as test-template
docker:
- image: circleci/python:3.7
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v1-py37-dependencies-{{ checksum "requirements.txt" }}
- v1-py37-dependencies-
- run:
name: install dependencies
command: |
python -m venv venv
. venv/bin/activate
pip install -e .
- save_cache:
paths:
- ./venv
key: v1-py37-dependencies-{{ checksum "requirements.txt" }}
- run:
name: run tests
command: |
. venv/bin/activate
py.test --junit-xml=./fatf/tests/report37.xml
- store_test_results:
path: ./fatf/tests
test-3.6:
<<: *test-template # Use template defined previously called test-template
docker:
- image: circleci/python:3.6 # Change one line of the test-template to use a different docker image