-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathJenkinsfile
More file actions
47 lines (39 loc) · 1.55 KB
/
Jenkinsfile
File metadata and controls
47 lines (39 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
node{
def tag, dockerHubUser, containerName, httpPort = ""
stage('Prepare Environment'){
echo 'Initialize Environment'
tag="3.0"
withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'dockerUser', passwordVariable: 'dockerPassword')]) {
dockerHubUser="$dockerUser"
}
containerName="bankingapp"
httpPort="8989"
}
stage('Code Checkout'){
try{
checkout scm
}
catch(Exception e){
echo 'Exception occured in Git Code Checkout Stage'
currentBuild.result = "FAILURE"
}
}
stage('Maven Build'){
sh "mvn clean package"
}
stage('Docker Image Build'){
echo 'Creating Docker image'
sh "docker build -t $dockerHubUser/$containerName:$tag --pull --no-cache ."
}
stage('Publishing Image to DockerHub'){
echo 'Pushing the docker image to DockerHub'
withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'dockerUser', passwordVariable: 'dockerPassword')]) {
sh "docker login -u $dockerUser -p $dockerPassword"
sh "docker push $dockerUser/$containerName:$tag"
echo "Image push complete"
}
}
stage('Ansible Playbook Execution'){
sh "export ANSIBLE_HOST_KEY_CHECKING=False && ansible-playbook -i inventory.yaml containerDeploy.yaml -e httpPort=$httpPort -e containerName=$containerName -e dockerImageTag=$dockerHubUser/$containerName:$tag -e key_pair_path=/var/lib/jenkins/server.pem --become"
}
}