For beginners! Automatic deployment with Rails6 + CircleCI + Capistrano + AWS (EC2)

Introduction

The other day, after creating the portfolio, I was able to implement automatic deployment with CI tools on AWS using Capistrano and CircleCI, so I will keep it as a memorandum.

As mentioned above, it is a memorandum, so there is no guarantee that it will be helpful.

Premise

・ Rails application can be created ・ Automatic deployment to EC2 with Capistrano

Capistrano will be automatically deployed by CI tool only when the above conditions are met.

Development environment

local

・ Rails6 ・ MySQL 5.6.50 ・ Docker ・ Docker-compose

Production

・ EC2 ・ RDS (MariaDB) ・ Unicorn ・ Nginx ・ Capistrano

Docker and docker-compose are not used in the environment of this application.

procedure

① Set the private key to log in to EC2 in CircleCI ② Write the ssh connection in .circleci/config.yml and check if you can log in. ③ Deploy with Capistrano when merging or pushing to the master branch of github

1) Set the private key for EC2 login in CircleCI

Many people often get stuck here, and the author himself was stuck for about two hours. First, it means ** I don't know which key I need **. All you need is a key called a key pair to use when logging in to EC2 from ssh.

Register the private key to be downloaded in the pem file format when creating an instance with CircleCI.

One caveat here is that CircleCI can only use pem format. In case of OPENSSH format, it is necessary to use pem format, so please refer to the following article and execute it. (In my case, it was in pem format from the beginning, so I'm not familiar with this area ...)

I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails

So, go to the terminal's ssh directory and run the following command:

Terminal


.ssh % cat XXXXXXXXXXXXX.pem
#Key pair for logging in to your EC2

Then, a very long cipher is output as shown below.

Terminal


-----BEGIN RSA PRIVATE KEY-----
WWIEXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#There are still more, but I will omit them
-----END RSA PRIVATE KEY-----%    

Copy this from ----- BEGIN RSA PRIVATE KEY ----- to ----- END RSA PRIVATE KEY -----%.

Alternatively, you can make a batch copy by the following method.

Terminal


.ssh % pbcopy < ~/.ssh/XXXXXXXXX.pem

You can use this command to copy the long cipher.

1-2) Register a key pair with CircleCI

Go to CircleCI's project and open project settings. (Click the gear icon in the upper right) 171a000a3a3105940bd7d90856926ca8.png

Then, the setting screen will be displayed as shown below. Select SSH Keys from the menu bar to set the key. 27f8250132f8c8f66b26738a49dd32dd.png

When the page switches, scroll down and click on the Add SSH Key below. ssh.png

Click it and it will look like the following. Enter Hostname and Private Key and click Add SSH Key.

You can put ElasticIP in Hostname, but in my case I got my own domain, so I entered it in the form of www.myapp.com.

Next, paste the code you copied earlier into Private Key. sshsh.png

After a while, it will return to the original screen. Then, I think that there are numbers, letters, and the character string ":" in the item Fingerprint. Make a copy of it and save it in a memo.

This completes the settings on CircleCI (GUI).

2) .Circlci/cofig.yml description

Next, check if you can SSH connection with CircleCI. Add the description under steps: -checkout of the existing description as shown below. In my case, before Capistrano, I built a CI pipeline with Heroku, so I added a description and git push.

ruby:.circleci/config.yml


- add_ssh_keys:
    fingerprints: "XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"

Let's add this description and confirm. It is OK if it succeeds as follows. install.png

3) Deploy with Capistrano when merged or pushed to the master branch of github

Here's actually the implementation of Capistrano auto-deploy with CI tools.

First of all, here is the successful code.

ruby:.circleci/config.yml


version: 2.1

orbs:
  ruby: circleci/[email protected]

jobs:
  build:
    docker:
      - image: circleci/ruby:2.6.5-node-browsers
        environment:
          BUNDLER_VERSION: 2.1.4
    steps:
      - checkout
      - run: gem install bundler -v 2.1.4
      - run:
          name: Which bundler?
          command: bundle -v
      - ruby/install-deps

  deploy:
    docker:
      - image: circleci/ruby:2.6.5-node-browsers
        environment:
          BUNDLER_VERSION: 2.1.4
    steps:
      - checkout
      - ruby/install-deps
      - add_ssh_keys:
          fingerprints: "XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"
      - deploy:
          name: Capistrano deploy
          command: bundle exec cap production deploy

workflows:
  version: 2.1
  build-deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master

I referred to the articles and officials in the references, but it was difficult (laughs).

To explain one by one, I wrote that docker's image matches my ruby ​​version and bundler's own version.

First, do build for code check as job, and install bundler.

And in deploy job, I run Capistrano and specify which SSH to connect to. This corresponds to the description add_ssh_keys: that was set or described earlier.

The composition is that Capistrano actually runs with the command below it.

And in workflows, it is set as only: master to be done when there is a push or merge in the master branch.

Detailed explanations are given in the following articles and the references referred to in the articles, so I think that it will be easier to understand if you also read them.

[CircleCI] rails5.2/Capistrano/Automatic deployment to AWS by CICD environment

I had a lot of trouble, but I managed to implement it. Everyone please work hard.

References

Official Document

I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails

Recommended Posts

For beginners! Automatic deployment with Rails6 + CircleCI + Capistrano + AWS (EC2)
I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails
Easy deployment with Capistrano + AWS (EC2) + Rails
Deploy to EC2 with CircleCi + Capistrano
[EC2 / Vue / Rails] EC2 deployment procedure for Vue + Rails
Procedure for publishing an application using AWS (7) Automatic deployment by Capistrano
Recipe for deploying Rails apps on AWS EC2
[Rails] Nginx, Puma environment deployment & server study [AWS EC2]
Tutorial to create a blog with Rails for beginners Part 1
[CircleCI] ECS automatic deployment --CircleCI edition-
Preparation for developing with Rails
(For beginners) [Rails] Install Devise
Tutorial to create a blog with Rails for beginners Part 2
Tutorial to create a blog with Rails for beginners Part 0
What to do if an SSH key authentication error occurs during automatic deployment to EC2 with Capistrano
Environment construction with Docker for beginners
[Rails] DB design for EC site
Launch Rails on EC2 (manual deployment)
rails AWS deployment is not reflected
Learn AWS for free with LocalStack
[AWS SDK] EC2 automatic construction script
[Rails] AWS EC2 instance environment construction
[Rails] AWS deployment error encounter summary
Deployed using Docker + Rails + AWS (EC2 + RDS)
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
Learn Java with "So What" [For beginners]
[Ruby on Rails] About bundler (for beginners)
Rails [For beginners] Implementation of comment function
[Rails] Image posting by CarrierWave [AWS EC2]
[AWS] Publish rails app with nginx + puma
Explanation of Ruby on rails for beginners ①