ci/cd with gitlab (part one)

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…


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

  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

.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

Image description

That is all


This content originally appeared on DEV Community and was authored by Mohammad Reza


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » ci/cd with gitlab (part one)." Mohammad Reza | Sciencx - Tuesday December 28, 2021, https://www.scien.cx/2021/12/28/ci-cd-with-gitlab-part-one/
HARVARD
Mohammad Reza | Sciencx Tuesday December 28, 2021 » ci/cd with gitlab (part one)., viewed ,<https://www.scien.cx/2021/12/28/ci-cd-with-gitlab-part-one/>
VANCOUVER
Mohammad Reza | Sciencx - » ci/cd with gitlab (part one). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/28/ci-cd-with-gitlab-part-one/
CHICAGO
" » ci/cd with gitlab (part one)." Mohammad Reza | Sciencx - Accessed . https://www.scien.cx/2021/12/28/ci-cd-with-gitlab-part-one/
IEEE
" » ci/cd with gitlab (part one)." Mohammad Reza | Sciencx [Online]. Available: https://www.scien.cx/2021/12/28/ci-cd-with-gitlab-part-one/. [Accessed: ]
rf:citation
» ci/cd with gitlab (part one) | Mohammad Reza | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.