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

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.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store