- [Instructor] So, we're doing the writing
for our application.
Writing test to ensure that bugs are tracked and fixed
before pushing it to production.
However, a lot of human errors
can still creep into the testing process.
Developers might forget to run their tests suits
before pushing it to the main repo.
We need a way to automate this in such a way that
tests guard unhealthy code from being pushed to production.
In today's DevOps world,
the solution is continuous integration pipelines.
Continuous integration pipelines, monitor code repositories,
and ensure that tests run automatically
whenever new code is pushed to a branch.
This prevents errors from being pushed into the main branch
as a merge will not be possible until all tests are passing.
Continuous integration pipelines can be built from scratch
with tools like Jenkins and hosted privately.
However, except you have peculiar needs,
using a hosted service is preferred.
One of the most popular continuous integration
and deployment service today is CircleCI.
CircleCI is easy to set up
and uses the infrastructure as code paradigm
to provision a continuous integration pipeline
for your code repositories.
We'll be setting up a continuous integration or CI pipeline
to run the Jest tests contained in our application.
To begin building our CI pipeline,
make sure that you're on the 05_03b branch of the project.
Also ensure that you have pushed your code
to a GitHub repository.
I've already done that for this application.
The first step in the process of setting up a CI pipeline
on CircleCI is writing the pipeline script.
To do this, you need to create a '.cicleci' folder
at the root of your application.
Next, you need to create a yml configuration file
with the name config inside the '.circleci' folder.
This is the file that will contain
your continuous integration script.
I will go ahead and write the script,
then go over it when I'm done.
Now that we have this script, let's go over it.
I will advise you not to try and crack your brain
regarding the syntax here.
Just try to understand what the file is doing
as I explain.
CircleCI scripts have their formats and keywords
well-defined in their documentation.
And that should be your reference
for getting familiar with the platform.
The pipeline script simply consists of jobs,
which can be grouped into workflows.
Here we have defined a single job called build
and set it's working directory to '/repo'.
Next, we set the docker image to a CircleCI image
running node version 12.
This environment is the environment in which
our tests will run.
Next, we go through a series of steps where we
check out the code, update NPM,
install and cache dependencies.
Finally, we run the test in the 'package.json' file
using NPM.
This should be enough for our test to run,
when we push the code to the remote repository.
Save this file and push it to your remote repository
on GitHub.
The next step is to connect our repo to CircleCI.
Click on the login or signup button
at the top right-hand corner,
depending on whether you already have an account or not.
Either of the pages has links for logging in with GitHub.
Click on the GitHub link to get into your account.
I have already logged in with my GitHub account
and now on the Projects page.
The Projects page is where new projects
are connected to CircleCI.
You will see all the projects in your GitHub account
listed here.
To add the 'explore-california' project,
click on the setup project button on the right-hand side.
On the dialogue box,
select that you'll be adding your own configuration file.
Next, type in the name of the branch
on which the configuration file exists.
It's advisable that the file should be in the main branch.
This way, every branch that is pushed to the repo
would be tested.
For this exercise, I have entered the project branch
I am currently using to teach this exercise.
That is 05_03b.
Now click on the 'Let's Go' button
and let's watch the pipeline run.
Now that the pipeline has finished running,
we get a pass for the build.
Let's click on the build to view the details.
As seen, each step is in its own tab
and you can expand the tab to view the details.
The step we care about the most
is the one that runs the tests.
So let's jump to that.
Here, we see that our tests are successfully run
and we can go through the test results.
With this pipeline set up,
anytime we push code to the repository,
the tests will automatically run.
If the tests pass,
the code will be cleared to merge with the main branch.
If not the main branch will be protected
until the faulty code is fixed.