DEV Community

chornos13
chornos13

Posted on

1

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

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay