forked from antrusd/ansible-wildfly-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
179 lines (166 loc) · 6.87 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def TF_OPERATION = 'Create'
def TF_WORKSPACE = 'default'
def TF_VM_COUNT = '1'
pipeline {
agent {
label "ansible && linux"
}
stages {
stage('Clean Workspace') {
steps {
sh "git clean -xdff"
}
}
stage('Install Ansible') {
steps {
sh "virtualenv --system-site-packages -p python3 ${WORKSPACE}/ansible-tf-azure"
withPythonEnv("${WORKSPACE}/ansible-tf-azure/") {
sh "pip install --upgrade pip"
sh "pip install -r requirements.txt"
}
}
}
stage('Install Terraform') {
steps {
sh "curl -o ${WORKSPACE}/terraform.zip https://releases.hashicorp.com/terraform/0.12.12/terraform_0.12.12_linux_amd64.zip"
sh "unzip -o -d ${WORKSPACE}/ansible-tf-azure/bin/ ${WORKSPACE}/terraform.zip"
sh "rm -f ${WORKSPACE}/terraform.zip"
withCredentials([string(credentialsId: 'ARM_CLIENT_ID', variable: 'ARM_CLIENT_ID'), string(credentialsId: 'ARM_CLIENT_SECRET', variable: 'ARM_CLIENT_SECRET'), string(credentialsId: 'ARM_SUBSCRIPTION_ID', variable: 'ARM_SUBSCRIPTION_ID'), string(credentialsId: 'ARM_TENANT_ID', variable: 'ARM_TENANT_ID')]) {
sh "${WORKSPACE}/ansible-tf-azure/bin/terraform init -no-color"
}
}
}
stage('Select Terraform Operation') {
steps {
script {
TF_OPERATION = input (
ok: 'Next',
message: 'Please Select Terraform Operation ',
parameters: [
choice (
name: 'TF_OPERATION',
choices: ['Create', 'Delete']
)
]
)
}
echo "Terraform Operation: ${TF_OPERATION}"
}
}
stage('Input Terraform Workspace') {
steps {
script {
TF_WORKSPACE = input (
message: 'Please Input Terraform Workspace ',
parameters: [
string (
name: 'TF_WORKSPACE',
defaultValue: 'default',
trim: true
)
]
)
}
echo "Terraform Workspace: ${TF_WORKSPACE}"
}
}
stage('Terraform Destroy') {
when {
expression {
TF_OPERATION == 'Delete'
}
}
steps {
withCredentials([string(credentialsId: 'ARM_CLIENT_ID', variable: 'ARM_CLIENT_ID'), string(credentialsId: 'ARM_CLIENT_SECRET', variable: 'ARM_CLIENT_SECRET'), string(credentialsId: 'ARM_SUBSCRIPTION_ID', variable: 'ARM_SUBSCRIPTION_ID'), string(credentialsId: 'ARM_TENANT_ID', variable: 'ARM_TENANT_ID')]) {
sh "${WORKSPACE}/ansible-tf-azure/bin/terraform workspace select ${TF_WORKSPACE}"
sh "${WORKSPACE}/ansible-tf-azure/bin/terraform plan -input=false -no-color"
sh "${WORKSPACE}/ansible-tf-azure/bin/terraform destroy -input=false -no-color -auto-approve"
}
}
}
stage('Input VM Count') {
when {
expression {
TF_OPERATION == 'Create'
}
}
steps {
script {
TF_VM_COUNT = input (
message: 'Please Input VM Count ',
parameters: [
string (
name: 'TF_VM_COUNT',
defaultValue: '1',
trim: true
)
]
)
}
echo "Terraform VM Count: ${TF_VM_COUNT}"
}
}
stage('Terraform Apply') {
when {
expression {
TF_OPERATION == 'Create'
}
}
steps {
withCredentials([string(credentialsId: 'ARM_CLIENT_ID', variable: 'ARM_CLIENT_ID'), string(credentialsId: 'ARM_CLIENT_SECRET', variable: 'ARM_CLIENT_SECRET'), string(credentialsId: 'ARM_SUBSCRIPTION_ID', variable: 'ARM_SUBSCRIPTION_ID'), string(credentialsId: 'ARM_TENANT_ID', variable: 'ARM_TENANT_ID')]) {
sh "${WORKSPACE}/ansible-tf-azure/bin/terraform workspace new ${TF_WORKSPACE} || ${WORKSPACE}/ansible-tf-azure/bin/terraform workspace select ${TF_WORKSPACE}"
sh "${WORKSPACE}/ansible-tf-azure/bin/terraform plan -input=false -no-color -out t_plan -var count_of_VMs=${TF_VM_COUNT}"
sh "${WORKSPACE}/ansible-tf-azure/bin/terraform apply -input=false -no-color t_plan"
}
}
}
stage('Check Connection Target') {
when {
expression {
TF_OPERATION == 'Create'
}
}
steps {
withPythonEnv("${WORKSPACE}/ansible-tf-azure/") {
ansiblePlaybook colorized: true,
installation: 'ansible',
credentialsId: 'vignoadmin',
playbook: 'winrm_ping.yml',
extras: '-e target_hosts=all'
}
}
}
stage('Deploying WildFly') {
when {
expression {
TF_OPERATION == 'Create'
}
}
steps {
withPythonEnv("${WORKSPACE}/ansible-tf-azure/") {
ansiblePlaybook colorized: true,
installation: 'ansible',
credentialsId: 'vignoadmin',
playbook: 'deploy_wildfly.yml',
extras: '-e target_hosts=all'
}
}
}
stage('Check Installation Result') {
when {
expression {
TF_OPERATION == 'Create'
}
}
steps {
withPythonEnv("${WORKSPACE}/ansible-tf-azure/") {
ansiblePlaybook colorized: true,
installation: 'ansible',
credentialsId: 'vignoadmin',
playbook: 'winrm_url.yml',
extras: '-e target_hosts=all'
}
}
}
}
}