【问题标题】:Upload files to server after pushing with GitLab CI使用 GitLab CI 推送后将文件上传到服务器
【发布时间】:2020-05-19 16:55:15
【问题描述】:

在推送到暂存分支后,我尝试使用 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 显示Job succeeded...但文件未上传...实际上什么也没发生...但我没有收到错误消息或警告...

我的 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

【问题讨论】:

  • 看看这个tutorial。请注意:.gitlab-ci.yml 是一个 YAML 文件,因此您必须特别注意缩进。始终使用空格,不是制表符。
  • 谢谢????缩进是正确的.. 日志向我显示了最后的脚本行.. 但为绿色。似乎这些行被编译为字符串,而不是 ssh 操作...... ????
  • "...日志向我显示了最后的脚本行.." 你是什么意思?
  • 我用日志截图编辑了我的主题。脚本运行并显示 ssh 操作..
  • 无论脚本运行什么,您是否尝试过手动运行一次,看看是否有效果?

标签: git ssh gitlab gitlab-ci


【解决方案1】:

您的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 命令实际上在做什么。

【讨论】:

  • 感谢您的回答。添加您的信息后,我已使用当前日志文件编辑了我的问题。
  • @asored 从外观上看,它确实与 SSH 密钥 user@User-iMac.local 连接,并且 mkdir: 可能 rync 步骤以某种方式使脚本的其余部分未执行?尝试没有 rsync 部分的相同脚本,只是为了测试下一个 ssh 命令是否已执行(即使它失败)
猜你喜欢
  • 2017-02-28
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 2021-02-11
  • 2015-12-04
  • 2020-05-08
相关资源
最近更新 更多