visit
Some basic knowledge of programming is assumed, as well as an understanding of the concept of Continuous Integration and Continuous Delivery (CI/CD).
It allows us to create workflows that are triggered by events. An example is a workflow that deploys the code to production when a pull request is merged into the main branch.
We will start by creating a file app.js
with a simple sum
function:
function sum(a, b) {
return a + b;
}
module.exports = { sum };
Next, we will create a file app.test.js
with one test case for the sum
function:
const sum = require("./app").sum;
test("add two numbers", () => {
expect(sum(3, 5)).toBe(8);
});
Finally, let’s add a test
script to our package.json
:
{
...
"scripts": {
...
"test": "jest"
},
...
}
We can run our test suite from the command line via npm test
.
The goal is to have the test suite run automatically every time we push a change to the main
branch or create a pull request.
To add workflows to a GitHub repository, we create a top-level folder .github
with a subfolder workflows
. In there, we add a YAML file for every workflow we want to have. In our case, that will be only one file test.yml
:
name: test
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16"
- run: npm ci
- run: npm test
name: test
👉 Quite straightforward: The name of our workflow.
on:
push:
branches:
- main
pull_request:
branches:
- main
👉 The on
block describes which events trigger the workflow. In this case, we want our test
workflow to run on any pushes to the branch main
and when a pull request into main
is created or updated.
jobs:
test:
runs-on: ubuntu-latest
👉 The jobs
block lists all jobs the workflow consists of. A job is executed on a runner and consists of one or more steps. In our case, we have only one job called test
which runs on an Ubuntu runner.
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16"
- run: npm ci
- run: npm test
👉 The steps
block describes exactly what our test
job does.
Each step can either run a reusable action or a custom script.
The provides numerous actions for different environments and use cases.To not reinvent the wheel, we use two of those actions in our first two steps to checkout the code and install Node v16 on our Ubuntu runner which we specified in the jobs
block.
In the next two steps, we run two commands to install the npm dependencies and execute the test
command from our package.json
. This is only possible because we set up the environment in steps 1 and 2 with the pre-built actions.
To activate the workflow we just created, all we have to do is push our code with the new .github/workflows
folder to our GitHub repository. The workflow will then automatically run on the specified events.
We can also go to the repository settings and set up branch protection rules for the main
branch, so that pull requests into main
can only be merged when the test
workflow runs successfully.
If we now create a new PR into main
, we can see that GitHub automatically runs our test
workflow and that merging is not possible before the checks pass.
(Note: You need to be logged in to a GitHub account to inspect those PRs.)