Karan Sharma

SSH Deployment with GitLab CI

1 minutes (192 words)

🔗SSH to a server

stages:
  - prepare
  - deploy

default:
  image:
    name: public.ecr.aws/ubuntu/ubuntu:20.04

.ssh-commands: &ssh-commands
  - apt-get -y update && apt-get install -y rsync
  - 'which ssh-agent || ( apt-get install -y openssh-client )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

sync-deploy-scripts:
  before_script:
    - *ssh-commands
  stage: deploy
  script:
    - rsync --cvs-exclude -ravz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" ./ ubuntu@<remote_ip>:/home/ubuntu/app
  only:
    - main

Tags: #Gitlab #CI/CD