Practical Example of GitLab CI YML Examples

Practical Examples of GitLab CI YML

In this tutorial, we’ll explore a real-world project scenario that requires the creation of a GitLab Continuous Integration (CI) pipeline using GitLab CI/CD. We’ll provide practical examples and explain the importance of gitlab ci yml examples in your DevOps workflow.

Let’s say you are part of a development team at a small software company. Your team is working on an application that involves multiple microservices written in various programming languages and technologies like Python, NodeJs, C++, Shell Script, Terraform, CloudFormation, Ansible, and Docker. To ensure your code is production-ready with minimal issues, you must create a robust GitLab CI/CD pipeline to automate building, testing, and deploying your application.

Importance of gitlab ci yml examples:

A gitlab-ci.yml file is where you define your pipeline’s workflows, stages, and jobs in YAML format. It helps automate various tasks, such as building code changes for testing environments or seamlessly deploying applications to production environments. Having well-defined gitlab ci yml, examples can speed up the process of setting up pipelines for different projects with varying requirements.

10 Practical Examples for GitLab CI/CD

Now, let’s look at ten scenarios using various technologies that effectively demonstrate how to implement GitLab CI/CD pipelines.

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 & 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 & 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 :
   only :
     – master@/repository_name


Boost your skills in Cloud Computing

Are you interested in expanding your knowledge of cloud computing? As shown in this tutorial, AWS is an essential part of modern cloud development, and having a solid understanding of its tools and services can significantly improve your team’s productivity. To help you get started, we offer a free download of our AWS Learning Kit, which includes 20 mind maps and 260 questions with answers to help you master AWS. Don’t miss this opportunity to enhance your skills and take your cloud computing game to the next level! Download the AWS Learning Kit now.

Example 6: AWS CloudFormation Template Validation & Deployment (Requires AWS CLI Configured)


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:v4.3.0a0
  script:
    - ansible-lint playbook.yml

execute_ansible_playbook:
  stage: execute_ansible_playbook
  image: williamyeh/ansible:$ANSIBLE_VERSION-alpine3
	script:
	  - ansible-playbook -i inventory.ini playbook.yml

Example 8: Docker Image Building and Pushing



stages:
  - build_docker_image
  - push_docker_image

variables:
  CONTAINER_IMAGE: registry.gitlab.com/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME:$CI_COMMIT_SHORT_SHA

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

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


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


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-bash

Example 10: Deploy Node.js Application using SSH & SCP (Requires SSH Keys Setup)





stages :
  – deploy_nodejs_app

deploy_nodejs_app :
stage : deploy_nodejs_app ¶
image : node:lts-alpine3.12
script :
 - 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@:~/app_directory

Conclusion

These examples showcase the power and flexibility of GitLab CI/CD pipelines, covering various technologies such as Python, NodeJs, C++, Shell Script, Terraform, CloudFormation, Ansible, and Docker. Using these gitlab ci yml examples as a starting point for your own workflows or adapting them to suit your specific scenarios and requirements can significantly improve your team’s productivity and application quality.

Leave a Comment

Your email address will not be published. Required fields are marked *

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.