Practical Examples of GitLab CI YML

Bits Lovers
Written by Bits Lovers on
Practical Examples of GitLab CI YML

In this tutorial, we’ll walk through a real project that needs a GitLab CI/CD pipeline. We’ll look at actual working examples and explain why gitlab ci yml examples matter in DevOps.

Picture this: you’re on a dev team building an app with multiple microservices in Python, Node.js, C++, Shell, Terraform, CloudFormation, Ansible, and Docker. To ship code without spending hours on manual builds and tests, you need a GitLab CI/CD pipeline that handles building, testing, and deployment for you.

Why gitlab ci yml examples matter

The gitlab-ci.yml file defines your pipeline’s workflows, stages, and jobs using YAML. It automates tasks like building code for testing or deploying to production. Good gitlab ci yml examples save you time when setting up pipelines for different projects.

10 GitLab CI/CD Examples

Here are ten examples showing how to use GitLab CI/CD with different technologies.

Example 1: Python Application Testing


stages:
  - test

test_python_app:
  stage: test
  image: python:3.8
  script:
    - pip install -r requirements.txt
    - pytest tests/

Example 2: Node.js Application Building and Unit Testing


stages:
  - build
  - test

build_nodejs_app:
  stage: build
  image: node:lts-alpine
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - dist/

test_nodejs_app:
  stage: test
  image: node:lts-alpine
  script:
    - npm install
    - npm run test

Example 3: C++ Application Compilation and Testing


stages:
  - build
  - test

build_cpp_app:
  stage: build
  image: gcc:latest
  script:
    - g++ --std=c++11 main.cpp -o app.out

test_cpp_app:
  stage: test
  image: gcc:latest
  script:
    - chmod +x test.sh
    - ./test.sh

Example 4: Shell Script Linting and Testing


stages:
  - lint
  - test

lint_shell_script:
  stage: lint
  image: koalaman/shellcheck-alpine:v0.6.0-stable
  script:
    - shellcheck my_script.sh

test_shell_script:
  stage: test
  before_script:
    - chmod +x my_script.sh
  script:
    - ./my_script.sh

Example 5: Terraform Init, Validate, and Apply Stages


stages:
  - init_terraform_backend
  - validate_terraform_files

init_terraform_backend:
  stage: init_terraform_backend
  image: hashicorp/terraform:$TERRAFORM_VERSION
  script:
    - terraform init

validate_terraform_files:
  stage: validate_terraform_files
  image: hashicorp/terraform:$TERRAFORM_VERSION
  script:
    - terraform validate

apply_changes_to_infrastructure:
  stage: deploy
  image: hashicorp/terraform:$TERRAFORM_VERSION
  script:
    - terraform apply -auto-approve
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

Example 6: AWS CloudFormation Template Validation and Deployment

Requires AWS CLI configured in your GitLab CI settings.


stages:
  - validate_cf_template
  - deploy_cf_stack

validate_cf_template:
  stage: validate_cf_template
  image: amazon/aws-cli:$AWS_CLI_VERSION
  script:
    - aws cloudformation validate-template --template-body file://$CI_PROJECT_DIR/cf-stack.yaml

deploy_cf_stack:
  stage: deploy_cf_stack
  image: amazon/aws-cli:$AWS_CLI_VERSION
  script:
    - aws cloudformation deploy --template-file $CI_PROJECT_DIR/cf-stack.yaml --stack-name GitLab-CI-Stack

Example 7: Ansible Playbook Linting and Execution


stages:
  - lint_ansible_playbook
  - execute_ansible_playbook

lint_ansible_playbook:
  stage: lint_ansible_playbook
  image: ansible/ansible-lint:devel
  script:
    - ansible-lint playbook.yml

execute_ansible_playbook:
  stage: execute_ansible_playbook
  image: williamyeh/ansible:ubuntu2204
  script:
    - ansible-playbook -i inventory.ini playbook.yml

Example 8: Docker Image Building and Pushing

Note: The docker:dind service handles Docker-in-Docker operations. Make sure your runner has Docker-in-Docker enabled.



stages:
  - build_docker_image
  - push_docker_image

variables:
  DOCKER_TLS_CERTDIR: "/certs"
  CONTAINER_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

build_docker_image:
  stage: build_docker_image
  image: docker:cli
  services:
    - docker:dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build --pull --no-cache --rm=true . --tag=$CONTAINER_IMAGE

push_docker_image:
  stage: push_docker_image
  image: docker:cli
  services:
    - docker:dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker push $CONTAINER_IMAGE

Example 9: Run Python Unit Tests and Upload Coverage to Codecov


stages:
  - test

test_python_app_and_coverage:
  stage: test
  image: python:3.8
  before_script:
    - pip install --upgrade pip
    - pip install pytest-cov codecov
  script:
    - pytest tests/ --cov=my_app --cov-report=xml
  after_script:
    - codecov

Example 10: Deploy Node.js Application using SSH and SCP

Requires SSH keys configured in your GitLab CI settings.



stages:
  - deploy_nodejs_app

deploy_nodejs_app:
  stage: deploy_nodejs_app
  image: node:lts-alpine
  before_script:
    - npm ci
    - npm run build
    - apk update && apk add openssh-client
  script:
    - echo "${SSH_PRIVATE_KEY}" | tr -d '\\r' > /root/.ssh/id_rsa
    - chmod 600 /root/.ssh/id_rsa
    - echo "${SSH_HOST_PUBLIC_KEY}" > /root/.ssh/known_hosts
    - scp -rp dist/* myuser@$SSH_HOST:~/app_directory

Wrapping up

These examples show how GitLab CI/CD pipelines work with Python, Node.js, C++, Shell, Terraform, CloudFormation, Ansible, and Docker. Use them as a starting point and adapt them to your own projects.

Bits Lovers

Bits Lovers

Professional writer and blogger. Focus on Cloud Computing.

Comments

comments powered by Disqus