2017-06-05 539 views
4

我一直在爲我的項目設置CD。我的Gitlab CI跑步者和我的項目將在同一臺服務器上。我跟着https://docs.gitlab.com/ee/ci/examples/deployment/composer-npm-deploy.html,但我總是收到SSH Permission denied (publickey,password).錯誤。我的所有變量,私鑰和其他變量都可以在項目設置中正確設置。Gitlab CI - SSH權限被拒絕(公鑰,密碼)

我用ssh-keygen -t rsa -C "[email protected]" -b 4096命令創建了我的ssh密鑰,沒有密碼,並且設置我的PRODUCTION_PRIVATE_KEY變量的內容爲~/.ssh/id_rsa文件。

這是我gitlab-ci.yml

stages: 
    - deploy 

deploy_production: 
    stage: deploy 
    image: tetraweb/php 
    before_script: 
    - 'which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)' 
    - eval $(ssh-agent -s) 
    - ssh-add <(echo "$PRODUCTION_PRIVATE_KEY") 
    - mkdir -p ~/.ssh 
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config 
    - apt-get install rsync 
    script: 
    - ssh [email protected]$PRODUCTION_SERVER 
    - hostname 
    only: 
    - master 

這是從Gitlab CI亞軍輸出:

Running with gitlab-ci-multi-runner 9.2.0 (adfc387) 
    on ci-test (1eada8d0) 
Using Docker executor with image tetraweb/php ... 
Using docker image sha256:17692e06e6d33d8a421441bbe9adfda5b65c94831c6e64d7e69197e0b51833f8 for predefined container... 
Pulling docker image tetraweb/php ... 
Using docker image tetraweb/php ID=sha256:474f639dc349f36716fb98b193e6bae771f048cecc9320a270123ac2966b98c6 for build container... 
Running on runner-1eada8d0-project-3287351-concurrent-0 via lamp-512mb-ams2-01... 
Fetching changes... 
HEAD is now at dfdb499 Update .gitlab-ci.yml 
Checking out dfdb4992 as master... 
Skipping Git submodules setup 
$ which ssh-agent || (apt-get update -y && apt-get install openssh-client -y) 
/usr/bin/ssh-agent 
$ eval $(ssh-agent -s) 
Agent pid 12 
$ ssh-add <(echo "$PRODUCTION_PRIVATE_KEY") 
Identity added: /dev/fd/63 (rsa w/o comment) 
$ mkdir -p ~/.ssh 
$ echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config 
$ apt-get install rsync 
Reading package lists... 
Building dependency tree... 
Reading state information... 
rsync is already the newest version. 
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 
$ ssh [email protected]$PRODUCTION_SERVER 
Pseudo-terminal will not be allocated because stdin is not a terminal. 
Warning: Permanently added '{MY_SERVER_IP}' (ECDSA) to the list of known hosts. 
Permission denied, please try again. 
Permission denied, please try again. 
Permission denied (publickey,password). 
ERROR: Job failed: exit code 1 

在此先感謝。

+2

您需要將公鑰添加到服務器,以便將其識別爲身份驗證密鑰。 – Jakuje

+0

我使用gitlab.com的gitlab。在這種情況下,我們指的是服務器gitlab.com的權利?我已經將公鑰添加到我的帳戶。我應該通過gitlab-ci.yml添加公鑰嗎?如果是這樣,怎麼樣?感謝您的答覆btw。 – kursat

+0

不,我正在談論'$ PRODUCTION_SERVER'。 – Jakuje

回答

4

您需要將公鑰添加到服務器,以便將其識別爲身份驗證密鑰。這是將與您正在使用的私鑰相對應的公鑰內容粘貼到$PRODUCTION_SERVER上的~/.ssh/authorized_keys

+0

謝謝,這救了我一個小時左右的牙齒拉xD – Horse