CI/CD Pipeline

2022-07-14

CI/CD stands for Continuous Integration and Continuous Deployment (or Continuous Delivery). It is a practice that automates the process of building, testing, and deploying code. I have been using CI/CD pipelines at work and it has changed the way we deliver software.

Continuous Integration

Continuous Integration is the practice of merging code changes into the main branch frequently, usually multiple times a day. Every time a developer pushes their code, the CI system automatically runs a series of steps: it pulls the latest code, installs dependencies, builds the project, and runs the tests. If any step fails, the team gets notified immediately.

Before we had CI, developers would work on their branches for days or even weeks, and then when they tried to merge, there would be a lot of conflicts and broken code. With CI, because we merge small changes often, the conflicts are smaller and easier to resolve.

Continuous Deployment

Continuous Deployment takes it a step further. After the CI process passes all the checks, the code is automatically deployed to the target environment. This means that every change that passes the tests goes straight to production without manual intervention.

Some teams prefer Continuous Delivery instead, which is similar but requires a manual approval step before deploying to production. The code is always in a deployable state, but someone has to click a button to actually deploy it.

Pipeline Stages

A typical CI/CD pipeline has several stages:

Build compiles the code and resolves dependencies. For a JavaScript project, this would be running npm install and npm run build.

Test runs the automated tests. This can include unit tests, integration tests, and end-to-end tests. If any test fails, the pipeline stops and the team is notified.

Code Quality checks for linting errors, code coverage thresholds, and other quality metrics. Some teams also run security scanning at this stage.

Deploy pushes the code to the target environment. This could be dev, UAT, or production depending on the branch and the pipeline configuration.

Tools

There are many tools for setting up CI/CD pipelines. At work, I have used Azure DevOps Pipelines, which integrates well with the rest of the Azure ecosystem. Other popular options are GitHub Actions, GitLab CI, and Jenkins.

In Azure DevOps, we define the pipeline in a YAML file. We specify the trigger (which branch should trigger the pipeline), the pool (what machine to run on), and the steps (what commands to execute). It also supports approval gates, where certain stages require manual approval before proceeding.

Setting up a CI/CD pipeline takes some initial effort, but once it is in place, it saves a lot of time and reduces the risk of human errors during deployment.