How to build a CI/CD pipeline -- with examples (original) (raw)

Ready to set up a CI/CD pipeline? Follow these general steps, with guidance toward specific examples depending upon your own specific needs and resources.

Developers deliver value by writing software that solves a problem. Software development and delivery traverse various stages and steps until they reach the customers, and this delivery chain must be fast, safe and reliable.

These are the principles that govern continuous integration and continuous delivery or deployment. An effective continuous integration/continuous delivery (CI/CD) pipeline integrates automation tools and workflows an organization needs to build, compile, test and release its applications. Automate as many of these steps as possible to minimize manual effort and errors, and enhance the feedback loops throughout the software development lifecycle. Teams deliver smaller chunks of better-quality code releases in less time.

Every organization will set up a CI/CD pipeline differently based on its internal processes, resources and infrastructure. The specific components and tools in any CI/CD pipeline example depend on the team's particular needs and existing workflow. However, at a high level CI/CD pipelines tend to have a common composition.

What are the stages of a CI/CD pipeline

A CI/CD pipeline resembles the various stages software goes through in its lifecycle and mimics those stages:

Let's lay out the steps that occur in each stage that collectively create a CI/CD pipeline, with pointers to specific CI/CD pipeline examples.

Source stage: Implement a code repository and version control system

This initial CI/CD stage involves storing and managing source code in a repository with a version control system (VCS) that supports collaboration and tracking changes across a distributed team. The VCS can trigger a pipeline execution based on various events, such as a branch push or pull request validation. These pipeline runs also can be scheduled or initiated by a user.

Actions to perform in this stage:

  1. Select a repository and VCS, such as Git or SVN.
  2. Determine if you'll host the VCS yourself or use a provider such as GitHub, Bitbucket, Azure DevOps and others.
  3. Create repositories to house the application source code along with the pipelines.

Build stage: Choose a CI engine, compile code, run checks

The key to this CI/CD stage is to provide early feedback to developers and maintain the application in a state where it can be released to an environment.

It is imperative to provide feedback as soon as a developer checks in new code changes to flag and resolve any issues such as syntax or compilation. Also, compilation ensures that the code can successfully generate artifacts for eventual release into environments further down the CI/CD pipeline.

Actions to perform in this stage:

  1. Determine the build/CI server to use, whether self-hosted such as Jenkins or Jenkins X, or a third-party Jenkins alternative such as GitHub Actions, CircleCI or Azure Pipelines.
  2. Ideally, set up a pipeline-as-code This can be stored in version control with appropriate triggers identified to release software, such as branch pushes and pull requests.
  3. Implement a stage/job in the pipeline that compiles (builds) the application source code. For modern cloud-native applications, this step can generate a Docker image.
  4. Run static analysis and style checks to ensure there are no "code smells" – indications the existence of deeper problems to explore -- and that the coding style is consistent with organizational guidelines. A CI engine may support various plugins to perform these static analyses and generate warnings.
  5. The pipeline then generates a versioned and built (compiled) artifact or a container image. Publish this build artifact to a store or feed (or container image to a registry), from where it is readily available for testing or deployment.

Required tasks and tool options for each stage of a CI/CD pipeline.

Each stage of a typical CI/CD pipeline has multiple tasks and multiple kinds of tools to accomplish them.

Testing stage: Test the build, publish results, release for production

The goal of this stage is to ensure that the changes do not break any logic or functionality and that the code is safe to release. In short, testing provides a safety net to release the code. Among the many tests that occur in this stage are unit, integration and functional tests.

Unit tests examine small units of application code and only test the logic in isolation without any dependencies, although if required, you can simulate them through mocking. For code developed in object-oriented languages such as Java or C#, etc., these small units can be class methods.

Integration tests analyze individual units of code together in a group. This builds confidence that individual modules integrated to build the entire application won't break under test.

An end-to-end functional test introduces the software into an environment to mimic a production deployment. This step is often automated with tools such as Selenium.

Actions to perform in this stage:

  1. Choose from a vast ecosystem of plugins, such as xUnit or JUnit, that enable integration of the test runner of choice within the pipeline.
  2. Publish the test report and code coverage reports so that the results of the testing are easily available in the pipeline run.
  3. Maintain a predetermined benchmark of code coverage to release a software build. If the code coverage drops below a certain threshold, fail the stage.

Deploy stage: Deliver and deploy the final build

Once the software is built and tested and artifacts are generated, it is ready to be released into an environment. Ideally, there are multiple environments through which the built artifacts are released and then tested, and if the release passes all the tests it progresses through to production deployment. This stage also handles adding the required resources to host the application in the cloud.

Actions to perform in this stage:

  1. Choose an application deployment strategy for the final release to the production environment. Common methods are blue-green, canary and rolling deployments. If the chosen stack for deployment involves containers, deployment should also involve orchestration platforms such as Kubernetes or OpenShift.
  2. If you deploy the application to the cloud, utilize the chosen cloud provider's infrastructure as code (IaC) option, such as Terraform, AWS CloudFormation templates or Azure Resource Manager templates. Whichever IaC path you choose, ensure that it supports idempotent deployments.
  3. CI/CD deployments to the cloud might require you to automate configuration inside the operating system. Look into configuration management tools such as Chef, Puppet and Ansible.
  4. Consider integrating tools that together can perform both cloud resource deployments and application release strategies, such as Jenkins and Spinnaker. Using a tool targeted for continuous delivery frees the team from managing ad hoc scripts to deploy the infrastructure and application.

CI/CD pipeline principles and best practices

There are no ironclad rules that define the best ways to create CI/CD pipelines. However, there are certain guidelines to follow while building these pipelines.

Overall considerations to build a CI/CD pipeline

CI/CD pipelines aim to streamline software development and delivery, but real-world implementations can widely differ from the theoretical concepts. Organizations have specific problems to tackle, resources to draw upon and technology decisions to weigh.

Consider investments in time and resources to manage the infrastructure that supports CI/CD pipelines. On-premises repos and version control such as Git, along with build servers such as Jenkins, require a lot of effort to patch and maintain.

If a team chooses to run the CI/CD pipelines with hosted providers, such as GitHub and Azure DevOps, there are additional considerations. A CI/CD pipeline in the cloud typically deploys the application as a hosted workload on that cloud's platform and will require the team to assign underlying infrastructure resources (IaaS, PaaS or SaaS).

There are also many security questions to answer with CI/CD in the cloud. How do you authenticate users and grant them appropriate access to resources? How does the provider store credentials or service connection strings to access outside resources? How do CI/CD pipelines with a cloud provider access internal resources if required? Many providers allow organizations to run the hosted agent in their internal network, but this requires outbound connections to allow egress traffic, which may require a security review and exception.

There is no one-size-fits-all answer to implementing a CI/CD pipeline. However, there are common attributes and CI/CD best practices that can help you design, implement and continuously improve your software delivery process. Start with very basic pipelines and then continuously gather feedback and improve them over time.

Next Steps

Try this AWS Proton tutorial to build templates quickly

Follow this Harness IO tutorial to get started with CI/CD

Identify and fix broken builds with CI/CD pipelines

How to implement CI/CD in network automation

Dig Deeper on Systems automation and orchestration