[Ruby] [Circle CI] Procedure for automatic deployment when pushing to GitHub

3 minute read

Until now, auto deploy used “capistrano”, but every time I modified the code, I did a local master merge and run “bundle exec cap production deploy”.

This time, using circleCI, I tried to automatically SSH to the server and deploy the code after master merge. Below, we will summarize the flow of implementation and the explanation of the clogged up parts!

  • Development environment
  • ruby: 2.5.1
  • rails: 2.4.2.3
  • circleCI: 2.1

*Docker is not used this time.

.circleci/config.yml settings

(1) First, locally configure config.yml used in circleCI. To set circleCI, create a folder called .circleci directly under the root of the corresponding repository, and use config.yml in it.

  • Add the following to config.yml.

config.yml


version: 2.1
orbs:
  ruby: circleci/[email protected]

jobs:
  deploy:
    machine:
      enabled: true
    steps:
      -add_ssh_keys:
          fingerprints:
            -"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" After registering the private key of the #SSH server, copy and paste the hashed fingerprints.
      -run: ssh -p $SSH_PORT [email protected]$SSH_HOST "/var/www/repository_name/deploy.sh"

workflows:
  version: 2.1
  deploy:
    jobs:
      -deploy:
          filters:
            branches:
              only: master
  • After creating the “deploy.sh” file directly under the root, enter the following.

deploy.sh


#!/bin/bash

cd /var/www/repository name / && git pull

I will explain below. (1) I have set automatic deployment here, and it is a mechanism to automatically deploy when master merge. ② Below “- add_ssh_keys:”, when you register the HOST (IP address) and secret key, copy and paste the fingerprints that are converted into hashes. This allows you to register the private key of the SSH server. ③ With “-run: ssh -p ~”, ssh with the set key + set environment variable, and log in to the server. After that, “/var/www/rails/soup/deploy.sh”” is set so that git pull will be performed when pull request. ④Workflows The following settings prevent deployment other than the master branch.

cirecle CI introduction / Add repository to be automatically deployed

① First, go to circleCI’s website (https://circleci.com) and register a new account (sign up). If you have a GitHub account, you can link immediately and register for an account.

② After logging in, select “Add Projects” and add the repository of the application or site you want to automatically deploy this time. To add, click “Set Up Project” of the relevant repository.

③ Copy and paste the config.yml set above and register with “Add Manually”. At this time, the private key has not been registered yet. I will register later. image.png

④ Select “Start Building”. We will add the target repositories this time. After adding, I think there is an error because the private key and environment variables have not been set. I will add that information.

Private key registration

① Click “Project Settings” in the upper right of the dashboard “Pipelines”.

② Select “SSH Keys”. Since there is “Additional SSH Keys” at the bottom, I will register the local private key information here.

③ Click “ADD SSH Key”. For “Hostname”, enter the IP address of the deployment destination.

(4) To enter the private key in “Private Key”, enter the “.ssh” directory locally and use the ls key to confirm that the private key is present (id_rsa etc.). Let’s see this with cat. Copy and paste all the displayed information and paste it into “Private Key”. In this case, include “—–BEGIN RSA PRIVATE KEY—–” etc.

⑤ Click “Add SSH Key” and if you are able to pass successfully, the setting is completed. If an error occurs, it is possible that the private key is not registered in pem format or the private key information is a new format, resulting in a non-target error. The latter error occurred in my case.

I fixed the error referring to the following site. https://blog.adachin.me/archives/11554 I have summarized what I did after updating the private key to the old style. https://qiita.com/akk_ayy/items/61215e89cfcf680d1c94

⑥ Finally, copy the hashed fingerprints and paste them in the relevant part of config.yml.

Set environment variables

① Select “Environment Variables” under “Project Settings”.

② Select “Add Variable”. Register the destination port information, user name, and host name.

  • SSH_PORT
  • SSH_USER
  • SSH_HOST This completes setting environment variables.

③ Make sure that you can SSH login with the contents set locally.

ssh -p port user name @ host name -i ~/.ssh/ private key name

Now you can log in,

/var/www/repository name/deploy.sh

If you hit this and git pull can be executed, automatic deployment is ready!

Run auto deploy

Merge test data with mastar and check if automatic deployment is possible. At this time, if circleCI is SUCCESS, you are successful! Thank you for your hard work!

Reference site https://blog.adachin.me/archives/10997 https://qiita.com/tatane616/items/8624e61473a9957d9a81 https://www.tweeeety.blog/entry/2018/02/09/195345