Error when deploying EC2 on CircleCI

Introduction

Introduced CircleCI in a personal app, When I tried to deploy to EC2, I got some errors, so I will describe the solution.

environment

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

Problems and solutions that occurred

① Error due to the port not being open

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

② Error due to mismatch of private key

(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 -----)

Finally

In my case, this cleared the error, Please try it.

reference

[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

Error when deploying EC2 on CircleCI
Error when deploying EC2
Error encountered with notes when deploying docker on rails
DB error on deploying with Heroku
EC2 on Docker-compose
Error when running git push heroku master on Heroku
Error installing ruby execution environment on EC2 instance
When deploying to Heroku, OpenApp causes an error
error code 400 appears when deploying with release: perform
Recipe for deploying Rails apps on AWS EC2
Possibility when deploying to EC2 but nothing is displayed in the error log
Disk expansion when booting CentOS 8.2 of aarch64 on EC2
Launch Rails on EC2
Error when using SnapKit
Deploy RAILS on EC2
Error when bundle install
1002 error on tomcat + webSocket
Error resolution on Heroku
Show detailed error in Logger when running Java on server
Gcc error solution with "pip install annoy" on AWS EC2
Put Oracle Java 8 on CircleCI 2
Error when playing with java
Install docker on AWS EC2
Error when using rails capybara
SpringBoot + Mybatis error when booting
Error when introducing SNS authentication