Gitlab CI - Deploy via SSH to remote server

28,028

Solution 1

ssh-copy-id is asking for a password. You can use sshpass -e and set the SSHPASS environment variable in Gitlab.

Solution 2

You are not passing the ssh key to rsync. You should do something like this which executes the ssh command to properly identify the ssh key:

rsync -avuz -e 'ssh -i ~/.ssh/deploy_rsa' $CI_PROJECT_DIR/dist/ [email protected]:/var/wwww/example.com
Share:
28,028

Related videos on Youtube

Rodrigo Moreno
Author by

Rodrigo Moreno

I'm developer for fun, I develop #Nodejs & #MongoDB. Also I create apps for coexist #iOS #Android. I go to #Hackathons. #TeamKids. require(@LeZelt);

Updated on September 18, 2022

Comments

  • Rodrigo Moreno
    Rodrigo Moreno over 1 year

    I have a Gitlab environment using Gitlab CI, for a new project to testify about the compiled files and copy via rsync to a production server.

    The machine where the build of these resources is exec is an image of docker (node 6), but now I have to copy the resulting files from that container Docker command to the server using linux ... My problem is to connect via ssh through rsync.

    Currently I have the following:

    stages:
      - deploy
    
    before_script:
        - npm i
        - npm run build
    
    job_deploy:
      stage: deploy
      script:
        - ssh-keygen -t rsa -b 4096 -C '' -f ~/.ssh/deploy_rsa
        - ssh-keyscan -H 8.8.8.8 >> ~/.ssh/known_hosts
        - ssh-copy-id -i ~/.ssh/deploy_rsa.pub [email protected]
        - rsync -avuz $CI_PROJECT_DIR/dist/ [email protected]:/var/wwww/example.com
      only:
        - master
    

    By this I'm getting:

        /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
        /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
        Permission denied, please try again.
        Permission denied, please try again.
        Permission denied (publickey,password).
    
    • Fábio Duque Silva
      Fábio Duque Silva almost 5 years
      Like @lrkwz, I am also missing the point of sending a new key on every build, when you are going to be asked for a password anyway. Also, I'd love to see the authorized_keys file on the remote server...
  • Yashu Mittal
    Yashu Mittal over 4 years
    Can you add an example too?