DEV Community

chornos13
chornos13

Posted on

Gitlab-CI Config to deploy to your server via SSH

STEP 1: Login SSH without password
ssh-keygen

Output:
Enter file in which to save the key (/home/user/.ssh/id_rsa): (input for custom filename)
Enter passphrase (empty for no passphrase): (skip if you want)

ssh-copy-id -i ~/.ssh/id_rsa.pub UserName@RemoteServer

ref: https://www.shellhacks.com/ssh-login-without-password/

STEP 2: Pull Git without password on Server

Login to your server:
ssh root@ip

ssh-keygen -t ed25519 -C "email@example.com"

copy content from generated-file ssh to DEPLOY KEYS REPOSITORY GITLAB

Testing that everything is set up correctly :
ssh -T git@gitlab.com

ref: https://gitlab.com/help/ssh/README#generating-a-new-ssh-key-pair

git remote add bySSH git@gitlab.com:username/project.git

STEP 3: Configure .gitlab-ci.yml

before_script:

  • apt-get update -qq
  • apt-get install -qq git # Setup SSH deploy keys
  • '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_staging:
type: deploy
environment:
name: staging
script:
- ssh root@ip "cd /var/www/project-folder && git checkout dev && git pull bySSH dev && npm install && pm2 reload idpm2 && exit"

only:
- dev

ref: https://medium.com/@hfally/a-gitlab-ci-config-to-deploy-to-your-server-via-ssh-43bf3cf93775

Top comments (0)