What language does my team use to manage the CI/CD pipeline?

147    Asked by Diyatomar in Devops , Asked on Jul 1, 2024

What language should my team use to define and manage CI/CD pipelines and how it can impact the flexibility and also the scalability of my DevOps processing? 

Answered by David WHITE

In the context of DevOps, your team can use the YAML to define and also manage the CI/CD pipelines. The readability and ease of use make it real for the configuring pipeline in tools such as Jenkins, GitHub actions, and Azure DevOps.

Here is an example given below of typical GitHub actions CI/CD pipeline might look like this:

Name: CI/CD Pipeline

On:

  Push:

    Branches: [ “main” ]

  Pull_request:

    Branches: [ “main” ]

Jobs:

  Build:

    Runs-on: ubuntu-latest

    Steps:

Name: Checkout code

      Uses: actions/checkout@v2

Name: Set up Node.js

      Uses: actions/setup-node@v2

      With:

        Node-version: ‘14’

Name: Install dependencies

      Run: npm install

Name: Run tests

      Run: npm test

Name: Build project

      Run: npm run build

Name: Deploy to production

      Run: npm run deploy

      If: github.ref == ‘refs/heads/main’

By using the YAML, you can easily achieve flexibility in configuring different stages such as building, testing, and deploying. It can easily scale our pipeline to accommodate new workflow and Integration. Additionally, the structure of YAML can provide you with help in maintaining the pipeline Configuration across various environments.

Here is a Python based script which would define and manage a CI/CD pipeline a hypothetical DevOps framework. This script can demonstrate various stages such as the checkout, build, testing and deploying, integrating with a git repository, docker for containerization, and AWS for deployment.

Import os

Import subprocess

Import boto3

From datetime import datetime

# Configuration settings

REPO_URL = ‘https://github.com/your-repo/project.git’

DOCKER_IMAGE_NAME = ‘your-docker-image’

AWS_REGION = ‘us-west-2’

ECR_REPOSITORY = ‘your-ecr-repo’

EC2_INSTANCE_ID = ‘i-0abcdef1234567890’

DEPLOYMENT_SCRIPT = ‘/path/to/deployment_script.sh’

# Utility functions

Def run_command(command):

    Try:

        Subprocess.run(command, check=True, shell=True)

    Except subprocess.CalledProcessError as e:

        Print(f”Error: {e}”)

        Exit(1)

Def log(message):

    Print(f”{datetime.now()}: {message}”)

# Pipeline stages

Def checkout_code():

    Log(“Checking out code from repository”)

    Run_command(f’git clone {REPO_URL} project’)

    Os.chdir(‘project’)

Def build_docker_image():

    Log(“Building Docker image”)

    Run_command(f’docker build -t {DOCKER_IMAGE_NAME} .’)

Def run_tests():

    Log(“Running tests”)

    Run_command(‘pytest tests’)

Def tag_and_push_docker_image():

    Log(“Tagging and pushing Docker image to AWS ECR”)

    Ecr_client = boto3.client(‘ecr’, region_name=AWS_REGION)

    Auth_data = ecr_client.get_authorization_token()[‘authorizationData’][0]

    Auth_token = auth_data[‘authorizationToken’]

    Ecr_url = auth_data[‘proxyEndpoint’]

    Run_command(f’echo {auth_token} | docker login –username AWS –password-stdin {ecr_url}’)

    Ecr_image_tag = f’{ecr_url}/{ECR_REPOSITORY}:{datetime.now().strftime(“%Y%m%d%H%M%S”)}’

    Run_command(f’docker tag {DOCKER_IMAGE_NAME} {ecr_image_tag}’)

    Run_command(f’docker push {ecr_image_tag}’)

    Return ecr_image_tag

Def deploy_to_ec2(ecr_image_tag):

    Log(“Deploying Docker image to EC2 instance”)

    Ec2_client = boto3.client(‘ec2’, region_name=AWS_REGION)

    Ssm_client = boto3.client(‘ssm’, region_name=AWS_REGION)

    Command = f’#!/bin/bash
sudo docker pull {ecr_image_tag}
sudo docker run -d {ecr_image_tag}’

    Ssm_client.send_command(

        InstanceIds=[EC2_INSTANCE_ID],

        DocumentName=’AWS-RunShellScript’,

        Parameters={‘commands’: [command]}

    )

Def main():

    Checkout_code()

    Build_docker_image()

    Run_tests()

    Ecr_image_tag = tag_and_push_docker_image()

    Deploy_to_ec2(ecr_image_tag)

If __name__ == “__main__”:

    Main()



Your Answer

Interviews

Parent Categories