Linting code
With your new workflow ci.yml
, you are able to run unit tests. But another key operation in most CI/CD workflows is linting the project, ensuring that the code follows a certain standard and set of conventions.
Using Github Actions, we want a pull request to fail if the branch contains poorly linted code.
Constructing the workflow
As described in the previous section, we think of answering two key questions when constructing the workflow:
"When will it run?" — established to be during
pull_request
(and additionallyworkflow_dispatch
for testing)"What will it do?" — execute the
yarn lint
script given in thepackage.json
However, there is an additional question we will want to answer, given that we already have an existing workflow:
"Is this going to be a separate workflow? A separate job in the same workflow? Or just another step in the existing job?"
There is no right or wrong answer for the above. But it is worth considering the following factors:
Is this a part of the CI workflow? — yes, so we might not want to separate it out
Is the task a part of unit testing? — no, so we might want to split it out to avoid cluttering a single job
So in this case, we choose to create a separate job within the same workflow ci.yml
. By default, jobs will run in parallel, but can be designed to run sequentially. So, we get the added benefit of having both linting and unit tests running in parallel, saving time (arguable since we need to reinstall the project dependencies in each job, but as jobs get more complex, running them in parallel will allow simpler ones to complete first), and preventing the results of one job from affecting the other (one can fail while the others pass).
Before we dive into the code required, maybe take some time to think about and attempt to implement the above job! It is not very different from the previous implementation!
Designing the workflow
Breaking it down
You will notice that every step except the last is the same as the unit-tests
job. That is because the initial setup of the virtual machine runner does not change! We still need to
Fetch the repository
Setup Node.js
Install project dependencies
And this is all because all jobs run in separate virtual machine runners! So linting
does not share these steps with unit-tests
.
Linting code
The only step that differs between linting
and unit-tests
is the linting step, which we rely on the provided lint
script in package.json
, which calls eslint .
.
Visualizing the workflow
Now, the single workflow has evolved to include two parallel jobs!
Verifying the workflow
As mentioned at the start of this section, we will be verifying that the linting works by using the workflow_dispatch
event. So, once again, push the latest changes to ci.yml
to your fork and manually run the workflow:
This time, you will see that there are now two separate jobs running within the same workflow:
Both of them will also complete at around the same time since both linting and unit tests are relatively small at this time:
Try playing around with this new job. Create a branch and purposely commit and PR poorly linted code and see what happens! The linting
job should fail while the unit-tests
job will continue to work.
Amazing! We have not only setup a CI/CD workflow that runs unit tests, but also linting, and both run in parallel and don't affect each other's outcomes!
Let's tackle the the final piece of the puzzle: deploying the application to Github Pages!
Last updated