Configure CI/CD Pipeline in GitLab and deployment to server via SSH

GitLab offers a continuous integration service. For each commit or push to trigger your CI pipeline, you must:

  • Add a .gitlab-ci.yml file to your repository’s root directory.
  • Ensure your project is configured to use a Runner.

Install and Configure a Runner:

  1. Simply download one of the binaries for your system:

# Linux x86–64

sudo curl -L — output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Linux x86

sudo curl -L — output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386

# Linux arm

sudo curl -L — output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm

# Linux arm64

sudo curl -L — output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3

2. Give it permissions to execute:

sudo chmod +x /usr/local/bin/gitlab-runner

3. Create a GitLab CI user(it’s a single command):

sudo useradd — comment ‘GitLab Runner’ — create-home gitlab-runner — shell /bin/bash

Once the Runner has been set up, you should see it on the Runners page of your project, following Settings ➔ CI/CD.

Register a gitlab-runner:

Register here: https://docs.gitlab.com/runner/register/

Push .gitlab-ci.yml to GitLab:

git add .gitlab-ci.yml

git commit -m “Add .gitlab-ci.yml”

git push origin master

Deploy app to server via ssh

ssh-keygen

After entering the command, you should see the following output:

Output

Generating public/private rsa key pair.

Enter file in which to save the key (/your_home/.ssh/id_rsa):

Press ENTER to save the key pair into the .ssh/ subdirectory in your home directory, or specify an alternate path.

You should then see the following prompt:

Output

Enter passphrase (empty for no passphrase):

You should then see the following output:

Output

Your identification has been saved in /your_home/.ssh/id_rsa.

Your public key has been saved in /your_home/.ssh/id_rsa.pub.

The key fingerprint is:

a9:49:2e:2a:5e:33:3e:a9:de:4e:77:11:58:b6:90:26 username@remote_host

The key’s randomart image is:

+ — [ RSA 2048] — — +

| ..o |

| E o= . |

| o. o |

| .. |

| ..S |

| o o. |

| =o.+. |

|. =++.. |

|o=++. |

+ — — — — — — — — -+

To read the ssh key, run the command:

cat ~/.ssh/id_rsa

Now, assuming we have ssh connection to our server, and git is installed on our server we can go ahead with the following:

  • Make sure you can connect to your server via your ssh-key without requiring a password.
  • On gitlab, go to your repository > settings > CI/CD > Variables
  • Add a new variable SSH_PRIVATE_KEY. The value is your ssh private key (e.g content of ~/.ssh/id_rsa)
  • Add the file below to your project (with modifications).

Create Deployment Scripts in your server:

vim deploy.sh

Push .gitlab-ci.yml to GitLab:

git add .gitlab-ci.yml

git commit -m “Add .gitlab-ci.yml”

git push origin master

DevOps | Python Developer