--Automatic testing with Rubocop and RSpec on CircleCI --Building a CI / CD pipeline using CircleCI orbs
--CircleCI initial settings --How to install Rubocop and RSpec
I refer to the following official page https://circleci.com/docs/ja/2.0/configuration-reference/#requires
version: 2.1
orbs:
aws-ecr: circleci/[email protected]
aws-ecs: circleci/[email protected]
references:
defaults: &defaults
working_directory: ~/myqpp
ruby-docker-image: &ruby-docker-image
#steps Common primary container
image: circleci/ruby:2.6.4-node-browsers
#Environment variables of the primary container
environment:
RAILS_ENV: test
MYSQL_HOST: 127.0.0.1
MYSQL_USERNAME: "db_name"
MYSQL_PASSWORD: ""
MYSQL_PORT: 3306
mysql-docker-image: &mysql-docker-image
image: circleci/mysql:8.0.19
command: [--default-authentication-plugin=mysql_native_password]
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_ROOT_HOST: "%"
jobs:
checkout_code:
<<: *defaults
docker:
- *ruby-docker-image
- *mysql-docker-image
steps:
- checkout
- save_cache:
key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
paths:
- ~/project
test:
<<: *defaults
docker:
- *ruby-docker-image
- *mysql-docker-image
steps:
#Check out the source code to working dir
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
- v1-dependencies-
#ruby dependency
- run: gem install bundler -v 2.1.4
- run: bundle install --jobs=4 --retry=3 --path vendor/bundle
- save_cache:
key: vi-dependencies-{{ checksum "Gemfile.lock" }}
paths:
- ./vendor/bundle
#DB wait/setup
- run: mv -f ./config/database.yml.ci ./config/database.yml
- run: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s
- run: bundle exec rails db:create db:schema:load --trace
#Rubocop automation
- run:
name: start RuboCop
command: bundle exec rubocop
#RSpec automation
- run:
name: start RSpec
command: |
mkdir /tmp/test-reports
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
circleci tests split --split-by=timings)"
bundle exec rspec \
--format progress \
--format RspecJunitFormatter \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
#Save reports and results
- store_artifacts:
path: /tmp/test-reports
destination: tr1
- store_test_results:
path: /tmp/test-reports
workflows:
version: 2.1
test_code:
jobs:
- checkout_code
- test:
requires:
- checkout_code
#Deploy Rails using ECR and ECS
build-and-deploy-rails:
jobs:
- aws-ecr/build-and-push-image:
filters:
branches:
only: master
account-url: AWS_ECR_ACCOUNT_URL
region: AWS_REGION
aws-access-key-id: AWS_ACCESS_KEY_ID
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
extra-build-args: "--build-arg RAILS_MASTER_KEY=${RAILS_MASTER_KEY}"
create-repo: true
dockerfile: ./Dockerfile
repo: "${AWS_RESOURCE_NAME_PREFIX_WEB}"
tag: "${CIRCLE_SHA1}"
- aws-ecs/deploy-service-update:
requires:
- aws-ecr/build-and-push-image
family: "${ECS_TASK}"
cluster-name: "${ECS_ARN}"
service-name: "${ECS_SERVICE}"
container-image-name-updates: "container=${AWS_RESOURCE_NAME_PREFIX_WEB},tag=${CIRCLE_SHA1}"
#deploy nginx
build-and-deploy-nginx:
jobs:
- aws-ecr/build-and-push-image:
filters:
branches:
only: master
account-url: AWS_ECR_ACCOUNT_URL
region: AWS_REGION
aws-access-key-id: AWS_ACCESS_KEY_ID
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
create-repo: true
dockerfile: ./nginx/Dockerfile
repo: "${AWS_RESOURCE_NAME_PREFIX_NGINX}"
tag: "${CIRCLE_SHA1}"
- aws-ecs/deploy-service-update:
requires:
- aws-ecr/build-and-push-image
family: "${ECS_TASK}"
cluster-name: "${ECS_ARN}"
service-name: "${ECS_SERVICE}"
container-image-name-updates: "container=${AWS_RESOURCE_NAME_PREFIX_NGINX},tag=${CIRCLE_SHA1}"
Two execution processes are set in jobs, and parallel execution of two jobs is defined in workflow.
The following official references are useful. https://circleci.com/docs/ja/2.0/language-ruby/
At this time, you need to be careful if you are using MySQL 8 or above. It seems that the authentication method has changed since 8.0, so add the following to handle it.
image: circleci/mysql:8.0.19
command: [--default-authentication-plugin=mysql_native_password] #add to
You can deploy to ECS by reading the official reference below https://circleci.com/docs/ja/2.0/ecs-ecr/
What you should be careful about here is to make sure that the environment variables set in CircleCI correspond firmly. You can build a CI / CD pipeline as long as the environment variables are set correctly.
The parts that are used repeatedly are refactored to improve readability.
&(anchor)
Defined in
*(alias)
Execute what is defined in
Install CircleCI CLI from the formula below. https://circleci.com/docs/ja/2.0/local-cli/
(Mac/For Linux)
curl -fLSs https://circle.ci/cli | bash
You can check if the configuration file is valid with this CircleCI CLI.
$ circleci config validate
CircleCI also has a Japanese document, how to write a configuration file, Furthermore, there is a thorough description of test execution settings such as RSpec.
I wrote this article while feeling the gratitude for the Japanese documentation.
https://patorash.hatenablog.com/entry/2018/06/24/122210
https://circleci.com/docs/ja/2.0/configuration-reference/#requires
https://circleci.com/docs/ja/2.0/workflows/
Recommended Posts