为什么我的gitlab ci配置退出代码1?

时间:2017-12-05 08:10:49

标签: gitlab-ci

我是Gitlab CI的新手。

我尝试使用ssh并执行ls命令来检查我的gitlab CI配置是否正确。

这是我的.gitlab-ci.yml文件配置:

before_script:
  - eval $(ssh-agent -s)
  - apt-get update
  - apt-get install sshpass

stage_deploy:
  only:
    - testing
  script:
    - sshpass -p $STAGING_PRIVATE_KEY ssh -p 20000 root@1.2.3.4 "ls"

每当我触发存储库推送时,它总是会出现错误消息错误:作业失败:退出代码1 ,这是作业结果:

Running with gitlab-runner 10.2.0 (0a75cdd1)
  on docker-auto-scale (e11ae361)
 Using Docker executor with image ruby:2.1 ...
 Using docker image sha256:9f27f70631c32ca0e5946c012e80704061ee559b30cb89e652c0936852e93e86 for predefined container...
 Pulling docker image ruby:2.1 ...
 Using docker image ruby:2.1 ID=sha256:223d1eaa9523fa64e78f5a92b701c9c11cbc507f0ff62246dbbacdae395ffea3 for build container...
 section_start:1512460812:prepare_script
Running on runner-e11ae361-project-4813010-concurrent-0 via runner-e11ae361-srm-1512460662-97b95eb4...
section_end:1512460814:prepare_script
section_start:1512460814:get_sources
 Cloning repository... 
Cloning into '/builds/budiantoip/cicd-demo'...
 Checking out dde5cdc4 as testing... 
 Skipping Git submodules setup 
section_end:1512460816:get_sources
section_start:1512460816:restore_cache
section_end:1512460818:restore_cache
section_start:1512460818:download_artifacts
section_end:1512460819:download_artifacts
section_start:1512460819:build_script
 $ eval $(ssh-agent -s) 
Agent pid 11
 $ apt-get update 
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
Ign http://deb.debian.org jessie InRelease
Get:2 http://deb.debian.org jessie-updates InRelease [145 kB]
Get:3 http://deb.debian.org jessie Release.gpg [2373 B]
Get:4 http://deb.debian.org jessie Release [148 kB]
Get:5 http://security.debian.org jessie/updates/main amd64 Packages [588 kB]
Get:6 http://deb.debian.org jessie-updates/main amd64 Packages [23.2 kB]
Get:7 http://deb.debian.org jessie/main amd64 Packages [9063 kB]
Fetched 10.0 MB in 6s (1563 kB/s)
Reading package lists...
 $ apt-get install sshpass 
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  sshpass
0 upgraded, 1 newly installed, 0 to remove and 62 not upgraded.
Need to get 11.2 kB of archives.
After this operation, 65.5 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian/ jessie/main sshpass amd64 1.05-1 [11.2 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 11.2 kB in 0s (12.0 kB/s)
Selecting previously unselected package sshpass.
(Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 21168 files and directories currently installed.)
Preparing to unpack .../sshpass_1.05-1_amd64.deb ...
Unpacking sshpass (1.05-1) ...
Setting up sshpass (1.05-1) ...
sshpass -p $STAGING_PRIVATE_KEY ssh  root@1.2.3.4 "ls"
ERROR: Job failed: exit code 1

有关正在发生的事情的任何线索?

2 个答案:

答案 0 :(得分:1)

我认为您缺少的是~/.ssh/config文件:

- mkdir -p ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

您是否尝试过Gitlab自己提供的示例?

image: ruby:2.1
before_script:
# install ssh-agent
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

# run ssh-agent
- eval $(ssh-agent -s)

# add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
- ssh-add <(echo "$SSH_PRIVATE_KEY")

# disable host key checking (NOTE: makes you susceptible to man-in-the-middle attacks)
# WARNING: use only in docker container, if you use it with shell you will overwrite your user's ssh config
- mkdir -p ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

Test SSH:
  script:
  # try to connect to GitLab.com
  - ssh git@gitlab.com

  # try to clone yourself, the SSH_PRIVATE_KEY was added as deploy key to this repository
  - git clone git@gitlab.com:gitlab-examples/ssh-private-key.git

此示例位于此处:https://gitlab.com/gitlab-examples/ssh-private-key/blob/master/.gitlab-ci.yml

此外,文档提供了有关SSH设置的更多信息:https://docs.gitlab.com/ee/ci/ssh_keys/README.html

答案 1 :(得分:0)

您是否有意用1.2.3.4替换了您的真实服务器IP,还是您正在使用的真实配置?

我认为问题是作业失败的原因是它无法执行最后一个命令

sshpass -p $STAGING_PRIVATE_KEY ssh -p 20000 root@1.2.3.4 "ls"