The first stage checks out the source code from the specified GitHub repository and its 'main' branch.
Stage 2 : Build
stage('Build and Test') { steps { sh 'mvn clean package' } }
The second stage compiles and packages the source code using Maven. It runs the 'mvn clean package' command to perform a clean build of the code.
Stage 3 : Static Code Analysis
The third stage performs a static code analysis of the code using SonarQube. It sets the SonarQube server URL in the 'environment' block and then runs the 'mvn sonar:sonar' command to analyze the code. It also sets up credentials for accessing the SonarQube server.
Stage 4 : Build and Push Image
The fourth stage builds a Docker image for the application and then pushes it to Docker Hub. It sets the Docker image name and tag in the 'environment' block and then runs the 'docker build' command to build the image. After that, it uses the Docker credentials to push the image to the Docker registry.
Stage 5 : Update Deployment File
The final stage updates the deployment file with the new Docker image tag and then pushes the changes to the GitHub repository. It sets the Git repository name, user name, and email in the 'environment' block. It then uses the Git credentials to push the updated deployment file to the GitHub repository. The 'sed' command is used to replace the old Docker image tag with the new tag in the deployment file. To generate the Github Personal Token click here.