Linux Programmer | RHCE | RHCSA

Search This Blog

Thursday, 10 July 2025

Gitlab_CICD_Sample Deployment

Gitlab CICD Stages:

build
- Create build of project.
- If it is docker project which contains dockerfile then, create docker image
- push into docker registry
 
test
- Test the created image is working fine or not
 
 deploy
- Deploy code on live server 
 

Keywords used in .gitlab-ci.yml

image
stages
script
before_script/after_script
tags
variables
cache
artifacts
only

 

Prerequisites:

Enable Gitlab runner:

Project -> settings -> CICD -> Runner -> Enable runner

Sample code:

# .gitlab-ci.yml

stages:
  - build
  - test
 
create_file:
  image: alpine:latest
  stage: build
  script:
   - echo "Building .."
   - mkdir build
   - touch build/somefile.txt
  artifacts:
    paths:
      - build/
 
test_file:
  image: alpine:latest
  stage: test
  script:
    - test -f build/somefile.txt

:

  • create_file, test_fileare the jobs.
  • If you not specify the image it will pull the default image alpine. 
  • Inside the container the folders and file will be created.
  • Container will be destroyed after one job is completed. That's why we have to use artifacts. So the build folder will be uploaded in artifacts of gitlab. 
  • Then you use in another job test. You can check the logs and you will find the log “ Downloading artifacts”.
  • If branch is protected then only all the variables will be supported in gitlab CI. 
  • Gitlab runner will use docker image to create build. Gitlab requires some workspace where it can execute build steps and from where it can deploy the application. 
  • Multiple jobs are there in gitlab-ci.yml 
  • Once one job is completed. The pulled image will be cleared. 
  • Build stage example with docker push. 

 

Now if you want to use variables in .gitlab-ci.yml then,

Project -> Settings -> CICD -> Variables
 
 

Sample build code for using variables inside gitlab ci and push image on docker registry 

image: docker:24.0.5
 
variables:
  DOCKER_HOST: tcp://docker:2375
  DOCKER_TLS_CERTDIR: ""
  DOCKER_DRIVER: overlay2
  QA_DEPLOY_PATH: /home/abc/docker-files/purval/frontend
  GIT_USERNAME: administrator
  GIT_PASSWORD: purval@123
  BRANCH: QA
  PROJECT_NAME: "purval_frontend"
  APP_IMAGE: docker.purval.app/purval/frontend-web:qa-$CI_COMMIT_SHORT_SHA

stages:
  - build
  - test
  - deploy
 
services:
- name: docker:24.0.5-dind
  alias: docker
 
build:
  stage: build
  script:
    - docker info
    - echo "$REGISTRY_TOKEN" | docker login -u "$REGISTRY_USER" --password-stdin "$REGISTRY_URL"
    - echo "Starting image build - qa-$CI_COMMIT_SHORT_SHA"
    - docker build -t $PROJECT_NAME:qa-$CI_COMMIT_SHORT_SHA .
    - echo "Setting up tag to image - $PROJECT_NAME:qa-$CI_COMMIT_SHORT_SHA"
    - docker tag $PROJECT_NAME:qa-$CI_COMMIT_SHORT_SHA $APP_IMAGE
    - echo "Push image to docker registry"
    - docker push $APP_IMAGE
 

Sample code for test stage: 

test:
  stage: test
  before_script:
    - apk add --no-cache curl
  script:
    - docker pull $APP_IMAGE
    - echo "Running container for testing"
    - docker run -e NODE_ENV=QA -d --name test_container -p 5300:5300 $APP_IMAGE
    - echo "Waiting for app to start...."
    - sleep 10
    - echo "Checking health endpoint...."
    - >
      if docker ps | grep -i 5300;
      then
        echo "✅ Test passed"
      else
        echo "❌ Test failed"
        docker logs test_container
        exit 1
      fi
    - docker stop test_container
    - docker rm test_container


Sample code for deploy stage:

deploy:
  stage: deploy
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - mkdir ~/.ssh
    - echo "$QA_SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - echo "QA_SSH_HOST - $QA_SSH_HOST"
    - ssh-keyscan -p $QA_SSH_PORT $QA_SSH_HOST >> ~/.ssh/known_hosts
  script:
    - |
      ssh -p "$QA_SSH_PORT" -i ~/.ssh/id_rsa "$QA_SSH_USER@$QA_SSH_HOST" bash -s <<EOF
      export QA_DEPLOY_PATH="$QA_DEPLOY_PATH"
      export APP_IMAGE="$APP_IMAGE"
      export GIT_USERNAME="$GIT_USERNAME"
      export GIT_PASSWORD="$GIT_PASSWORD"
      # export COMPOSE_PROJECT_NAME="$PROJECT_NAME"
 
      echo "✅ Connected to remote server: $(hostname)"
      echo "Running your deployment script..."
 
 
      if [ -d "$QA_DEPLOY_PATH" ]; then
        echo "✅ Directory exists: $QA_DEPLOY_PATH"
        cd "$QA_DEPLOY_PATH" &&
        echo "APP_IMAGE=$APP_IMAGE" > .env
 
      
        docker-compose down &&
        sleep 2 &&
        # docker-compose up -d --build &&
        docker-compose up -d &&
        echo "✅ Deployment completed."
      else
        echo "❌ Directory does not exist: $QA_DEPLOY_PATH"
        exit -1
      fi
      EOF
 
  only:
    - QA

 
Once you push this changes in gitlab. the runners will start running the pipeline.
Gitlab -> Project -> Build -> Pipeline
 
You will get an error in deploy stage. as we have not set the private key of gitlab.
 
Generate Private key of gitlab.
 
1. Login gitlab server: 
ssh-keygen -t rsa -b 4096 -C "gitlab-ci-deploy-key"
cat .ssh/gitlab-ci-deploy-key 
 
2. Copy that Private key and add into the Gitlab CICD variables,
SSH_PRIVATE_KEY 
 
3. Copy public key of gitlab and paste into the target servers authorized_keys.
cat .ssh/gitlab-ci-deploy-key.pub
 
Paste into target server: 
cat .ssh/authorized_keys 
  
Now again commit some changes and it will work.
 
Thank you. 

No comments:

Post a Comment

Gitlab_CICD_Sample Deployment

Gitlab CICD Stages: build - Create build of project. - If it is docker project which contains dockerfile then, create docker image - push in...