CI/CD & Automation - gitlab-org/gitlab

GitLab’s CI/CD capabilities are a key feature of the platform, allowing for version control, automated builds, tests, and deployments. The foundation of GitLab CI/CD is the .gitlab-ci.yml file, which is a configuration file placed in the root of a repository. This file contains the CI/CD configuration, including jobs, stages, and dependencies.

Stages and Jobs

A CI/CD pipeline in GitLab is composed of stages and jobs. Stages are defined in the .gitlab-ci.yml file and represent a sequence of jobs that run in a specific order. Jobs are the individual tasks that run within a stage. For example, a typical pipeline might include stages for building, testing, and deploying an application.

Here’s an example of a simple .gitlab-ci.yml file that defines a build and test stage:

stages:
- build
- test

build-job:
stage: build
script: echo "Building the application"

test-job:
stage: test
script: echo "Testing the application"

GitLab Runner

GitLab Runner is the application that runs the scripts defined in the jobs. Runners can be installed on a local machine or in the cloud, and they can be shared among multiple projects or dedicated to a single project. To ensure that jobs run correctly, it’s important to have at least one available runner.

Docker Integration

GitLab CI/CD supports Docker integration in two primary ways: running CI/CD jobs in Docker containers and building Docker images. By using Docker containers for CI/CD jobs, you can ensure that the environment is consistent and reproducible. Building Docker images as part of the CI/CD pipeline allows for easy deployment and distribution of applications.

Here’s an example of a .gitlab-ci.yml file that uses Docker for building and testing a Node.js application:

image: node:14

stages:
- build
- test

build-job:
stage: build
script: echo "Building the application"

test-job:
stage: test
script: echo "Testing the application"

In this example, the image directive specifies that the Node.js image should be used for all jobs.

Automation with Ansible

GitLab CI/CD can also be integrated with Ansible for automating infrastructure and application deployment. By using Ansible Playbooks as part of the CI/CD pipeline, you can ensure consistent and reproducible deployments.

Here’s an example of a .gitlab-ci.yml file that uses Ansible for deploying an application:

stages:
- deploy

deploy-job:
stage: deploy
script: ansible-playbook deploy.yml

In this example, the deploy.yml Ansible Playbook is executed as part of the deploy stage.

Additional Resources

Sources: