Upload files to server after pushing with GitLab CI

Viewed 1032

I try to upload files automatically to a remote server with SSH after pushing to staging branch.

I have the folling GitLab CI command in my .gitlab-ci.yml:

stages:
  - deploy

deploy_staging:
  stage: deploy
  image: tetraweb/php:7.1
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - ssh-add <(echo "$DD_PRIVATE_KEY")
    - apt-get install rsync
  script:
    - ssh -p22 ssh-xxxx@xxxxx.com "mkdir -p /website.com/_tmp"
    - rsync -rav -e ssh --exclude='.git/' --exclude='.gitlab-ci.yml' --delete-excluded ./ ssh-xxxx@xxxxx.com:/xxx.com/_tmp
    - ssh -p22 ssh-xxxx@xxxxx.com "mv /website.com/yyy/ /website.com/_old && mv /website.com/_tmp /website.com/yyy/"
    - ssh -p22 ssh-xxxx@xxxxx.com "rm -rf /website.com/_old"
  only:
    - staging

The GitLab Job says Job succeeded... but the files were not uploaded...in fact nothing happens.. but I get no error message or warnings...

enter image description here

Is there an error in my CI file?

Edit for VonC

I´ve added the right permissions and I´m using the -v flag now. The log looks like this: (I´ve replaced some informations)

$ ssh -v -p22 $HOST "mkdir -p $HOST:$ROOT/weburl/path/_tmp"
 OpenSSH_6.7p1 Debian-5+deb8u4, OpenSSL 1.0.1t  3 May 2016
 debug1: Reading configuration data /root/.ssh/config
 debug1: /root/.ssh/config line 1: Applying options for *
 debug1: Reading configuration data /etc/ssh/ssh_config
 debug1: /etc/ssh/ssh_config line 19: Applying options for *
 debug1: Connecting to web-address.host.com [85.13.163.167] port 22.
 debug1: Connection established.
 debug1: permanently_set_uid: 0/0
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_rsa type -1
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_rsa-cert type -1
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_dsa type -1
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_dsa-cert type -1
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_ecdsa type -1
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_ecdsa-cert type -1
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_ed25519 type -1
 debug1: key_load_public: No such file or directory
 debug1: identity file /root/.ssh/id_ed25519-cert type -1
 debug1: Enabling compatibility mode for protocol 2.0
 debug1: Local version string SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u4
 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
 debug1: match: OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 pat OpenSSH* compat 0x04000000
 debug1: SSH2_MSG_KEXINIT sent
 debug1: SSH2_MSG_KEXINIT received
 debug1: kex: server->client aes128-ctr umac-64-etm@openssh.com none
 debug1: kex: client->server aes128-ctr umac-64-etm@openssh.com none
 debug1: sending SSH2_MSG_KEX_ECDH_INIT
 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
 debug1: Server host key: ECDSA 43:89:10:22:d2:a6:54:f8:e0: .......
 Warning: Permanently added 'web-address.host.com,85.13.163.167' (ECDSA) to the list of known hosts.
 debug1: SSH2_MSG_NEWKEYS sent
 debug1: expecting SSH2_MSG_NEWKEYS
 debug1: SSH2_MSG_NEWKEYS received
 debug1: SSH2_MSG_SERVICE_REQUEST sent
 debug1: SSH2_MSG_SERVICE_ACCEPT received
 debug1: Authentications that can continue: publickey,password
 debug1: Next authentication method: publickey
 debug1: Offering RSA public key: user@User-iMac.local
 debug1: Server accepts key: pkalg ssh-rsa blen 407
 debug1: Authentication succeeded (publickey).
 Authenticated to web-address.host.com ([85.13.163.167]:22).
 debug1: channel 0: new [client-session]
 debug1: Requesting no-more-sessions@openssh.com
 debug1: Entering interactive session.
 debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
 debug1: Sending environment.
 debug1: Sending command: mkdir -p ssh-address.host.com:weburl/path/_tmp
 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
 debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
 debug1: channel 0: free: client-session, nchannels 1
 debug1: fd 0 clearing O_NONBLOCK
 debug1: fd 1 clearing O_NONBLOCK
 debug1: fd 2 clearing O_NONBLOCK
 Transferred: sent 3784, received 2560 bytes, in 0.4 seconds
 Bytes per second: sent 9472.1, received 6408.2
 debug1: Exit status 0
Running after_script
00:02
Saving cache
Uploading artifacts for successful job
00:01
 Job succeeded
1 Answers

Your image: and before_script: sections means you are running your CI/CD jobs run inside a Docker container.

In that case, the "SSH keys when using the Docker executor" paragraph mentions:

##
## Create the SSH directory and give it the right permissions
##
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh

I would test first if adding the right permission solves the issue.

And you need to make sure your ~/.ssh/known_hosts includes the target server own public key, as in this script (but you chose StrictHostKeyChecking no in your case).

I would also use an ssh -v -p22 ... in order to check if more details are printed when the ssh command is executed.

The SSH commands will looks for ~/.ssh/id_rsa, which I don't see in your script (only $DD_PRIVATE_KEY). So double-check the output of an ssh -v to see what an ssh command is actually doing.

Related