You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: "Continuous Integration with GitHub Actions"
3
-
teaching: 10
4
-
exercises: 2
3
+
teaching: 20
4
+
exercises: 25
5
5
---
6
6
7
7
:::::::::::::::::::::::::::::::::::::: questions
@@ -20,39 +20,61 @@ exercises: 2
20
20
21
21
## Continuous Integration
22
22
23
-
Continuous Integration (CI) is the practice of automating the merging of code changes into a project.
24
-
In the context of software testing, CI is the practice of running tests on every code change to ensure that the code is working as expected.
25
-
GitHub provides a feature called GitHub Actions that allows you to integrate this into your projects.
23
+
Continuous Integration (CI) is the practice of automating the merging of code
24
+
changes into a project. In the context of software testing, CI is the practice
25
+
of running tests on every code change to ensure that the code is working as
26
+
expected. GitHub provides a feature called GitHub Actions that allows you to
27
+
integrate this into your projects.
26
28
27
-
In this lesson we will go over the very basics of how to set up a GitHub Action to run tests on your code.
29
+
In this lesson we will go over the very basics of how to set up a GitHub Action
30
+
to run tests on your code.
31
+
32
+
:::::: prereq
33
+
34
+
This lesson assumes a working knowledge of Git and GitHub. If you get stuck,
35
+
you may find it helpful to review the Research Coding Course's
36
+
[material on version control](https://researchcodingclub.github.io/course/#version-control-introduction-to-git-and-github)
37
+
38
+
:::::::::::::
28
39
29
40
## Setting up your project repository
30
41
31
-
- Create a new repository on GitHub for this lesson called "python-testing-course" (whatever you like really)
32
-
- Clone the repository into your local machine using `git clone <repository-url>` or GitKraken if you use that.
42
+
- Create a new repository on GitHub for this lesson called
43
+
"python-testing-course" (whatever you like really). We
44
+
recommended making it public for now.
45
+
- Clone the repository into your local machine using `git clone
46
+
<repository-url>` or via Github Desktop.
33
47
- Move over all your code from the previous lessons into this repository.
34
48
- Commit the changes using `git add .` and `git commit -m "Add all the project code"`
35
-
- Create a new file called `requirements.txt` in the root of your repository and add the following contents:
49
+
- Create a new file called `requirements.txt` in the root of your repository
50
+
and add the following contents:
36
51
37
52
```
38
53
pytest
39
54
numpy
40
55
pandas
41
-
pytest-mpl
42
-
pytest-regtest
43
-
matplotlib
44
56
```
45
57
46
-
This is just a list of all the packages that your project uses and will be needed later.
47
-
Recall that each of these are used in various lessons in this course.
58
+
This is just a list of all the packages that your project uses and will be
59
+
needed later. Recall that each of these are used in various lessons in this
60
+
course.
61
+
62
+
:::::: callout
48
63
64
+
Nowadays it is usually preferable to list dependencies in a file called
65
+
`pyproject.toml`, which also allows Python packages to be installed and
66
+
published. Look out for our upcoming course on reproducible environments to
67
+
learn more!
68
+
69
+
::::::::::::::
49
70
50
71
Now we have a repository with all our code in it online on GitHub.
51
72
52
73
## Creating a GitHub Action
53
74
54
-
GitHub Actions are defined in `yaml` files (these are just simple text files that contain a list of instructions). They are stored
55
-
in the `.github/workflows` directory in your repository.
75
+
GitHub Actions are defined in `yaml` files -- a structured text file which is
76
+
commonly used to pass settings to programs. They are stored in the
77
+
`.github/workflows` directory in your repository.
56
78
57
79
- Create a new directory in your repository called `.github`
58
80
- Inside the `.github` directory, create a new directory called `workflows`
@@ -66,93 +88,243 @@ Let's add some instructions to the `tests.yaml` file:
66
88
# This is just the name of the action, you can call it whatever you like.
67
89
name: Tests (pytest)
68
90
69
-
# This is the event that triggers the action. In this case, we are telling GitHub to run the tests whenever a pull request is made to the main branch.
91
+
# This sets the events that trigger the action. In this case, we are telling
92
+
# GitHub to run the tests whenever a push is made to the repository.
93
+
# The trailing colon is intentional!
70
94
on:
71
-
pull_request:
72
-
branches:
73
-
- main
95
+
push:
74
96
75
-
# This is a list of jobs that the action will run. In this case, we have only one job called build.
97
+
# This is a list of jobs that the action will run. In this case, we have only
98
+
# one job called build.
76
99
jobs:
77
-
build:
78
-
# This is the environment that the job will run on. In this case, we are using the latest version of Ubuntu, however you can ues other operating systems like Windows or MacOS if you like!
79
-
runs-on: ubuntu-latest
80
-
81
-
# This is a list of steps that the job will run. Each step is a command that will be executed on the environment.
82
-
steps:
83
-
# This command tells GitHub to use a pre-built action. In this case, we are using the actions/checkout action to check out the repository. This just means that GitHub will use this repository's code to run the tests.
84
-
- uses: actions/checkout@v3 # Check out the repository on github
85
-
# This is the name of the step. This is just a label that will be displayed in the GitHub UI.
86
-
- name: Set up Python 3.10
87
-
# This command tells GitHub to use a pre-built action. In this case, we are using the actions/setup-python action to set up Python 3.10.
88
-
uses: actions/setup-python@v3
89
-
with:
90
-
python-version: "3.10"
91
-
92
-
# This step installs the dependencies for the project such as pytest, numpy, pandas, etc using the requirements.txt file we created earlier.
93
-
- name: Install dependencies
94
-
run: |
95
-
python -m pip install --upgrade pip
96
-
pip install -r requirements.txt
97
-
98
-
# This step runs the tests using the pytest command. Remember to use the --mpl and --regtest flags to run the tests that use matplotlib and pytest-regtest.
99
-
- name: Run tests
100
-
run: |
101
-
pytest --mpl --regtest
100
+
101
+
build:
102
+
103
+
# This is the environment that the job will run on. In this case, we are
104
+
# using the latest version of Ubuntu, however you can use other operating
105
+
# systems like Windows or MacOS if you like!
106
+
runs-on: ubuntu-latest
107
+
108
+
# This is a list of steps that the job will run. Each step is a command
109
+
# that will be executed on the environment.
110
+
steps:
111
+
112
+
# This command tells GitHub to use a pre-built action. In this case, we
113
+
# are using the actions/checkout action to check out the repository. This
114
+
# just means that GitHub will clone this repository to the current
115
+
# working directory.
116
+
- uses: actions/checkout@v6
117
+
118
+
# This is the name of the step. This is just a label that will be
119
+
# displayed in the GitHub UI.
120
+
- name: Set up Python 3.12
121
+
# This command tells GitHub to use a pre-built action. In this case, we
122
+
# are using the actions/setup-python action to set up Python 3.12.
123
+
uses: actions/setup-python@v6
124
+
with:
125
+
python-version: "3.12"
126
+
127
+
# This step installs the dependencies for the project such as pytest,
128
+
# numpy, pandas, etc using the requirements.txt file we created earlier.
129
+
- name: Install dependencies
130
+
run: |
131
+
python -m pip install --upgrade pip
132
+
pip install -r requirements.txt
133
+
134
+
# This step runs the tests using the pytest command.
135
+
- name: Run tests
136
+
run: |
137
+
pytest
102
138
```
103
139
104
-
This is a simple GitHub Action that runs the tests for your code whenever a pull request is made to the main branch.
140
+
This is a simple GitHub Action that runs the tests for your code whenever code
141
+
is pushed to the repository, regardless of what was changed in the repository
142
+
or which branch you push too. We'll see later how to run tests only when
143
+
certain criteria are fulfilled.
105
144
106
145
## Upload the workflow to GitHub
107
146
108
147
Now that you have created the `tests.yaml` file, you need to upload it to GitHub.
109
148
110
149
- Commit the changes using `git add .` and `git commit -m "Add GitHub Action to run tests"`
111
-
- Push the changes to GitHub using `git push origin main`
150
+
- Push the changes to GitHub using `git push`
151
+
152
+
This should trigger a workflow on the repository. While it's running, you'll see an orange
153
+
circle next to your profile name at the top of the repo. When it's done, it'll change to
154
+
a green tick if it finished successfully, or a red cross if it didn't.
155
+
156
+
{alt="GitHub repository view with a green tick indicating a successful workflow run"}
157
+
158
+
You can view all previous workflow runs by clicking the 'Actions' button on the
0 commit comments