Set Up a NodeJS API With CI on Gitlab for Your Web/Mobile App (Express/Jasmine/PostgreSQL/Docker) Part-3/3
In this document I will explain how to build a rest API with automatic tests on managed by Gitlab-CI. At the end of this series of documents you will be able to build your own API and especially to maintain it ! You will also be familiar with docker and the node testing framework Jasmine. The API that you will create can be used for your web application or/and for your mobile application.
The more you test the more you prevent bugs
Now that we have Dockerized our application (in the last article), I will firstly show you how to add tests quickly with a Makefile, then I will show you how to set up continuous integration with the file gitlab-ci.yaml.
Launch tests with makefile
We saw in the last article how to launch tests locally in docker, but we also saw that it was complicated, we needed to build our image at each modification, load our dump and launch our tests…
With a Makefile we can automatize all this process, and launch all these steps in one command.
Create a file at the root of your project called ‘makefile’.
In the file, copy/paste the following code :
sudo docker build -t myimage .
sudo docker-compose up -d
sudo docker exec postgres psql -U postgres -h postgres -f home/dumps/tests/medium.sql
sudo docker exec myimage npx jasmine spec/api.spec.js spec/*.js
sudo docker-compose down
you see above that we wrote all the steps, the keyword ‘sudo’ is optional at each line if you well downloaded docker.
So now if we add tests to our application, we will launch the command
in a terminal at the root to add them to the image myimage and launch them.
By launching this command, it will execute the steps bellow the keyword builded-test in the makefile.
So we will build our image, start the docker-compose, wait 5 seconds (to prevent containers bugs), load our dump in postgres container, exec tests in myimage container and stop the docker-compose.
All this in one command, well done ;) !
Create a gitlab-ci.yaml and follow pipeline creation
Now that we know how to simply create and run tests in docker, we can create our gitlab-ci.yaml !
We created a gitlab repository in the first article.
firstly, push all your updates on gitlab, so at the root of the project
git add .
git commit -m "feat : add tests"
then, go on your project in gitlab.com.
You can either click on Setup CI/CD on the top right corner and then commit your changes directly in gitlab or create a ‘.gitlab-ci.yml’ file at the root of your project.
Let’s create a .gitlab-ci.yml at the root, and copy paste the below code inside
- docker version
- docker-compose versionbuild:
- docker build -t myimage .
- docker-compose up -dtest:
- docker build -t myimage .
- docker-compose up -d
- sleep 5
- docker exec postgres psql -U postgres -h postgres -f home/dumps/tests/medium.sql
- docker exec myimage npx jasmine spec/api.spec.js spec/*.js
- docker-compose down
You can see on the top that we have two stages in our CI, every stages should be defined and should contains steps inside, we can see that under the key words build there is ‘stage: build’ and under test there is ‘stage: test’.
In build we verify that the docker image can be build and that the docker compose is working, and in test we build and launch our test like we did in the makefile !
gitlab-ci uses docker, that’s why we first run our test in docker locally previously.
To use Docker and Docker-compose, we need to precise in the begin of gitlab-ci.yml
it means that basic image of our application will be the docker/compose:latest image, and we use the service docker: dind (dind is the acronym for docker in docker) to use the cli docker in gitlab-ci.
follow pipeline, build and test
Pipelines are automatically launched at each push of your code on gitlab ! This is one of the strength of continous integration, if you broke something you will know it with your pipelines !
Now that you created your gitlab-ci.yml like above, push your code
git add .
git commit -m 'feat : add ci on the app'
After pushing your app on gitlab, go in gitlab, CI/CD, pipelines
You will see the list of all your pipelines, pipelines is composed of stages precised in your gitlab-ci.yml
click on the first pipeline, and you will see your stages
if you click on stages you will see logs of what happened during the execution on gitlab’s servers !
launch tests everyday at a hour
If you frequently want to launch tests, you can make it by scheduling your pipelines ! You can go in schedule on gitlab and specify when launch your pipelines automatically.
Now you know how to setup an API with CI, congratulations ! You are ready to start your own Web application or Mobile application connected to this API, you have a backend code easily maintainable and upgradable !
You can develop your API quietly without the risk of missing bugs and without make your code regressing !
Thanks for reading, see u ;)