-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
101 lines (82 loc) · 3.42 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
pipeline {
//Use any jenkins agent available (master or slaves)
agent any
options {
ansiColor('xterm')
}
stages {
stage("plan"){
environment {
VSPHERE_SERVER = credentials('vsphere-hp')
//git_creds = credentials('AlexmartgonAccessToken')
VSPHERE_USER = credentials('esxi-alejandro-role')
}
steps {
echo 'Performing terraform plan and storing plan to apply at the next stage.'
// sh 'export TF_VAR_vsphere_server=$VSPHERE_SERVER'
// sh 'export TF_VAR_vsphere_user=$VSPHERE_USER_USR'
// sh 'export TF_VAR_vsphere_password=$VSPHERE_USER_PSW'
// sh 'echo $TF_VAR_vsphere_password'
// echo 'Planning Terraform script'
// sh 'terraform init'
// sh 'terraform plan -input=false -out tfplan'
// sh 'terraform show -no-color tfplan' //> tfplan.txt
// terraform show -no-color tfplan > tfplan.txt
//sh 'cat tfplan.txt'
sh '''
#!/bin/bash
export TF_VAR_vsphere_server=$VSPHERE_SERVER
export TF_VAR_vsphere_user=$VSPHERE_USER_USR
export TF_VAR_vsphere_password=$VSPHERE_USER_PSW
export PATH=$PATH:/home/jenkins/ovftool
echo "Remove the current terraform state"
if [ -f "terraform.tfstate" ]; then
rm terraform.tfstate
fi
terraform init
terraform plan -input=false -out tfplan
'''
sh 'terraform show tfplan'
}
}
stage("apply"){
environment {
VSPHERE_SERVER = credentials('vsphere-hp')
//git_creds = credentials('AlexmartgonAccessToken')
VSPHERE_USER = credentials('esxi-alejandro-role')
}
steps{
// Create an Approval Button with a timeout of 15minutes.
timeout(time: 15, unit: "MINUTES") {
input message: 'Do you want to approve the deployment? Look at the tfplan that was created on the previous stage.', ok: 'Yes'
}
echo 'Provisioning the VMs'
sh '''
#!/bin/bash
export TF_VAR_vsphere_server=$VSPHERE_SERVER
export TF_VAR_vsphere_user=$VSPHERE_USER_USR
export TF_VAR_vsphere_password=$VSPHERE_USER_PSW
export PATH=$PATH:/home/jenkins/ovftool
terraform init
terraform apply -input=false tfplan
'''
}
}
stage("output"){
steps{
echo 'Printing Terraform Output for the VMs created:'
sh '''
terraform output -state=terraform.tfstate
echo "Schema: Name, Private IP, RAM, CPU."
'''
//echo 'Schema: Name, Private IP, RAM, CPU.'
}
}
// stage("clean-up"){
// steps{
// echo 'Cleaning up environment variables.'
// //sh('unset TF_VAR_vsphere_server TF_VAR_vsphere_user TF_VAR_vsphere_password')
// }
// }
}
}