Imagine a workflow where every code change is automatically built, tested, and deployed to your users. That's the power of Continuous Integration/Continuous Delivery (CI/CD)! CI/CD helps you catch bugs early, ensures code quality, and delivers software faster and more frequently.
What is CI/CD?
- Continuous Integration is a development practice where developers integrate code changes into a shared repository frequently, preferably several times a day. Each integration is then verified by an automated build and test process, allowing teams to detect problems early.
- Continuous Delivery extends CI by automating the release pipeline, ensuring that your code is always in a deployable state. You can deploy your application to various environments (e.g., staging, production) with a single click or automatically.
- Continuous Deployment takes it a step further by automatically deploying every successful build to production. This requires a high degree of confidence in your automated tests and deployment process.
Why GitLab CI/CD?
GitLab CI/CD is a powerful, integrated system that comes built-in with GitLab. It offers a seamless experience for automating your entire software development lifecycle. With GitLab CI/CD, you can:
- Automate everything: Build, test, and deploy your applications with ease.
- Catch bugs early: Detect and fix errors before they reach production.
- Get faster feedback: Receive immediate feedback on your code changes.
- Improve collaboration: Work together more effectively with automated workflows.
- Accelerate delivery: Release software faster and more frequently.
- Reduce risk: Minimize deployment errors and rollbacks.
The elements of GitLab CI/CD
.gitlab-ci.yml
: This YAML file, located in your project's root directory, defines your CI/CD pipeline, including stages, jobs, and runners.- GitLab Runner: This agent executes your CI/CD jobs on your infrastructure (e.g. physical machines, virtual machines, Docker containers, or Kubernetes clusters).
- Stages: Stages define the order of execution for your jobs (e.g. build, test, and deploy).
- Jobs: Jobs are individual units of work within a stage (e.g. compile code, run tests, and deploy to staging).
Setting up GitLab CI
Getting started with GitLab CI is simple. Here's a basic example of a .gitlab-ci.yml
file:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the application..."
test_job:
stage: test
script:
- echo "Running tests..."
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
environment:
name: production
This configuration defines three stages: "build," "test," and "deploy." Each stage contains a job that executes a simple script.
CI/CD configuration examples
Let's explore some more realistic examples.
Building and deploying a Node.js application
The pipeline definition below outlines using npm to build and test a Node.js application and dpl to deploy the application to Heroku. The deploy stage of the pipeline makes use of GitLab CI/CD variables, which allow developers to store sensitive information (e.g. credentials) and securely use them in CI/CD processes. In this example, an API key to deploy to Heroku is stored under the variable key name $HEROKU_API_KEY
used by the dpl tool.
stages:
- build
- test
- deploy
build:
stage: build
image: node:latest
script:
- npm install
- npm run build
test:
stage: test
image: node:latest
script:
- npm run test
deploy:
stage: deploy
image: ruby:latest
script:
- gem install dpl
- dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_API_KEY
Deploying to different environments (staging and production)
GitLab also offers the idea of Environments with CI/CD. This feature allows users to track deployments from CI/CD to infrastructure targets. In the example below, the pipeline adds stages with an environment property for a staging and production environment. While the deploy_staging stage will always run its script, the deploy_production stage requires manual approval to prevent accidental deployment to production.
stages:
- build
- test
- deploy_staging
- deploy_production
build:
# ...
test:
# ...
deploy_staging:
stage: deploy_staging
script:
- echo "Deploying to staging..."
environment:
name: staging
deploy_production:
stage: deploy_production
script:
- echo "Deploying to production..."
environment:
name: production
when: manual # Requires manual approval
GitLab Auto DevOps
GitLab Auto DevOps simplifies CI/CD by providing a pre-defined configuration that automatically builds, tests, and deploys your applications. It leverages best practices and industry standards to streamline your workflow.
To enable Auto DevOps:
- Go to your project's Settings > CI/CD > General pipelines.
- Enable the Auto DevOps option.
Auto DevOps automatically detects your project's language and framework and configures the necessary build, test, and deployment stages. You don’t even need to create a .gitlab-ci.yml
file.
CI/CD Catalog
The CI/CD Catalog is a list of projects with published CI/CD components you can use to extend your CI/CD workflow. Anyone can create a component project and add it to the CI/CD Catalog or contribute to an existing project to improve the available components. You can find published components in the CI/CD Catalog on GitLab.com.
CI templates
You can also create your own CI templates to standardize and reuse CI/CD configurations across multiple projects. This promotes consistency and reduces duplication.
To create a CI template:
- Create a
.gitlab-ci.yml
file in a dedicated project or repository. - Define your CI/CD configuration in the template.
- In your project's
.gitlab-ci.yml
file, use theinclude
keyword to include the template.
Take your development to the next level
GitLab CI/CD is a powerful tool that can transform your development workflow. By understanding the concepts of CI/CD, configuring your pipelines, and leveraging features like Auto DevOps, the CI/CD Catalog, and CI templates, you can automate your entire software development lifecycle and deliver high-quality software faster and more efficiently.
Want to take your learning to the next level? Sign up for GitLab University courses. Or you can get going right away with a free 60-day trial of GitLab Ultimate.
"Getting Started with GitLab" series
Check out more articles in our "Getting Started with GitLab" series: