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.
・ 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.
・ Rails6 ・ MySQL 5.6.50 ・ Docker ・ Docker-compose
・ EC2 ・ RDS (MariaDB) ・ Unicorn ・ Nginx ・ Capistrano
Docker and docker-compose are not used in the environment of this application.
① 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
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.
Go to CircleCI's project and open project settings. (Click the gear icon in the upper right)
Then, the setting screen will be displayed as shown below. Select SSH Keys
from the menu bar to set the key.
When the page switches, scroll down and click on the Add SSH Key
below.
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
.
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).
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.
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.
I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails
Recommended Posts