I tried to introduce CircleCI 2.0 to Rails app

Overview

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!

What is CircleCI

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.

Installation procedure

  1. Link CircleCI and GitHub
  2. Select the repository (app) currently created on CircleCI
  3. Create a .circleci directory and a config.yml file in the root directory of the repository selected in your local environment.
  4. Create config/database.yml.ci for testing
  5. After setting config.yml, go back to the CircleCI repository, click ** Use Existing Config, and select build **.
  6. Push to GitHub repository

CircleCI settings

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

Details

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 databaseindatabase.yml.ci and the MYSQL_USER and MYSQL_DB in config.yml, you'll get an error like this:

The test does not pass

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.)

important point

--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. ..

Summary

--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.

References

-CircleCI Official

[Addition] 2021-1-16 Click here for articles that support JavaScript!

Recommended Posts

I tried to introduce CircleCI 2.0 to Rails app
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Rails6 I tried to introduce Docker to an existing application
[Rails] I tried to create a mini app with FullCalendar
Introducing CircleCI to Rails
Introduce Vue.js to Rails
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to create a LINE clone app
I tried Rails beginner [Chapter 1]
I tried Rails beginner [Chapter 2]
Incorporate circleCI into CircleCI Rails app
I tried to verify yum-cron
I want to push an app made with Rails 6 to GitHub
I tried to introduce UI animation to Pokedex using Poké API
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails
[Rails] I tried to implement batch processing with Rake task
[Rails] I tried to implement "Like function" using rails and js
I tried to chew C # (indexer)
I tried to summarize iOS 14 support
Until you introduce fonts to Rails
I tried to interact with Java
I tried to explain the method
[Rails] I tried deleting the application
How to introduce jQuery in Rails 6
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to create a simple map app in Android Studio
I tried to implement Ajax processing of like function in Rails
I tried to sort the data in descending order, ascending order / Rails
I tried to implement the image preview function with Rails / jQuery
I tried to understand how the rails method "redirect_to" is defined
I tried to build a simple application using Dockder + Rails Scaffold
I tried to understand how the rails method "link_to" is defined
Rails Tutorial Extension: I tried to create an RSS feed function
I tried to make a group function (bulletin board) with Rails
I tried to summarize the methods used
How to change app name in rails
I tried migrating Processing to VS Code
I want to make an ios.android app
I tried to summarize Java lambda expressions
Introduce two-factor authentication to your Rails application
I tried to get started with WebAssembly
Introduced Vue.js to an existing Rails app
Upload Rails app image file to S3
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
Preparing to introduce jQuery to Ruby on Rails
Try deploying Rails app to EC2-Part 2 (Deploy)-
I tried to summarize the Stream API
[Rails 5.x] How to introduce free fonts
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx