-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJenkinsfile
155 lines (129 loc) · 6.93 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
pipeline{
agent any
stages{
stage('build'){
agent {
dockerfile {
filename 'Dockerfile'
dir 'build'
args '-v $PWD:/home/codefiles'
}
}
steps{
sh label: '', script: 'cd /home/codefiles'
git changelog: false, credentialsId: '<git_credential>', poll: false, url: '<git url for app>'
sh label: '', script: 'ls -a'
sh label: '', script: 'mvn package'
}
post {
success {
archiveArtifacts artifacts: 'target/*.war', fingerprint: true
}
failure {
mail to: '<recipient>', subject: "Build Failed: ${currentBuild.fullDisplayName}",body: "This is the build ${env.BUILD_URL}"
cleanWs()
}
}
}
stage('test'){
steps{
sh label:'',script:"docker container run -itd --name webdocker -p 80:8094 awsacdev/ubuntu_tomcat:1.0"
sh label:'',script:"docker cp ${env.JENKINS_HOME}/jobs/${currentBuild.projectName}/builds/${currentBuild.number}/archive/target/. webdocker:/servers/tomcat8/webapps"
}
post {
success{
echo 'success'
mail to: '<recipient>',subject: "Deployed to Test: ${currentBuild.fullDisplayName}",body: "This is the build ${env.BUILD_URL}\n Test with this URL:${env.JENKINS_URL}Devops_maven_1-1.0.0/ \n\n Abort URL: ${env.BUILD_URL}stop"
}
failure{
mail to: '<recipient>', subject: "Failed to deploy to test: ${currentBuild.fullDisplayName}",body: "This is the build ${env.BUILD_URL}"
sh label:'',script:"docker container rm -f webdocker"
cleanWs()
}
}
}
stage('Approval Step'){
steps{
//----------------send an approval prompt-------------
script {
env.APPROVED_DEPLOY = input message: 'User input required',
parameters: [choice(name: 'Deploy?', choices: 'no\nyes', description: 'Choose "yes" if you want to deploy this build')]
}
//-----------------end approval prompt------------
}
}
stage('deploy'){
when {
environment name:'APPROVED_DEPLOY', value: 'yes'
}
steps{
sh label:'',script:"docker container rm -f webdocker"
echo 'deploy stage'
git changelog: false, credentialsId: '<GIT credential>', poll: false, url: '<CHEF GIT URL>'
sh label: '', script: "ls -a"
//-----copy war file--
sh label:'',script:"cp -r ${env.JENKINS_HOME}/jobs/${currentBuild.projectName}/builds/${currentBuild.number}/archive/target/. ${env.WORKSPACE}/cookbooks/prepare_env/files/default/warfile"
//--------end copy war file---
//------------create .chef folder----
sh label:'',script:'mkdir -p .chef'
//-----------end create .chef folder----
//---copy knife folder
sh label:'',script:"aws s3 cp s3://<knife_file> ${env.WORKSPACE}/.chef --recursive --profile adminprof"
//---end copy knife folder
//--------launch instance--
sh label:'',script:"aws ec2 run-instances --image-id ami-026c8acd92718196b --instance-type t2.micro --key-name <KEY_NAME> --security-group-ids <SG_ID> --subnet-id <SUBNET_ID> --count 1 --associate-public-ip-address --region us-east-1 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]' --profile jenkinsprof"
//---------end launch instance--
//waiting for the instance to be launched
sleep 30
script {
env.EC2DNS = sh(label:'',script:"aws ec2 describe-instances --filters 'Name=tag:Name,Values=WebServer' 'Name=instance-state-name,Values=running' --query 'Reservations[].Instances[].PublicDnsName' --profile jenkinsprof",returnStdout: true).trim()
}
echo "${env.EC2DNS}"
//-----get instance id
script {
env.EC2INST = sh(label:'',script:"aws ec2 describe-instances --filters 'Name=tag:Name,Values=WebServer' --query 'Reservations[].Instances[].InstanceId' --profile jenkinsprof",returnStdout: true).trim()
}
echo "${env.EC2INST}"
//------end get instance id
//upload war file to cookbook
sh label:'',script:'knife upload cookbooks --force --no-freeze'
//---end upload war file to cookbook
//-----copy key pair
sh label:'',script:"aws s3 cp s3://<key_file> ${env.WORKSPACE} --recursive --profile adminprof"
//------end copy pair
//---bootstrap node
sh label:'',script:"knife bootstrap ${env.EC2DNS} --ssh-user <USER> --sudo --yes --ssh-identity-file <KEY_FILE> --node-name prodnode --run-list 'role[prodserver]'"
//----end bootstrap node
}
post{
aborted{
sh label:'',script:"docker container rm -f webdocker"
}
failure{
mail to: '<recipient>',subject: "Failed PROD deployment: ${currentBuild.fullDisplayName}",body: "This is the build ${env.BUILD_URL}"
//terminate the ec2
sh label:'',script:"aws ec2 terminate-instances --instance-ids ${env.EC2INST} --profile jenkinsprof"
cleanWS()
}
success{
mail to: '<recipient>',subject: "Deployment to PROD succeeded: ${currentBuild.fullDisplayName}",body: "This is the build ${env.BUILD_URL}. The application is live at ${env.EC2DNS}/Devops_maven_1-1.0.0/"
cleanWs()
}
}
}
stage('abortdeploy'){
when{
environment name:'APPROVED_DEPLOY',value:'no'
}
steps{
sh label:'',script:"docker container rm -f webdocker"
mail to:'<recipient>',subject:'Deployment Aborted',body:"The deployment has been aborted. Here are the details: Project Name: ${currentBuild.projectName} Build #: ${currentBuild.number}"
}
post {
always{
cleanWS()
}
}
}
}
}