DEV Community

Cover image for ci/cd with gitlab (part one)
Mohammad Reza
Mohammad Reza

Posted on

ci/cd with gitlab (part one)

In this article we try to just run a command from the pipline in our server

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


  - apt-get update -qq
  - apt-get install -qq git
  - "which ssh-agent || ( apt-get install -qq openssh-client )"
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

  type: deploy
    - ssh root@ "pwd && ls"
    - main

Enter fullscreen mode Exit fullscreen mode

After that you need to run this command because You need to add the public key to the server so it would be recognized as an authentication key.

cat ~/.ssh/ >> ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

Just consider to change root@ the use and also the IP address.
Now if you push to the main branch it will run the pipeline and then run the command on your server :))
In the next article we use this to deploy our project to the server automatically

Image description

That is all

Top comments (0)