
こんにちは。iQeda [@iQeeeda] です。
CircleCI に SSH させて自動デプロイさせたい!Warning: Permanently added '**************' (ECDSA) to the list of known hosts.
けどこんなエラーが… 今回はその解消法をシェアします。
Warning: Permanently added ‘*’ (ECDSA) to the list of known hosts の原因
僕の場合、下記が原因のようでした。
- CircleCI がサーバに対して SSH できず、パスワードを問われてタイムアウトしていた
- 原因は CircleCI に正しい SSH Key が登録されていなかったこと
- さらに SSH Key に紐づく Fingerprint を config.yml に登録する必要があった
今回は Linode のインスタンスで起きた事象ですが、他のサービスでも起きうると思います。
.circleci/config.yml の設定内容
Laravel で開発した Web アプリをテストしたあとにデプロイする設定です。
大事なのは deploy ジョブだけなので、そこだけ注目してください!
# PHP CircleCI 2.0 configuration file
# See: https://circleci.com/docs/2.0/language-php/
version: 2
# Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
jobs:
build:
# Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
# See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor
docker:
# Specify the version you desire here
- image: circleci/php:7.3-node-browsers
environment:
- APP_DEBUG: true
- APP_ENV: ci-testing
- APP_KEY: base64:j0QhjXx+TEDfE5lgUCM//HMHlR95Mh26f7CEdFtcv9U=
- DB_CONNECTION: mysql
- DB_DATABASE: circle_test
- DB_USERNAME: root
- image: circleci/mysql:5.7
environment:
- MYSQL_DATABASE: circle_test
- MYSQL_ALLOW_EMPTY_PASSWORD: true
- MYSQL_ROOT_HOST: "%"
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# Using the RAM variation mitigates I/O contention
# for database intensive operations.
# - image: circleci/mysql:5.7-ram
#
# - image: redis:2.8.19
# Add steps to the job
# See: https://circleci.com/docs/2.0/configuration-reference/#steps
steps:
- checkout
- run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
- run: sudo docker-php-ext-install zip pdo_mysql
# Download and cache dependencies
- restore_cache:
keys:
# "composer.lock" can be used if it is committed to the repo
- v1-dependencies-{{ checksum "composer.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: composer install -n --prefer-dist
- save_cache:
key: v1-dependencies-{{ checksum "composer.json" }}
paths:
- ./vendor
- restore_cache:
keys:
- node-v1-{{ checksum "package.json" }}
- node-v1-
- run: yarn install
- save_cache:
key: node-v1-{{ checksum "package.json" }}
paths:
- node_modules
# prepare the database
- run: php artisan config:clear
- run: php artisan config:cache
- run: php artisan migrate
- run: php artisan db:seed
# run tests with phpunit or codecept
- run: ./vendor/bin/phpunit
- run: ./vendor/bin/phpstan analyse --memory-limit=2G
- run: php artisan insights --no-interaction --min-quality=70 --min-complexity=70 --min-architecture=70 --min-style=70
deploy:
machine:
image: circleci/classic:edge
steps:
- checkout
- add_ssh_keys:
fingerprints:
- "${KEY_FINGERPRINT}"
- run: ssh -o StrictHostKeyChecking=no ${USER_NAME}@${HOST_NAME} 'cd /var/www/OKR-manage-app/ && git checkout main && git pull && composer install --no-scripts --no-interaction && php artisan config:clear && php artisan config:cache && php artisan migrate'
workflows:
version: 2
build_and_deploy:
jobs:
- build
- deploy:
requires:
- build
filters:
branches:
only: main
add_ssh_keys
config.yml の中で最も重要な部分です。"${KEY_FINGERPRINT}" となっていますが、環境変数で Fingerprint の値を読み込んでいます。
まず CircleCI の設定画面から SSH Keys の設定をしてください。
ここで登録する秘密鍵は「すでに SSH できている秘密鍵」です。
~/.ssh/id_rsa の中身を登録するといった感じです。その秘密鍵を元に SSH Key を生成するので、CircleCI コンテナも同様に SSH できるようになる仕組みです。環境変数を登録する
Add SSH Key 登録後 Fingerprint が発行されます。これを config.yml 内に値をベタ書きするのは危険ですので、環境変数 KEY_FINGERPRINT として登録して参照するようにします。
このとき環境変数 HOST_NAME と USER_NAME も登録しておいてください。ssh -o StrictHostKeyChecking=no ${USER_NAME}@${HOST_NAME} で必要になります。
関連記事
お仕事ください!
僕が代表を務める 株式会社 EeeeG では Web 制作・システム開発・マーケティング相談を行っています。 なにかお困りごとがあれば、Twitter DM や Web サイトからお気軽にご相談ください。
カテゴリ「CI/CD」の最新記事



【使い方】CircleCIとは?EC2にsshしてデプロイ自動化
TerraformによるLinodeインスタンス新規作成サンプル
【Laravel】セッションタイムアウト後のログイン処理で前回URLに遷移するバグ修正
M1 Mac(2021)でanyenv/phpenvの初期設定!
コメント