How To Deploy A Git Repository To A Server Using GitLab CI/CD

10 Comments
Modified: 19.08.2023

Do you want to learn how to deploy a git repository to your own server using GitLab CI/CD? In this post, I will lead you through creating it step-by-step. This includes the steps on your server, the steps on GitLab, and the action itself. Therefore we will go through the different sections:

  1. The idea and the goal
  2. GitLab CI/CD to deploy a git repository on a server
  3. Conclusion
  4. Helpful Links
Don’t want to read? Watch the video instead!

The idea and the goal

The goal of this action is to push changes to your repository and to pull them onto your server automatically. This can be useful for deploying a website or a web app that automatically updates when the user visits/reloads your page.
To accomplish this, the idea of the action is to first connect to the server via ssh as a user with enough rights for the next steps. Then we want to change the directory to the directory containing the repository, change to the main branch, and pull the changes. Afterward, we can exit the shell again.

Server icon

VPS Hosting Course

Learn everything you need to know about servers and hosting your own applications!

GitLab CI/CD to deploy a git repository on a server

Now that we described the action’s goal and idea, we can start to set everything up. Follow the following steps:

On your server:

  1. Create a new user or use your own
  2. Check that user has access to the directory containing the repository (if the repository does not yet exist on the server, clone it into the directory you want)
  3. Create an SSH key:
    ssh-keygen -t rsa -b 4096
  4. Add the public key to the authorized_keys file:
    cat <path/to/public/key> >> ~/.ssh/authorized_keys
  5. Copy content of the private key file:
    cat <path/to/private/key> | base64 -w0

On the GitLab website:

  1. Go to “Settings > CI/CD > Variables > ‘Expand'”
  2. Create the following variables (mask the possible ones and make sure the private key is masked):
    • SSH_PRIVATE_KEY: content of the private key file encoded as base64
    • SSH_USER: user to access the server
    • SSH_HOST: hostname/ip-address of your server
    • WORK_DIR: path to the directory containing the repository
    • MAIN_BRANCH: name of the main branch (mostly main)
  3. Go to the “Repository” tab in your GitLab repository
  4. Click on “New file” and name it “.gitlab-ci.yml”
  5. Then paste the following content:
stages:
  - deploy

deploy:
  image: ubuntu:latest
  stage: deploy
  only: 
    - main
  before_script:
    - apt-get -yq update
    - apt-get -yqq install ssh
    - install -m 600 -D /dev/null ~/.ssh/id_rsa
    - echo "$SSH_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa
    - ssh-keyscan -H $SSH_HOST > ~/.ssh/known_hosts
  script:
    - ssh $SSH_USER@$SSH_HOST "cd $WORK_DIR && git checkout $MAIN_BRANCH && git pull && exit"
  after_script:
    - rm -rf ~/.ssh

The different blocks do the following:

Need help or want to share feedback? Join my discord community!

Conclusion

In this post, we create a GitLab CI/CD to deploy changes in a git repository to a server easily. I hope this little guide was helpful to you. In case you have any questions, feel free to ask in the comments or send me a mail at mail@programonaut.com (also check out the helpful links sections before).

If you liked this post, consider subscribing to my newsletter to get monthly updates!

KOFI Logo

If this guide is helpful to you and you like what I do, please support me with a coffee!

[convertkit form=2303042]

With these links, I solved problems that occurred along the way:

  1. Encode SSH_PRIVATE_KEY
  2. Bad owner or permissions on /home/<user>/.ssh/config

Discussion (10)