I aim to get a job at an in-house developed company from inexperienced. I decided to study CircleCI to create a good portfolio.
The current level of knowledge is that you can easily develop applications using Ruby on rails, version control using git, and deploy using heroku. I hope it will be of some help to those who are thinking of trying CircleCI in the future at the same level as their own memorandum.
Make automatic tests run every time you git push on CircleCI. Also make automated testing successful
【Related article】 [Challenge CircleCI from 0] Learn the basics of CircleCI [Challenge CircleCI from 0] Build an automated test (Rails6.0 / mysql8.0 / Rspec) [Challenge CircleCI from 0] Understand AWS ECR / ECS [Challenge CircleCI from 0] Automatically deploy with CircleCI / AWS (ECR / ECS)
ruby 2.6.6 rails 6.0 db: mysql 8.0 test: rspec
We will proceed on the assumption that the cooperation between github and CircleCI has been completed.
Basically, circleCI official or [[circleCI] Rails app to run rubycop and rspec test in cooperation with github If you refer to ru etc., you can easily set the automatic test. However, in order to perform automatic deployment and more complicated settings in the future, it is necessary to decompose and understand the description of the file. Let's look at the whole first.
yaml:.circleci/config.yml
version: 2.1
jobs: # ---------------------------------------------- ★ job
build:
docker: #----------------------------------------------★Executor
- image: circleci/ruby:2.6.6-node-browsers
environment:
RAILS_ENV: 'test'
MYSQL_HOST: 127.0.0.1
MYSQL_USERNAME: 'root'
MYSQL_PASSWORD: ''
MYSQL_PORT: 3306
- image: circleci/mysql:8.0
command: mysqld --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
MYSQL_ROOT_HOST: '%'
working_directory: ~/repo
steps: # --------------------------------------------- ★ Steps
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
- v1-dependencies-
- run:
name: install dependencies
command: |
bundle install --jobs=4 --retry=3 --path vendor/bundle
- save_cache:
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
- run: mv config/database.yml.ci config/database.yml
- run: bundle exec rake db:create
- run: bundle exec rake db:schema:load
- run:
name: RSpec
command: |
mkdir /tmp/test-results
TEST_FILES="$(circleci tests glob "spec/models/users_spec.rb" | \
circleci tests split --split-by=timings)"
bundle exec rspec \
--format progress \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
- store_test_results:
path: /tmp/test-results
- store_artifacts:
path: /tmp/test-results
destination: test-results
Aside from the detailed description,Previous article"Steps","Jobs"and"Executors"mentionedin(Executor)Let's organize about.
-Steps are where "steps" is written on line 18. This time, there is only one place where the step is declared, so there is only one step. "Steps" Below is a list of commands that all belong to one step. -Jobs are declared on the second line from the top where "jobs" is written. Since there is only one description here, there is only one job this time. Everything below "jobs" is the content of the job. By the way, the job name is "build"
In other words, in summary, "This file has one job called" build ", that job is executed in the docker environment, and the execution content is a step.(One)以下だよ」という意味になります。大まかな構造をみたので、次はOneOne細かく見ていきましょう。
yaml:.circleci/config.yml
version: 2.1
jobs:
build:
docker:
- image: circleci/ruby:2.6.6-node-browsers
environment:
RAILS_ENV: 'test'
MYSQL_HOST: 127.0.0.1
MYSQL_USERNAME: 'root'
MYSQL_PASSWORD: ''
MYSQL_PORT: 3306
- image: circleci/mysql:8.0
command: mysqld --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
MYSQL_ROOT_HOST: '%'
working_directory: ~/repo
-version is the version of CircleCI. 2 or 2 if you want to set from now on.I think 1 is fine. -jobs is a declaration that we are going to write the contents of the job. The build on the next line will be the name of the job. By the way, if there is only one job, the job name must be build, so be careful. -docker is "Executor(Executor)Declares to build a docker environment and execute jobs. -The first image is getting the image of ruby. "Circleci" in front of ruby/"" Refers to the image prepared by CircleCI, which is called a convenience image. -enviroment Below are the settings for the ruby image. "RAILS_ENV: 'test'Indicates that it is a test environment, and "MYSQL"HOST: 127.0.0."1" means connecting to MYSQL in the same local. "MYSQL_USERNAME "・" MYSQL ""PASSWORD" is the setting of MYSQL. This also needs to be included in the test config file. I'll talk about that file later. "MYSQL_PORT:"3306" is the MYSQL port number. -Next, there is another image, which is creating an image of MYSQL. The configuration is the same, and the environment settings of the image are set under environment. By the way, "command"~Is changing the authentication plugin for mysql. mysql8.Please note that it is indispensable when using o or later. "MYSQL_ALLOW_EMPTY_PASSWORD: 'true'Allows you to log in to MYSQL without a password. "MYSQL_ROOT_HOST: '%'Indicates that it is accessible from any host.
Let's look at the second half.
###② steps~Place
yaml:.circleci/config.yml
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
- v1-dependencies-
- run:
name: install dependencies
command: |
bundle install --jobs=4 --retry=3 --path vendor/bundle
- save_cache:
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
- run: mv config/database.yml.ci config/database.yml
- run: bundle exec rake db:create
- run: bundle exec rake db:schema:load
- run:
name: RSpec
command: |
mkdir /tmp/test-results
TEST_FILES="$(circleci tests glob "spec/models/users_spec.rb" | \
circleci tests split --split-by=timings)"
bundle exec rspec \
--format progress \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
- store_test_results:
path: /tmp/test-results
- store_artifacts:
path: /tmp/test-results
destination: test-results
-Declares to write a command list of steps in "steps".
###③database.yml.ci (file on host) As it often appears, let's also check the database configuration file on CircleCI.
config
test: &default
adapter: mysql2
encoding: utf8
pool: 5
username: <%= ENV.fetch("MYSQL_USERNAME") %>
password: <%= ENV.fetch("MYSQL_PASSWORD") %>
host: <%= ENV.fetch("MYSQL_HOST") %>
port: <%= ENV.fetch("MYSQL_PORT") %>
database: ci_test
This is the usual database.It's the same as a yml file, so I think it's almost unnecessary to explain. By the way, environment variables are used.
This is the end of the explanation of the configuration file. After that, every time you git push, the test will be run. The following is the test execution screen. Make sure everything is successful. (* For the second database from the top, there is no problem as long as false is not displayed. The database container will start normally and will be marked as finished)
I think that the automatic test completion mark will be displayed on the pull request of github as shown below.
#Summary / impression Since the CircleCI formula was competent, basically the formula solved most of the questions. It seems easy to introduce automated testing. However, it seems a little difficult for automatic deployment, so I would like to challenge while reviewing the knowledge of AWS and Docker.
#reference [Book] "Introduction to CircleCI Practice ──CI/Achieving both development speed and quality brought about by CD Masato Urai(Written by),Tomoya Otake(Written by),Gold Western country(Written by) 』
【qiita】 『It's a long time ago, but since I started CircleCI, I tried to summarize it in an easy-to-understand manner』 『[Circle CI] Introduced to Rails application (about setting file)』
Recommended Posts