Configuring a pipeline using multiple CircleCI orbs
Software Engineer
Continuous integration/continuous delivery (CI/CD) tools give developers the ability to automate the software development process. As soon as developers push code to git, your CI/CD system can build, test, stage, integration test, deploy, and scale. That’s fantastic!
In this tutorial, we will look at CircleCI orbs and how they can support your CI/CD practice. We’ll look at how to use multiple orbs and how orbs can help with multi-builds for a variety of application types.
Prerequisites
Before you start, make sure you have the following requirements in place:
A clone of the demo application is available on GitHub. Set-up and installation instructions are available on each project directory.
What are orbs?
Orbs are reusable YAML configuration packages that reduce the configuration to a few lines of code, allowing developers to package, ship, and reuse configurations easily.
Orbs are useful because they:
- Save time spent on project setup
- Boost organizational efficiency
- Simplify third-party integrations
Orbs can be the heroes of your config file.
To use an orb, go to the CircleCI orb registry. There you will find the right orb for your tech stack along with instructions to get started. If you cannot find an orb for your technology stack, you can create a specialized orb by following the CircleCI Best Practices and Getting Started guides.
Using multiple orbs
CircleCI supports using multiple orbs in the same configuration file - this section will show you how, using the project you cloned earlier.
The cloned project combines a Flask API with a Node.JS command-line application. We already have tests for both applications; they need to be run on CircleCI using the Python and the Node orbs.
Why multiple orbs?
Developers are adaptable in terms of which technology to employ and when. Most current applications are made up of APIs, with a frontend that plugs in to consume the API. Assume the API is built in Python. The developers decide to use a JavaScript framework like React for the frontend. We have a Python and JavaScript application in this context.
When configuring a CI/CD pipeline for this type of application, you should have all of your configurations in a single config file. That is because CircleCI requires a single config file in .circleci/config
.
Because orbs are flexible, we can combine orbs in a single config.yml
file to run our tests on CircleCI. Multiple orbs will result in faster project execution, a more robust CI/CD pipeline, and excellent reusability.
Setting up multiple orbs
The process of configuring multiple orbs is similar to that of configuring a single orb.
To begin, follow these steps:
- At the top of your
.circleci/config.yml
file, add the line below to specify the CircleCI versionversion: 2.1
- Invoke the orbs you wish to use. For example:
orbs: node: circleci/node@5.0.2 python: circleci/python@2.0.3
This code block lists the orbs right after the CircleCI version. We will then use the two throughout the script. Because the orbs are referenced using different names, they are entirely independent of one another.
- Introduce node elements into your existing workflows and jobs. At this point, the orb’s elements are available for use as
<orb-name>/<element>
.
Now that you have covered the fundamentals of configuring multiple orbs, you can start configuring your cloned app with the Python and Node orbs. These orbs are unique, and proper configuration can be found in the orbs documentation guide.
Here is a breakdown of the config file into several chunks containing the version
, orbs
, jobs
, and workflow
.
CircleCI version
version: 2.1
Orbs
These are the specific orb versions to use in your pipelines:
orbs:
node: circleci/node@5.0.2
python: circleci/python@2.0.3
Jobs
You can consider having two separate jobs for each application because you are testing two. Each orb will have its own execution job.
Job A
Role: Testing the Python application
Orb used: Python orb
jobs:
test-python-app:
executor: python/default
steps:
- checkout
- python/install-packages:
app-dir: ~/project/flask-api
pip-dependency-file: requirements.txt
pkg-manager: pip
- run:
command: |
pytest -v
name: Test
Job B
Role: Testing the JavaScript application
Orb used: Node orb
test-node-app:
executor: node/default
steps:
- checkout
- node/install-packages:
app-dir: ~/project/nodejs-cli
cache-path: ~/project/nodejs-cli/node_modules
override-ci-command: npm install
- run:
command: |
npm run test
working_directory: ~/project/nodejs-cli
Workflow
Workflows determine the order of execution of defined jobs.
workflows:
test:
jobs:
- test-python-app
- test-node-app
Comparing configs with and without multiple orbs
A complete, packed version of the config.yml
is available for reference in the sample GitHub repository. The typical configuration does not use CircleCI orbs.
When you review the configuration files, note the differences, and run each workflow with a different one. For example, run one using multiple orbs, and one using a standard config, without orbs.
Aside from the organization and readability orbs offer, reviewing the workflow run time reveals more benefits orbs provide. A successful run for two workflows is shown in the following screenshot: one with the standard configuration and the other with orbs.
This graphic shows that the workflow that runs with the config that uses multiple orbs executes slightly faster than the one that runs with the standard config. There are minor differences in the time it takes each job in both workflows to complete.
Orbs triumph once more!
Conclusion
This tutorial showed the benefits of using multiple orbs in your CI pipelines. You were able to configure a CI pipeline to use multiple orbs based on the architecture of different applications. You have experienced how multiple orbs can speed up workflow runs, increase reusability, and ease integration with third-party applications. Until next time, keep building!