Jenkins Scripted Pipeline - Create Jenkins Pipeline for Automating Builds, Code quality checks, Deployments to Tomcat - How to build, deploy WARs using Jenkins Pipeline - Build pipelines integrate with github, Sonarqube, Slack, JaCoCo, Nexus, Tomcat
Pre-requisites:
1. Install Deploy to container, Slack, Jacoco, Nexus Artifact Uploader and SonarQube plug-ins (if already installed, you can skip it)
Steps to Create Scripted Pipeline in Jenkins
1. Login to Jenkins
2. Create a New item
3. Give name as MyfirstPipelineJob and choose pipeline
4. Click ok. Pipeline is created now
5. Under build triggers, click on poll SCM, schedule as
H/02 * * * *
6. Go to Pipeline definition section, click on Pipeline syntax link. under sample step drop down, choose checkout: Checkout from version control. enter bitbucket or GitHub Repository URL, and enter right credentials. Click here to learn to use PAT if you are using GitHub. scroll down, click on Generate Pipeline script. Copy the code.
7. Now copy the below pipeline code highlighted section into Pipeline section in the pipeline. Please copy stage by stage
8. Change Maven3, SonarQube variables and also Slack channel name as highlighted above in red as per your settings.
9. For Nexus Upload stage, You need to change the Nexus URL and credentials ID for Nexus (which you can grab from Credentials tab after login)
10. For Dev Deploy stage, you can copy credentials ID used for connecting to Tomcat.
node {
def mvnHome = tool 'Maven3'
stage ("checkout") {
copy code here which you generated from step #6
}
stage ('build') {
sh "${mvnHome}/bin/mvn clean install -f MyWebApp/pom.xml"
}
stage ('Code Quality scan') {
withSonarQubeEnv('SonarQube') {
sh "${mvnHome}/bin/mvn -f MyWebApp/pom.xml sonar:sonar"
}
}
stage ('Code coverage') {
jacoco()
}
stage ('Nexus upload') {
nexusArtifactUploader(
nexusVersion: 'nexus3',
protocol: 'http',
nexusUrl: 'nexus_url:8081',
groupId: 'myGroupId',
version: '1.0-SNAPSHOT',
repository: 'maven-snapshots',
credentialsId: '2c293828-9509-49b4-a6e7-77f3ceae7b39',
artifacts: [
[artifactId: 'MyWebApp',
classifier: '',
file: 'MyWebApp/target/MyWebApp.war',
type: 'war']
]
)
}
stage ('DEV Deploy') {
echo "deploying to DEV Env "
deploy adapters: [tomcat9(credentialsId: '4c55fae1-a02d-4b82-ba34-d262176eeb46', path: '', url: 'http://your_tomcat_url:8080')], contextPath: null, war: '**/*.war'
}
stage ('Slack notification') {
slackSend(channel:'channel-name', message: "Job is successful, here is the info - Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
stage ('DEV Approve') {
echo "Taking approval from DEV Manager for QA Deployment"
timeout(time: 7, unit: 'DAYS') {
input message: 'Do you approve QA Deployment?', submitter: 'admin'
}
}
stage ('QA Deploy') {
echo "deploying into QA Env "
deploy adapters: [tomcat9(credentialsId: '4c55fae1-a02d-4b82-ba34-d262176eeb46', path: '', url: 'http://your_tomcat_url:8080')], contextPath: null, war: '**/*.war'
}
slackSend(channel:'channel-name', message: "QA Deployment was successful, here is the info - Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
stage ('QA Approve') {
echo "Taking approval from QA manager"
timeout(time: 7, unit: 'DAYS') {
input message: 'Do you want to proceed to PROD Deploy?', submitter: 'admin,manager_userid'
}
echo "deploying into PROD Env "
deploy adapters: [tomcat9(credentialsId: '4c55fae1-a02d-4b82-ba34-d262176eeb46', path: '', url: 'http://your_tomcat_url:8080')], contextPath: null, war: '**/*.war'
}
}
11. Click Apply, Save