CircleCI 2.0 - SSH密钥丢失(从Circle 1.0迁移)

3
从Circle 1.0迁移到2.0。 我可以轻松获取我的代码,但用于部署的ssh密钥似乎不可用。
该密钥在项目ssh权限中 project ssh keys permissions 没有~/.ssh/config文件,密钥也不在那里: enter image description here 因此,当部署步骤开始时,它会失败:
#!/bin/bash --login
if [ "${CIRCLE_BRANCH}" == "develop" ]; then
  bundle exec cap staging deploy
else
  echo "Not on develop branch"
fi

(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as [redacted]@staging.captaincontrat.com: Authentication failed for user [redacted]@staging.captaincontrat.com

Net::SSH::AuthenticationFailed: Authentication failed for user [redacted]@staging.captaincontrat.com

Tasks: TOP => rvm:hook
(See full trace by running task with --trace)
Exited with code 1

我尝试使用add_ssh_keys步骤,但是密钥仍然无法使用。正如文档指定的默认添加所有密钥,我将其移除。
这是config.yml文件,其中大部分是迁移脚本的结果:
version: 2
jobs:
  build:
    working_directory: ~/captaincontrat/captaincontrat
    parallelism: 1
    shell: /bin/bash --login
    environment:
      CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
      CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
    # As our ruby version is a bit old, we can't use a pre-configured circle image.
    # So we need to use one with a large number of languages and other packages.
    # Once ruby is updated, choose a more recent image for better and faster builds.
    # https://circleci.com/docs/2.0/circleci-images/
    docker:
    - image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37
      command: /sbin/init
    steps:
    - checkout
    # Prepare for artifact and test results
    - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
    # Dependencies
    - run:
        name: Show me the available ssh keys
        command: 'ls -lha ~/.ssh'
    - run:
        name: Start redis
        command: 'sudo redis-cli ping >/dev/null 2>&1 || sudo service redis-server
          start; '
    # Restore the dependency cache
    - restore_cache:
        keys:
        # This branch if available
        - captaincontrat-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
        # Default branch if not
        - captaincontrat-develop-
    - run: gem install bundler
    - run: echo -e "export RAILS_ENV=test\nexport RACK_ENV=test" >> $BASH_ENV
    - run: 'bundle check --path=vendor/bundle || bundle install --path=vendor/bundle
        --jobs=4 --retry=3 '
    - save_cache:
        key: captaincontrat-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
        paths:
        - vendor/bundle
        - ~/.bundle
    - run: |-
        mkdir -p config && echo 'test:
          adapter: mysql2
          database: circle_ruby_test
          username: ubuntu
          host: localhost
        ' > config/database.yml
    - run:
        command: bundle exec rake db:create db:schema:load --trace
        environment:
          RAILS_ENV: test
          RACK_ENV: test
    # Test
    #   This would typically be a build job when using workflows, possibly combined with build
    - run: bin/rspec_all
    - run: bundle exec codeclimate-test-reporter $CIRCLE_ARTIFACTS/coverage/.resultset.json
    # Deploy if develop
    #   This should be in a workflow, but workflows can't cancel redundant jobs for now.
    - deploy:
        name: Deploy to staging if branch is develop
        command: |
           if [ "${CIRCLE_BRANCH}" == "develop" ]; then
             bundle exec cap -t staging deploy
           else
             echo "Not on develop branch => Not deploying to staging"
           fi
    # Teardown
    #   If you break your build into multiple jobs with workflows, you will probably want to do the parts of this that are relevant in each
    # Save test results
    - store_test_results:
        path: /tmp/circleci-test-results
    # Save artifacts
    - store_artifacts:
        path: /tmp/circleci-artifacts
    - store_artifacts:
        path: /tmp/circleci-test-results

我缺少了什么吗?谢谢!

编辑:这是解决方案

关键点如下:
- add_ssh_keys
- 然后在cap deploy之前运行evalssh-agent&& ssh-add ~/.ssh/id_rsa*,因为我需要通过代理转发从repo检出代码的.ssh/id_rsa

# Deploy if develop
- add_ssh_keys
- deploy:
    name: Deploy to staging if branch is develop
    command: |
       if [ "${CIRCLE_BRANCH}" == "develop" ]; then
         eval `ssh-agent` && ssh-add ~/.ssh/id_rsa* && bundle exec cap staging deploy
  # ... snip

为了确保代理转发,您可以将set :ssh_options, forward_agent: true添加到Capistrano阶段配置中。

只是一个提示:你可以回答自己的问题,不必使用问题本身来提供答案。这样我们就可以对答案进行投票和评论了。 :) - keen
1个回答

3
您需要添加add_ssh_keys步骤才能将SSH密钥注入到容器中。此步骤需要在deploy步骤之前执行。
文档中提到默认情况下添加了所有密钥,是指使用add_ssh_keys时。之所以这样说,是因为您也可以使用该特殊步骤来添加特定密钥。

谢谢!那是第一步。然后我遇到了一些问题,无法正确转发ssh-agent和密钥,但我已经解决了它。 - Yannick Fonjallaz
很好。如果可能的话,在这里添加你所做的内容作为注释,可能会帮助未来在SO上找到这个问题的人们。 - FelicianoTech
1
关键点是:
  • add_ssh_keys
  • cap deploy之前运行eval \ssh-agent` && ssh-add ~/.ssh/id_rsa*,因为我需要.ssh/id_rsa`通过代理转发从repo检出代码
`

如果是develop分支则部署

  • add_ssh_keys
  • deploy: name: 如果是develop分支则部署到staging command: | if [ "${CIRCLE_BRANCH}" == "develop" ]; then eval ssh-agent && ssh-add ~/.ssh/id_rsa* && bundle exec cap staging deploy # ... snip
为确保代理转发,您可以将set :ssh_options, forward_agent: true`添加到capistrano阶段配置中。
- Yannick Fonjallaz
已添加解决方案以回答问题,因为评论的格式不太适合我想提供的详细信息级别 :) - Yannick Fonjallaz
已经在 Circle 上开了一个问题,希望能够纠正这个文档 - https://github.com/circleci/circleci-docs/issues/2462 - keen
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接