This content originally appeared on DEV Community and was authored by Mohammad Reza
In this article we try to just run a command from the pipline in our server
- 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
.gitlab-ci.yml
before_script:
- 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'
deploy:
type: deploy
script:
- ssh root@123.123.123.123 "pwd && ls"
only:
- main
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/id_rsa.pub >> ~/.ssh/authorized_keys
Just consider to change root@123.123.123.123
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
That is all
This content originally appeared on DEV Community and was authored by Mohammad Reza

Mohammad Reza | Sciencx (2021-12-28T13:26:43+00:00) ci/cd with gitlab (part one). Retrieved from https://www.scien.cx/2021/12/28/ci-cd-with-gitlab-part-one/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.