Please find steps below for configuring your existing pipeline as a code (Jenkinsfile).
Pre-requistes:
1. Project setup in Bitbucket or GitHub
2. Jenkins and Tomcat (web container) set up.
3. Maven installed as well
4. Sonarqube setup and integrated with Jenkins
Add Jenkinsfile (pipeline code) to MyWebApp in BitBucket.
Step 1.
Go to Bitbucket and choose the Repo where you setup MyWebApp.
Step 2
Click on three dots ... and Add File
Step 3
Enter Jenkinsfile as a file name
Step 4
Copy and paste the below code
node {
def mvnHome = tool 'Maven3'
stage ('Checkout') {
checkout scm
}
stage ('Build') {
sh "${mvnHome}/bin/mvn clean install -f MyWebApp/pom.xml"
}
stage ('Code quality scan') {
withSonarQubeEnv('SonarQube') {
sh "${mvnHome}/bin/mvn sonar:sonar -f MyWebApp/pom.xml"
}
}
stage ('Nexus upload')
{
nexusArtifactUploader(
nexusVersion: 'nexus3',
protocol: 'http',
nexusUrl: 'ec2-18-223-182-14.us-east-2.compute.amazonaws.com: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 tomcat "
sh 'sudo cp /var/lib/jenkins/workspace/$JOB_NAME/MyWebApp/target/MyWebApp.war /var/lib/tomcat8/webapps'
}
stage ('DEV Approve') {
echo "Taking approval from DEV"
timeout(time: 7, unit: 'DAYS') {
input message: 'Do you want to deploy?', submitter: 'admin'
}
}
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})")
}
}
Step 5
Click on Commit and enter comments
Click on Commit
That's it. Pipeline as a code - Jenkinsfile is setup in BitBucket.
Create Pipeline and Run pipeline from Jenkinsfile
1. Login to Jenkins
2. Click on New item, give some name and choose Pipeline and say OK
3. Under build triggers, choose Poll SCM,
Enter H/02 * * * *
4. Under Pipeline section. click on choose pipeline script from SCM
5. Under SCM, choose Git
6. Enter SSH or HTTPS URL of repo and choose credentials - SSH private key if you are using SSH url. or user/password of GitHub if you are using HTTPS url.
Script path as Jenkinsfile
7. Click on Apply and Save
8. Click on Build now.
You should see pipeline running and application is deployed to Tomcat.