Setting up CI/CD on GitLab
With DevOps taking up the trend we all agree that Continuous integration(CI), Continuous Delivery(CD), Cloud, Automation and Configuration Management are basics and everyone wants a hand of it.
With proper implementation of CI, developers can review code faster and the push taking place after the changes being validated, with a lesser number of bugs.
CI/CD is essential to software development using Agile methodologies which suggest the use of automated testing and beta apps to be tested before being released to a larger base of users.
I recently had an experience in implementing CI/CD on GitLab and will like to share my experience and the approach to do it.
Step 1: Setting up a GitLab Account
Firstly get yourself on GitLab, finish the signup process and then create a repository and push the project or create a new one to it.
If you already have one configured then let’s get to Step 2.
Step 2: Configuring CI/CD on your Project
GitLab CI/CD is configured through a file called .gitlab-ci.yml placed in the root of a repository. There are certain scripts, commands defined in the file that is executed by GitLab runners.
Once the file is created, we need to edit it so we can create our own CI configuration that can meet our needs.
Your CI configuration will look like this and should be inside the file .gitlab-ci.yml.
image: node:latest stages: - build - test cache: paths: - node_modules/ setting_up: stage: build script: - npm install artifacts: paths: - node_modules/ testing: stage: test script: npm run test
Now lets understand what above snippet is all about.
The configuration file starts by declaring a docker image best suitable to run your project on.
Next we define the various stages(processes), of integration.
stages: - build - test
As the stages are defined, now a cache is defined that specifies the file that should be saved for later so it can be used for various stages.
cache: paths: - node_modules/
Next, are we defining a job as setting_up that describes what is to be done in the stage build? Typically we are trying to set up the environment to run our project by installing dependencies.
setting_up: stage: build script: - npm install artifacts: paths: - node_modules/
Lastly job testing describes the commands to be run during the stage test. As we cached the folder node_modules, it will be accessed by the job testing to run the command.
testing: stage: test script: npm run test
The names of the jobs i.e setting_up and testing can be any and were chosen by me, you can have yours.
Step 3: Configuring a GitLab Runner
Since our repository includes a .gitlab-ci.yml file that means every commit will trigger a new CI run. And to run the jobs we need a runner that can be a preconfigured runner by GitLab, triggers on every code push to execute the jobs.
But it has some restrictions on it so if you want to configure your own runner, I will recommend you to follow the instructions here.
Step 4: Register a GitLab Runner
As your runner is configured but you still need to register it that binds it with a GitLab Instance. Follow the instructions here.
Running the runner executor on the shell is the easiest to setup, so after registration, install and start the service with these commands:
$ gitlab-runner install $ gitlab-runner start
To make sure the runner is running run this command.
$ gitlab-runner status
If you see this: gitlab-runner: Service is running! , then the runner is up and running.
I hope I was able to get you through the process of setting up CI/CD with GitLab, will explain different components deeply in upcoming blogs. Feedbacks are always welcome.