git - 使用GitLab CI推送后将文件上传到服务器

标签 git ssh gitlab gitlab-ci

推送到暂存分支后,我尝试使用SSH自动将文件上传到远程服务器。

我的.gitlab-ci.yml中有以下GitLab CI命令:

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

GitLab作业说Job succeeded ...但是文件没有上传...实际上什么也没发生..但是我没有收到错误信息或警告...

enter image description here

我的CI文件中有错误吗?

编辑VonC

我已经添加了正确的权限,现在正在使用-v标志。日志如下所示:(我已替换了一些信息)
$ 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

最佳答案

您的image:before_script:部分意味着您正在运行在Docker容器内运行的CI / CD作业。

在这种情况下,“SSH keys when using the Docker executor”段提到:

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

如果添加正确的权限可以解决问题,我将首先进行测试。

并且您需要确保your ~/.ssh/known_hosts includes the target server own public key,与this script一样(但您在情况下选择了StrictHostKeyChecking no)。

我还将使用ssh -v -p22 ...来检查在执行ssh命令时是否打印了更多详细信息。

SSH命令将查找~/.ssh/id_rsa,我在您的脚本中看不到(仅$DD_PRIVATE_KEY)。因此,请仔细检查ssh -v的输出以查看ssh命令的实际作用。

关于git - 使用GitLab CI推送后将文件上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61840160/

相关文章:

powershell - 通过将密码与命令一起传递来使用 powerShell 脚本 ssh

powershell - 将 Gitlab CI Trigger Curl 转换为 Powershell Invoke-RestMethod

另一个 git 存储库中的 Git 存储库

git - Git 的提交日期或作者日期时间戳的分辨率是多少?

ssh - gcloud ssh -- 没有可用的受支持的身份验证方法(服务器发送 : publickey)

python子进程在后台运行远程进程并立即关闭连接

Gitlab Markdown 工件链接

gitlab - 定期删除 GitLab 中的合并分支?

git - 是否可以只将更改后的文件部署到 AWS Elastic Beanstalk 应用程序而不是上传我的整个源代码?

linux - Git 尝试解析尚未设置的代理