gitlab runner

Gitlab Runner

Hi, folks,

This blog contains the steps on how to implement continuous integration and continuous deployment using the Gitlab runner.

Here in the following example, we have two separate servers.

Install Gitlab Runner in Linux Server:

To install Gitlab runner use the following command :

sudo apt-get install gitlab-runner

Once the git lab runner is installed, we should register the runners of the project.

  1. To register the runner, run the command,
sudo gitlab-runner register
  1. Once you hit enter it asks for instance URL,
  2. Next, It will ask for of token of the runner. This token can be seen in the repository.
Path for the token ----> Settings > CI/CD > Runners > Token

There are two types of runners in the Gitlab runners. They are

  • Specific Runners
  • Shared Runners

Specific Runners :

These runners are useful for the job of a project which has specific requirements.

Shared Runners:

These runners are useful for a job of different projects which has similar requirements.

4. Next, It will ask for the description for the runner.

5. Next, it will ask for tags associated with the runner ( these tags can be changed later in the UI)

6. Finally, it will ask for runner executor: ( Here I use Shell script for runner executor)

Now the Gitlab runner is registered successfully in the server.

Next, .gitlab-ci.yml file needs to be created by which the Gitlab runner is executed.

This .gitlab-ci.yml file should be created in the project root. This file triggers when the code is needed to push to the server.

Stages in .gitlab-ci.yml File:

Stages are a series of steps to reach the final destination. Gitlab allows you to define any number of stages with any names. Gitlab allows to run the stages one by one.  If anyone of the stages fails, it prevents the other ones to run.

Stages

  • build
  • test
  • staging
  • production

Example script for .gitlab-ci.yml

stages

    -deploy

deploy_to_prod

stage: deploy

script : “echo deploy to production && ssh of your server in which code needs to be deployed && commands to deploy the code in the server.

only:

   -master

To deploy the code in the server using Gitlab runner we should add the ssh key of the server to the repository.

Steps to add server ssh in the repository’s deployed key.

  1. Use cat ~/.ssh/id_rsa.pub command to get the ssh key of the server.
  2. Open your repository in the Gitlab and go to Settings > Repository > Deployed Keys(Expand). Add the ssh key of the server in the deployed keys.

Now, when a new code is pushed to master branch the code is deployed to the server successfully.

 

Leave a Reply

Your email address will not be published. Required fields are marked *