?? Ultimate Guide to Deployment with GitLab CI/CD ??
image from Appcircle

?? Ultimate Guide to Deployment with GitLab CI/CD ??

I'm thrilled to share a detailed GitLab CI/CD pipeline configuration designed for seamless updates. This setup not only ensures continuous delivery but also maintains high availability and zero downtime for applications. Below is a step-by-step guide through the configuration:

Name: Deployment 

stages:
  - deploy

variables:
  DOCKER_HUB_IMAGE: <name>/<name>

before_script:
  - echo "--- welcome to app-deployment: ---"
  - apt-get update && apt-get install -y jq

app-deployment:
  stage: deploy
  image: ubuntu:latest
  services:
    - docker:dind
  script:
    ### Step 1: Checkout Repository
    - echo "Checking out repository"
    - git clone https://$CI_REPOSITORY_URL .

    ### Step 2: Generate a Random Version Number
    - echo "Creating version number"
    - export VERSION_NUMBER=$(shuf -i 1000-9999 -n 1)
    - echo "VERSION_NUMBER=${VERSION_NUMBER}"

    ### Step 3: Build Docker Image
    - echo "Building Docker image"
    - docker build -t ${DOCKER_HUB_IMAGE}:${VERSION_NUMBER} .

    ### Step 4: Log into Docker Hub
    - echo "Logging into Docker Hub"
    - echo "$DOCKER_HUB_ACCESS_TOKEN" | docker login -u "$DOCKER_HUB_USERNAME" --password-stdin

    ### Step 5: Push Docker Image to Docker Hub
    - echo "Pushing Docker image"
    - docker push ${DOCKER_HUB_IMAGE}:${VERSION_NUMBER}

    ### Step 6: Install kubectl
    - echo "Installing kubectl"
    - curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
    - chmod +x ./kubectl
    - mv ./kubectl /usr/local/bin/kubectl

    ### Step 7: Set Up AWS Credentials
    - echo "Setting up AWS credentials"
    - mkdir -p ~/.aws
    - echo "[default]" > ~/.aws/credentials
    - echo "aws_access_key_id = $AWS_ACCESS_KEY" >> ~/..aws/credentials
    - echo "aws_secret_access_key = $AWS_SECRET_KEY" >> ~/..aws/credentials

    ### Step 8: Connect to the EKS Cluster
    - echo "Connecting to EKS cluster"
    - aws eks update-kubeconfig --region us-east-1 --name <name>

    ### Step 9: Install Helm
    - echo "Installing Helm"
    - curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

    ### Step 10: Use Helm to Install/Upgrade Charts
    - echo "Using Helm to install charts"
    - helm upgrade --install <name> <helm chart path> --set image.tag=${VERSION_NUMBER} --set replicaCount=2

    ### Step 11: Announce the Latest Version
    - echo "Announcing the latest version"
    - apt-get update && apt-get install -y ssmtp
    - echo "root=$EMAIL" >> /etc/ssmtp/ssmtp.conf
    - echo "mailhub=smtp.gmail.com:465" >> /etc/ssmtp/ssmtp.conf
    - echo "AuthUser=$EMAIL" >> /etc/ssmtp/ssmtp.conf
    - echo "AuthPass=$EMAIL" >> /etc/ssmtp/ssmtp.conf
    - echo "UseTLS=YES" >> /etc/ssmtp/ssmtp.conf
    - echo "UseSTARTTLS=YES" >> /etc/ssmtp/ssmtp.conf
    - echo "New Docker Image Version for Blogging Web App - app-deployment" | ssmtp -v <recipient emails>

  only:
    - master
        

Detailed Breakdown:

  1. Repository Checkout: The pipeline starts by cloning the repository to ensure the latest code is available.
  2. Version Number Creation: A unique version number is generated using a random number generator, ensuring each deployment is tagged distinctively.
  3. Docker Image Build: Next, the pipeline builds a Docker image from the repository code, preparing it for deployment.
  4. Docker Hub Authentication: Secure login to Docker Hub is achieved using access tokens.
  5. Docker Image Push: The newly built Docker image is then pushed to Docker Hub for storage and accessibility.
  6. kubectl Installation: The pipeline installs kubectl, the Kubernetes command-line tool, to interact with the Kubernetes cluster.
  7. AWS Credentials Setup: AWS credentials are configured to enable communication with AWS services, specifically EKS.
  8. EKS Cluster Connection: The pipeline connects to the Amazon EKS cluster to manage Kubernetes resources.
  9. Helm Installation: Helm, the package manager for Kubernetes, is installed for deploying Helm charts.
  10. Helm Chart Deployment: The pipeline upgrades or installs Helm charts, setting the image tag to the new version and adjusting the replica count for rolling updates.
  11. Deployment Notification: Finally, the pipeline sends an email notification announcing the new Docker image version, ensuring stakeholders are informed.


This CI/CD pipeline is designed to automate and streamline the deployment process, reducing downtime and ensuring efficient updates. Feel free to reach out if you have any questions or need further assistance with your CI/CD pipelines.

#DevOps #CICD #GitLab #Kubernetes #AWS #Docker #Helm #ContinuousDelivery #RollingUpdate

要查看或添加评论,请登录

Abatan Taiwo的更多文章

社区洞察

其他会员也浏览了