forked from richeney/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
88 lines (77 loc) · 2.78 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
pipeline {
agent any
tools {
'org.jenkinsci.plugins.terraform.TerraformInstallation' 'terraform'
}
environment {
TF_IN_AUTOMATION = 'true'
ARM_USE_MSI = true
ARM_TENANT_ID = credentials('tenant_id')
ARM_SUBSCRIPTION_ID = credentials('subscription_id')
ARM_BACKEND_RESOURCEGROUP = credentials('resource_group')
ARM_BACKEND_STORAGEACCOUNT = credentials('storage_account')
}
options {
ansiColor('xterm')
}
stages {
stage('Info') {
steps {
echo "Running ${env.JOB_NAME} (${env.BUILD_ID}) on ${env.JENKINS_URL}."
echo "Terraform version:"
sh 'terraform -version'
echo "Azure CLI version"
sh "az version --output jsonc"
sh '''
az login --identity --output jsonc
az account set --subscription $ARM_SUBSCRIPTION_ID
storageId=$(az storage account show --name $ARM_BACKEND_STORAGEACCOUNT \
--resource-group $ARM_BACKEND_RESOURCEGROUP --query id --output tsv)
az role assignment list --include-inherited \
--scope $storageId --query "[?contains(roleDefinitionName, 'Storage')]" --output jsonc
az logout
'''
}
}
stage('Terraform Init') {
steps {
sh '''
az login --identity --output jsonc
az account set --subscription $ARM_SUBSCRIPTION_ID
echo "Initialising Terraform"
terraform init \
--upgrade \
--backend-config="resource_group_name=$ARM_BACKEND_RESOURCEGROUP" \
--backend-config="storage_account_name=$ARM_BACKEND_STORAGEACCOUNT"
'''
}
}
stage('Terraform Plan') {
steps {
sh '''
az login --identity --output jsonc
az account set --subscription $ARM_SUBSCRIPTION_ID
echo "Terraform Plan"
terraform plan --input=false
'''
}
}
stage('Waiting for approval...') {
steps {
timeout(time: 10, unit: 'MINUTES') {
input(message: 'Deploy the infrastructure?')
}
}
}
stage('Terraform Apply') {
steps {
sh '''
az login --identity --output jsonc
az account set --subscription $ARM_SUBSCRIPTION_ID
echo "Terraform Apply"
terraform apply --input=false --auto-approve
'''
}
}
}
}