This is an article that summarizes my knowledge when I started to learn about CircleCI. And a simple guide to use it.
What is CI?
CI stands for Continuous Integration which translates as “Continuous Integration”. This is considered an optimal method of software development, every time there is a change to the code, there will be a server that automatically builds, runs tests to check and ensures no errors occur during development. development. This also requires team members to constantly update their code to the repositories on a regular basis.
What is CD?
CD (Continuous Deployment) roughly translates as “Continuous Deployment” is a process of product or software release, code changes are automatically deployed on staging, production servers, along with running the test to ensure accuracy.
Introducing the CircleCI tool
CircleCI is a platform that helps us to apply CI / CD to projects easily. Just log in with a github account and your repos can connect to CircleCI.
The process of running a job on CircleCI is as follows:
- When the code is merged into a branch, CircleCI will automatically catch this event and run the corresponding job that was configured.
- CircleCI creates an environment on its server, pulls docker images, and runs steps to build the code against our config.
- After running all the steps, its job is done, you can see the success or failure status on Dashboard screen.
Next are some concepts when working with CIrcleCI.
configuration
Your entire installation with CircleCI is located in the .circleci/config.yml
file
1 2 3 | |-- .circleci | |-- config.yml |
For more details you can refer to https://circleci.com/docs/2.0/configuration-reference/
pipelines
A pipeline is all of your configuration as you work on a project. Can be seen through the CircleCI cloud. The pipeline includes your workflows, and the workflow includes jobs.
workflows
Responsible for arranging many jobs. For example a workflow to build -> run test -> deploy.
jobs
The steps are usually responsible for executing the steps is usually a series of statements.
Run it on a rails app
First, I create a new repository on github.
After creating, you can go to the projects section on CircleCI and you can see the project you just created.
Next, you can create a rails project, install rspec, rubocop … and push the code to repo. For convenience, I have to fork the circle’s demo project to see and it configs as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <span class="token key atrule">version</span> <span class="token punctuation">:</span> <span class="token number">2.1</span> <span class="token comment"># Các orb giống như các thư viện chưa các jobs có sẵn cho chúng ta sử dụng</span> <span class="token key atrule">orbs</span> <span class="token punctuation">:</span> <span class="token key atrule">ruby</span> <span class="token punctuation">:</span> circleci/ <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> <span class="token key atrule">node</span> <span class="token punctuation">:</span> circleci/ <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> <span class="token key atrule">jobs</span> <span class="token punctuation">:</span> <span class="token key atrule">build</span> <span class="token punctuation">:</span> <span class="token key atrule">docker</span> <span class="token punctuation">:</span> <span class="token comment"># docker image mà ta sử dụng</span> <span class="token punctuation">-</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> cimg/ruby <span class="token punctuation">:</span> 2.7 <span class="token punctuation">-</span> node <span class="token key atrule">steps</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> checkout <span class="token comment"># cài đặt gem, bundle</span> <span class="token punctuation">-</span> ruby/install <span class="token punctuation">-</span> deps <span class="token comment"># store bundle cache</span> <span class="token punctuation">-</span> <span class="token key atrule">node/install-packages</span> <span class="token punctuation">:</span> <span class="token key atrule">pkg-manager</span> <span class="token punctuation">:</span> yarn <span class="token key atrule">cache-key</span> <span class="token punctuation">:</span> <span class="token string">"yarn.lock"</span> <span class="token key atrule">test</span> <span class="token punctuation">:</span> <span class="token key atrule">parallelism</span> <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token key atrule">docker</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> cimg/ruby <span class="token punctuation">:</span> 2.7 <span class="token punctuation">-</span> node <span class="token punctuation">-</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> circleci/postgres <span class="token punctuation">:</span> 9.5 <span class="token punctuation">-</span> alpine <span class="token key atrule">environment</span> <span class="token punctuation">:</span> <span class="token key atrule">POSTGRES_USER</span> <span class="token punctuation">:</span> circleci <span class="token punctuation">-</span> demo <span class="token punctuation">-</span> ruby <span class="token key atrule">POSTGRES_DB</span> <span class="token punctuation">:</span> rails_blog_test <span class="token key atrule">POSTGRES_PASSWORD</span> <span class="token punctuation">:</span> <span class="token string">""</span> <span class="token key atrule">environment</span> <span class="token punctuation">:</span> <span class="token key atrule">BUNDLE_JOBS</span> <span class="token punctuation">:</span> <span class="token string">"3"</span> <span class="token key atrule">BUNDLE_RETRY</span> <span class="token punctuation">:</span> <span class="token string">"3"</span> <span class="token key atrule">PGHOST</span> <span class="token punctuation">:</span> 127.0.0.1 <span class="token key atrule">PGUSER</span> <span class="token punctuation">:</span> circleci <span class="token punctuation">-</span> demo <span class="token punctuation">-</span> ruby <span class="token key atrule">PGPASSWORD</span> <span class="token punctuation">:</span> <span class="token string">""</span> <span class="token key atrule">RAILS_ENV</span> <span class="token punctuation">:</span> test <span class="token key atrule">steps</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> checkout <span class="token punctuation">-</span> ruby/install <span class="token punctuation">-</span> deps <span class="token punctuation">-</span> <span class="token key atrule">node/install-packages</span> <span class="token punctuation">:</span> <span class="token key atrule">pkg-manager</span> <span class="token punctuation">:</span> yarn <span class="token key atrule">cache-key</span> <span class="token punctuation">:</span> <span class="token string">"yarn.lock"</span> <span class="token punctuation">-</span> <span class="token key atrule">run</span> <span class="token punctuation">:</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> Wait for DB <span class="token key atrule">command</span> <span class="token punctuation">:</span> dockerize <span class="token punctuation">-</span> wait tcp <span class="token punctuation">:</span> //localhost <span class="token punctuation">:</span> 5432 <span class="token punctuation">-</span> timeout 1m <span class="token punctuation">-</span> <span class="token key atrule">run</span> <span class="token punctuation">:</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> Database setup <span class="token key atrule">command</span> <span class="token punctuation">:</span> bundle exec rails db <span class="token punctuation">:</span> schema <span class="token punctuation">:</span> load <span class="token punctuation">-</span> <span class="token punctuation">-</span> trace <span class="token comment"># Run rspec in parallel</span> <span class="token punctuation">-</span> ruby/rspec <span class="token punctuation">-</span> test <span class="token punctuation">-</span> ruby/rubocop <span class="token punctuation">-</span> check <span class="token key atrule">workflows</span> <span class="token punctuation">:</span> <span class="token key atrule">version</span> <span class="token punctuation">:</span> <span class="token number">2</span> <span class="token key atrule">build_and_test</span> <span class="token punctuation">:</span> <span class="token key atrule">jobs</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> build <span class="token punctuation">-</span> <span class="token key atrule">test</span> <span class="token punctuation">:</span> <span class="token key atrule">requires</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> build |
The run commands are available in the orb here, but depending on the job you can also write your own commands.
After having the config file, every time we have any commit, CircleCI will automatically run our workflow, you can reinstall in Project Setting> Advanced.
…
The interface and how to use CircleCI at its basic level are quite simple and easy to use. Hopefully after my article, you can have more excitement to try this platform out.