-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathJenkinsfile
94 lines (77 loc) · 3.45 KB
/
Jenkinsfile
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!groovy
pipeline {
agent any
tools {
maven 'MAVEN'
}
parameters {
choice(name: 'BUILD', choices: ['Yes', 'No'], description: 'Pick Yes or No')
string(name: "WAR_FILE_PATH", defaultValue: "/var/lib/jenkins/workspace/final-project/target/*SNAPSHOT.war", trim: false, description: "Provide war file job path in jenkins")
choice(name: "ENVIRONMENT", choices: ["", "Dev", "Test", "PROD"], description: 'Choose an environment to deploy')
string(name: "ANSIBLE_IP", defaultValue: "18.212.36.144", trim: false, description: "Provide Ansible Server IP-ADDRESS")
password(name: "ANSIBLE_USER_PASSWORD", defaultValue: "vijay", description: "Provide Ansible server user password")
}
stages {
stage('environment-setting') {
steps {
script {
env.BUILD = "Yes" // Setting env variable for Build
env.USER = 'Vijay'
//Generally it is possible to use Groovy’s conditionals in a declarative syntax, when we use a script step.
}
}
}
stage ('Checkout SCM'){
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/vijay2181/java-maven-SampleWarApp.git']]])
}
}
stage ('Build') {
when {
// Only build if "BUILD" value is 'Yes'
expression { params.BUILD == 'Yes' }
}
steps {
echo "Hello, Vijay!"
sh "mvn clean install"
sh "scp -o StrictHostKeyChecking=no ${params.WAR_FILE_PATH} vijay@${params.ANSIBLE_IP}:/opt/artifact"
}
}
stage("Deploy") {
parallel { //parallel is a switch case
stage("Dev") {
when {
allOf {
expression { params.BUILD == 'Yes' }
expression { params.ENVIRONMENT == 'Dev' } //two conditions must satisfy
}
}
steps {
echo "Executing Ansible playbook on remote Ansible Host from jenkins Server"
sh "sshpass -v -p '${params.ANSIBLE_USER_PASSWORD}' ssh 'vijay@${params.ANSIBLE_IP}' 'ansible-playbook /etc/ansible/playbooks/copy-war.yml --limit webserver1' "
}
}
stage("Test") {
when {
allOf {
expression { params.BUILD == 'Yes' }
expression { params.ENVIRONMENT == 'Test' }
}
}
steps {
echo "Executing Ansible playbook on remote Ansible Host from jenkins Server"
sh "sshpass -v -p '${params.ANSIBLE_USER_PASSWORD}' ssh 'vijay@${params.ANSIBLE_IP}' 'ansible-playbook /etc/ansible/playbooks/copy-war.yml --limit webserver2' "
}
}
stage("PROD") {
when { expression { params.BUILD == 'Yes' }
expression { params.ENVIRONMENT == 'Prod'}
}
steps {
sh "echo 'vijay'"
}
}
}
}
}
}