SSH keys are missing (migrating from Circle 1.0)


#1

Hello everyone!

Migrating from Circle 1.0 to 2.0.
I can fetch my code without any problem, but the ssh key used to deploy seems to be unavailable.

The key is in the project ssh permissions

There are no ~/.ssh/config file, and the key is not there either:
image

So when the deploy step kicks in, it fails:

#!/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

I tried using the add_ssh_keys step but the key remains unavailable.
As the documentation specifies that it adds all keys by default anyway, I removed it.

Here’s the config.yml file, most of which is the result of the migration script:

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

By the way: thanks to the Circle team for providing the migration script!

What am I missing‽
Thanks!


#2

It has been fixed! Turns out I do need the add_ssh_keys and also I needed to restart the ssh_agent to ensure id_rsa is added.

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
      CODECLIMATE_REPO_TOKEN: e9af40121ad6eb9b295bbc5e354960114a6b3342f05ca0d7485eb37bb1f7edbd
    # 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: 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: cp config/google_api_auth_test.json config/google_api_auth.json
    - 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.
    - 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
           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

#3

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.