Tuesday, June 9, 2026

How to integrate SonarQube with GitLab CICD Pipeline | SonarQube Integration with GitLab CICD | Automate Code Scan using SonarQube In GitLab CICD

 Please find steps for integrating SonarQube with GitLab CICD



Pre-requisites:

How to integrate SonarQube with GitLab CICD:
We will be following below steps:
  • Create Token in SonarQube to authenticate with GitLab
  • Add Sonar Token, SonarQube URL as Secrets in GitLab
  • Create GitLab CICD yaml
  • Add tasks for Maven build and Sonar Scan
  • Verify scan report in SonarQube

Create Token in SonarQube to authenticate with GitLab
You need to login to SonarQube using your admin password and click on Admin on your top side.
Click on My Account, Security. 
Under Tokens, Give some value for token name and choose global analysis token, click on generate Tokens. Copy the token value generated.


Add Sonar Token and Sonar Host URLs as Secret in GitLab
Go to your GitLab Repo --> Click on Settings --> CICD --> Variables



Click new Repository Secret



Add another variable for storing Sonar token




Create GitLab CICD workflow yaml:

Go to GitLab repo where your Java project is, create a new file:.gitlab-ci.yml

The below file have three stages:
    - build
    - sonar
    - deploy

Create .gitlab-ci.yml CICD Pipeline:
stages:
  - build
  - sonar
  - deploy

build_war:
  stage: build
  image: maven:3.8.6-eclipse-temurin-11

  script:
    - echo "Building WAR file using Maven"
    - mvn clean install -f MyWebApp/pom.xml
    - echo "Listing target directory"
    - ls -la MyWebApp/target

  artifacts:
    paths:
      - MyWebApp/target/*.war
    expire_in: 1 hour

sonarqube_scan:
  stage: sonar
  image: maven:3.9.6-eclipse-temurin-17

  script:
    - |
      mvn sonar:sonar \
        -f MyWebApp/pom.xml \
        -Dsonar.projectKey=MyWebApp \
        -Dsonar.host.url="${SONAR_HOST_URL}" \
        -Dsonar.token="${SONAR_TOKEN}"
      
deploy_to_tomcat:
  stage: deploy
  image: curlimages/curl:latest

  dependencies:
    - build_war

  script:
    - echo "Deploying WAR file to Tomcat running on AWS EC2"

    - |
      curl -v -u ${TOMCAT_USER}:${TOMCAT_PASSWORD} \
      -T MyWebApp/target/MyWebApp.war \
      "http://${TOMCAT_HOST}/manager/text/deploy?path=/MyWebApp&update=true"

Commit the file.

As soon as you commit, build will run immediately in GitLab CICD. 
Now you can see the output of build in Actions tab.



Now login to SonarQube to see the Scan report


Saturday, June 6, 2026

How to Implement CICD Pipeline using GitLab Yaml | GitLab CICD Tutorials | GitLab CICD Pipeline | Build Java WAR file using GitLab CICD YAML file

Here below is the code for creating GitLab CICD yaml file for Java Web App project to automate build and deployment. 

What is GitLab CICD?

GitLab CI/CD is a continuous integration and continuous deployment solution built into GitLab.


GitLab CI/CD

GitLab CI/CD is a feature of GitLab that automates:

  • Building code
  • Testing applications
  • Scanning code
  • Deploying applications

whenever developers push code into Git repositories.

What is .gitlab-ci.yml?

The .gitlab-ci.yml file is the heart of GitLab CI/CD pipelines.

It contains:

  • Pipeline stages
  • Jobs
  • Scripts
  • Variables
  • Artifacts
  • Deployment instructions

GitLab automatically reads this file whenever code changes are pushed into the repository. GitLab Runner uses a Docker container image to run the job. 

Pre-requisites:

.gitlab-ci.yml for implementing CICD using GitLab

stages:

  - build

  - deploy


build_war:

  stage: build

  image: maven:3.8.6-eclipse-temurin-11


  script:

    - echo "Building WAR file using Maven"

    - mvn clean install -f MyWebApp/pom.xml

    - echo "Listing target directory"

    - ls -la MyWebApp/target


  artifacts:

    paths:

      - MyWebApp/target/*.war

    expire_in: 1 hour


deploy_to_tomcat:

  stage: deploy

  image: curlimages/curl:latest


  dependencies:

    - build_war


  script:

    - echo "Deploying WAR file to Tomcat running on AWS EC2"


    - |

      curl -v -u ${TOMCAT_USER}:${TOMCAT_PASSWORD} \

      -T MyWebApp/target/MyWebApp.war \

      "http://${TOMCAT_HOST}/manager/text/deploy?path=/MyWebApp&update=true"




How to integrate SonarQube with GitLab CICD Pipeline | SonarQube Integration with GitLab CICD | Automate Code Scan using SonarQube In GitLab CICD

  Please find steps for integrating SonarQube with GitLab CICD Pre-requisites: Make sure SonarQube is up and running Make sure Java Project ...