I would like to organize the introduction of CircleCI
.
As a prior knowledge, there are many articles such as CircleCI Official Tutorial and other articles, so I think that it will not be a problem for learning, but this time I would like to look at the code as an arrangement of what I learned. I will.
[Addition] 2021-1-16 Click here for articles that support JavaScript!
CircleCI
is a Saas type CI/CD service.
Specifically, when you push it to the GitHub repository, the process of building, testing, and deploying the app will be executed automatically.
.circleci directory
and a config.yml
file in the root directory of the repository selected in your local environment.config/database.yml.ci
for testingconfig.yml
, go back to the CircleCI repository, click ** Use Existing Config, and select build **.I wrote it like this.
config:.circleci/config.yml
version: 2
jobs:
build:
docker:
- image: circleci/ruby:2.6.6-node-browsers #Match your Ruby version
environment: #Use environment variables
- BUNDLER_VERSION: 2.0.2 #Match your bundler version
- RAILS_ENV: 'test'
- image: circleci/mysql:8.0 #Match your version of MySQL
command: [--default-authentication-plugin=mysql_native_password]
environment:
- MYSQL_USER: root
- MYSQL_DB: ci_test
working_directory: ~/myapp
steps: #Steps to install in a container
- checkout #Start installation
- restore_cache: #Find a cache with a key that matches the key template
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
- v1-dependencies-
- run:
name: install dependencies
command: |
gem install bundler -v 2.0.2
bundle install --jobs=4 --retry=3 --path vendor/bundle
- save_cache: #Create cache
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
- run: mv config/database.yml.ci config/database.yml
- run: yarn install
#DB creation
- run: bundle exec rake db:create
- run: bundle exec rake db:schema:load
#Run the test
- run:
name: run tests
command: |
mkdir /tmp/test-results
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
circleci tests split --split-by=timings)"
sudo gem install bundler
sudo gem install rspec
sudo gem install rspec-core
bundle exec rspec \
--format progress \
--format RspecJunitFormatter \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
#Save test results
- store_test_results:
path: /tmp/test-results
- store_artifacts:
path: /tmp/test-results
destination: test-results
config:config/database.yml.ci
test:
adapter: mysql2
encoding: utf8
pool: 5
username: 'root'
port: 3306
host: '127.0.0.1'
database: ci_test
Authentication plugin 'caching_sha2_password' cannot be loaded
command: [--default-authentication-plugin = mysql_native_password]
Added this description.
Mysql2::Error: Unknown MySQL error This error is a MySQL not found error. I reviewed the settings, thinking that I used to go there locally.
If you don't take the ** consistency ** of the username and databasein
database.yml.ci and the MYSQL_USER and MYSQL_DB in
config.yml, you'll get an error like this:
It was solved by the image containing circleci/ruby: 2.6.6-node-browsers
node and run: yarn install
. By setting -tag
+ -node
, it seems that the state of the build container can be fixed and unexpected changes from upstream can be prevented. See Best Practices in the circleci image for more information.
** * This time, no tag is attached. ** (We are looking for the cause because an error will occur.)
--Watch out for indentation ――Do not push one or eight
This article was helpful. I was late to notice and messed up the repository a bit. ..
You can test grammar checks and jobs locally before pushing using the CircleCI CLI
. Please see the article for details.
――Proceed while understanding what you are doing
I haven't fully understood it yet, but it may be faster to understand ** if you think about what you are doing and how the file is executed. ..
--Introduction is relatively easy due to the large amount of information. ――However, it takes time to understand, so first move your hand to grasp the flow. --You also need to know Docker, so you need to review it again.
[Addition] 2021-1-16 Click here for articles that support JavaScript!
Recommended Posts