Introduced CircleCI in a personal app, When I tried to deploy to EC2, I got some errors, so I will describe the solution.
config.yml
version: 2
jobs:
#build job:Create and test a Docker container on CircleCI
build:
docker:
- image: alpine
steps:
- checkout
- run:
name: Echo Test
command: echo "CircleCI Test"
#deploy job:SSH into EC2 and perform the deployment
deploy:
machine:
image: circleci/classic:edge
steps:
- checkout
#Call the private key registered with CircleCI
- add_ssh_keys:
#SSH using environment variables registered with CircleCI
- run: ssh ${USER_NAME}@${HOST_NAME} 'cd /var/www/myapp && git pull origin master'
workflows:
version: 2
# build_and_deploy job:Job called first
build_and_deploy:
#Call build and deploy jobs
jobs:
- build
- deploy:
requires:
#Run the build job before the deploy job!
- build
#Run the deploy job only if pushed to the master branch
filters:
branches:
only: master
ssh: connect to host ************* port 22: Connection timed out
【solution】 Edit the inbound of the EC2 security group as follows. Note) For security reasons, it is preferable to use an intermediate server.
Port range | Source |
---|---|
22 | My IP xxx.xxx〜 |
↓
Port range | Source |
---|---|
22 | Custom 0.0.0.0/0 |
(1) Execute the following command on EC2 to create a pem key (name it circleci-key etc. because it may conflict with other keys)
ssh-keygen -m pem
(2) Execute the following command in EC2 and copy the public key (key name.pub) created in (1) to authorized_keys.
cat ~/.ssh/circleci-key.pub >> authorized_keys
③ Execute the following command on EC2 to display the private key created in ①.
cat ~/.ssh/circleci-key
④ Set the displayed private key to CircleCI's SSH-Key (At this time, include ----- BEGIN RSA PRIVATE KEY ----- and ----- END RSA PRIVATE KEY -----)
In my case, this cleared the error, Please try it.
[How to use CircleCI](https://suwaru.tokyo/%E3%80%90%E4%BD%BF%E3%81%84%E6%96%B9%E3%80%91circleci%E3%81%A8 % E3% 81% AF% EF% BC% 9Fec2% E3% 81% ABssh% E3% 81% 97% E3% 81% A6% E3% 83% 87% E3% 83% 97% E3% 83% AD% E3 % 82% A4% E8% 87% AA% E5% 8B% 95% E5% 8C% 96 /)
Recommended Posts