CI / CD is a pair of jobs that involve automating repetitive tasks by developers, including CI (Continuous Integration) and CD (Continuous Delivery), which means integration. more often, faster when code and regularly updated new versions (delivery).
CircleCI is a product that implements both CI / CD, in particular it builds, tests the scripts you install and tests (unit tests) for example and automatically deploys the application – Continuous Deploy.
CircleCI automates your software builds, tests, and deployments.
CircleCI runs nearly 1 million jobs every day, supporting 30,000 organizations. These organizations choose CircloCI because jobs run fast and application builds can be optimized for speed. CircleCI can be configured to run multiple complex pipelines effectively.
For a developer, you can also use SSH into any joib to debug build issues, install jobs in parallel to run faster, and config caching with a few simple keywords to reuse data from. previous jobs via workflow.
For administrators and administrators, using CircleCI installed on a shared server, CircleCI provides an intuitive interface for monitoring your build.
The document provides step-by-step instructions for you to build your first success on Circle 2.0
0. Required before first build build
- Basic knowledge of Git, with GitHub.com account, GitHub repository
- Have a basic knowledge of
terminal
orbash
to use the command line effectively
1. Createting a Repository
Step 1: Select repositories and click New or go directly to this page https://github.com/new
Step 2: Select Initialize this repository with a README and click Create repository button:
2. Add a .yml
file
Circle uses 1file YAML to determine how you want to test the environment and test what you want to run. In Circle 2.0 , this file must be called config.yml
and must be in the hidden .circleci
folder. On Mac, Linux and Windows, the names of files and folders starting with a dot will be pressed according to the default settings.
1 2 3 | <span class="token function">mkdir</span> .circleci <span class="token function">touch</span> .circleci/config.yml |
And the content of config.yml
file
1 2 3 4 5 6 7 8 9 | version: 2 jobs: build: docker: - image: circleci/ruby:2.4.1 steps: - checkout - run: echo "A first hello" |
Then commit and push to the repo as usual
1 2 3 4 | <span class="token function">git</span> add .circleci/config.yml <span class="token function">git</span> commit -m <span class="token string">"Create circle-ci config"</span> <span class="token function">git</span> push origin master |
Or you can create with skins
3. Setting up Your Build on CircleCI
- Step 1: Before performing this step you need to create a CircleCI account. Then go to the Dashboard page.
- Step 2: Next you will get the option to list the projects you can access and build CircleCI. On the next screen, you can add the repo you created as a new project on CircleCI
- Step 3: To add your new repo, click Set Up project
- Step 4: On the next screen, you will get several configuration options for your project on CircleCI. Choose the right one and click Start building
4. Running Your First CircleCI Build!
You should watch the build run automatically and pass! So, what’s going on? Click on the blue Success button on the Dashboard to review the runs.
- Spin up environment : CircleCI used circleci / ruby: 2.4.1. Docker image to launch a virtual computing environment.
- Checkout code : CircleCI checked out your GitHub repository and “cloned” into the virtual environment launched in Step 1.
- echo : Here are the other commands in the
config.yml
file: CircleCI runs the echo command with inpuit “A first hello”. Although there is actually no source code on your repo and actually not configured the tests in the config.yml file, CircleCI still considers your build to be “successful” because all steps have been successful (returned). 0). In fact, many projects are much more complex, usually there will be multiple Docker images and many steps, including a large number of tests.
5. Breaking Your Build!
Edit the config.yml file on GitHub and create a new commit to make a new build. If the build fails, there will be a red Failed button and CircleCI will send you an email to notify the error.
6. Using the Workflows Functionality
The next tutorial will show you how to use workflow, a strength of CircleCI to build jobs in parallel or need to depend on each other.
- Step 1: To view the workflow, edit the
.circleci/config.yml
file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | version: 2 jobs: build: docker: - image: circleci/ruby:2.4.1 steps: - checkout - run: echo "A first hello" build: docker: - image: circleci/ruby:2.4.1 steps: - checkout - run: echo "A first hello" |
- Step 2: Next, rename the 2 jobs to a different name. In this example we will name them one and two . Change the
echo
content differently too. To have time between csac last build, we can add thesleep
command. - Step 3: Add the
workflows
section to theconfig.yml
file. This part can be placed anywhere in the file. But it is usually placed at the beginning or the end of the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | version: 2 jobs: one: docker: - image: circleci/ruby:2.4.1 steps: - checkout - run: echo "A first hello" - run: sleep 25 two: docker: - image: circleci/ruby:2.4.1 steps: - checkout - run: echo "A more familiar hi" - run: sleep 15 workflows: version: 2 one_and_two: jobs: - one - two |
- Step 4: Commit this change, push it to your repository and look again at the CircleCI dashboard
- Step 5: Click the link to see the
workflow
2 jobs running in parallel
See more workflow at: https://circleci.com/docs/2.0/workflows/#overview
7. Adding Some Changes to use the Workspaces Functionality
Each workflow needs a workspace link, which can be used to move files from the top jobs to the workflow they’re processing. You can use the workflow to do this.
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 | version: 2 jobs: one: docker: - image: circleci/ruby:2.4.1 steps: - checkout - run: echo "A first hello" - run: mkdir -p my_workspace - run: echo "Trying out workspaces" > my_workspace/echo-output - persist_to_workspace: # Must be an absolute path, or relative path from working_directory root: my_workspace # Must be relative path from root paths: - echo-output two: docker: - image: circleci/ruby:2.4.1 steps: - checkout - run: echo "A more familiar hi" - attach_workspace: # Must be absolute path or relative path from working_directory at: my_workspace - run: | if [[ $(cat my_workspace/echo-output) == "Trying out workspaces" ]]; then echo "It worked!"; else echo "Nope!"; exit 1 fi workflows: version: 2 one_and_two: jobs: - one - two: requires: - one |
8. SSH into Your Build
If you are more comfortable with the terminal, you can use SSH directly to the CircleCI jobs to handle the problem with builds by running the build again with the SSH enabled option.
Using this method is also very convenient for you to debug your jobs, great is not it.
Note that you will need to add your SSH keys to your GitHub account: https://help.github.com/articles/connecting-to-github-with-ssh/ .
OK then, you can copy the generated shh command and access it
1 2 | ssh -p 64539 54.146.195.254 |
So you are executing this job as a command line. You can use the commands in the workspace only:
1 2 3 4 5 | pwd # print what directory, find out where you are in the file system ls -al # list what files and directories are in the current directory cd <directory_name> # change directory to the <directory_name> directory cat <file_name> # show me the contents of the file <file_name> |
summary
- To integrate CircleCI application, simply, create the configuration file
.circleci/config.yml
and the configuration on Dashboard is https://circleci.com/dashboard - If you just run simple jobs like check 1 convention, unit tests, without setting too much content will be fairly simple files, including
run
ofsteps
are being
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | version: 2.1 jobs: build: docker: - image: circleci/php:7.2-node-browsers steps: - checkout - run: name: Install Composer command: | php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer invalid'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');" - run: name: Running PHPCS command: | cd ./vendor/squizlabs/php_codesniffer/src/Standards git clone <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> :wataridori/framgia-php-codesniffer.git Framgia cd ../../../../../ ./vendor/bin/phpcs -n |
- If there are more jobs, consider using
jobs
andworkflows
(for example, you just want to check tests and want to deploy, for example). - Use SSH to debug jobs, access the container to test.
- The commands in CircleCI can completely refer to environment variables, allowing you to work more extensively!