A gitlab-ci config to deploy to your server via ssh

Henry Falade
Oct 7, 2018 · 2 min read

The prerequisite of reading this is that you are already familiar with SSH, GITLAB, and the Continuous Integration feature of gitlab, referred to as the gitlab-ci. Else, click on each to read more on them.

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 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).
  • Push

MODIFICATIONS

  • If you pay close attention to the file, you will see on LINE 17 root@example.com; you should change it to your server’s username@host.
  • Also on LINE 17, modify the cd var/… to change into your project’s root directory.
  • On LINE 15, change the URL to your website’s. This is optional though.
  • On LINE 19, it behaves like an event listener for a push to a branch. Thus, you can change it to any branch’s name. However, note that whenever an update is pushed to that branch, the auto deployment will start.
  • Make sure the branch git is pulling from on LINE 17 matches the branch name on LINE 19.

NOTE

  • This works for repositories on gitlab only.
  • Git must be installed on your server.
  • The project on your server must have been initially cloned from your repository / linked via git remote add … and in sync.

Please feel free to leave your comments (observations, questions and contributions).

Thanks.

Henry Falade

Written by

A programmer and web tech enthusiast! Love Angular & Laravel.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade