This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Jenkinsfile
92 lines (84 loc) · 3.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
89
90
91
92
// ******
// Exptected parameters for HTTP request to pipeline
// ******
//deployment="client01"
//vm_images_rg="vm-images-RG"
//vmss_rg="vmss_RG"
//image_name="vmimage01"
//vmss_name="testvmss"
//location="northeurope"
podTemplate(label: 'azurevm', containers: [
containerTemplate(name: 'terraform-az', image: '<containerRegistry>/terraform-az', ttyEnabled: true, command: 'cat',envVars: [
secretEnvVar(key: 'ARM_CLIENT_ID', secretName: '<your-secret-name>', secretKey: 'clientid'),
secretEnvVar(key: 'ARM_CLIENT_SECRET', secretName: '<your-secret-name>', secretKey: 'clientsecret'),
secretEnvVar(key: 'ARM_TENANT_ID', secretName: '<your-secret-name>', secretKey: 'tenantid'),
secretEnvVar(key: 'ARM_SUBSCRIPTION_ID', secretName: '<your-secret-name>', secretKey: 'subscriptionid')
]),
])
{
node('azurevm') {
def gitRepo = 'https://github.com/<your-repo>/terraform-with-jenkins-samples'
currentBuild.result = "SUCCESS"
try {
stage('Init parameters'){
container('terraform-az') {
// Get SSH public for the VMSS from Jenkins
withCredentials([sshUserPrivateKey(credentialsId: '<your-public-key-id>', keyFileVariable: 'PUBLICKEY')]) {
sh """
mkdir /home/jenkins/.ssh
cat $PUBLICKEY >/home/jenkins/.ssh/id_rsa.pub
"""
}
}
}
stage('Checkout'){
container('terraform-az') {
// Get the terraform plan
git url: gitRepo, branch: 'master'
}
}
stage('Terraform init'){
container('terraform-az') {
// Initialize the plan
sh """
cd terraform-plans/create-vmss-from-image
terraform init -input=false
"""
}
}
stage('Terraform plan'){
container('terraform-az') {
// Get the VM image ID for the VMSS
sh '''
az login --service-principal -u $ARM_CLIENT_ID -p $ARM_CLIENT_SECRET --tenant $ARM_TENANT_ID
'''
image_id = sh (
script: "az image show -g $vm_images_rg -n $image_name --query '{VMName:id}' --out tsv",
returnStdout: true).trim()
sh (script:"cd terraform-plans/create-vmss-from-image && terraform plan -out=tfplan -input=false -var 'terraform_resource_group='$vmss_rg -var 'terraform_vmss_name='$vmss_name -var 'terraform_azure_region='$location -var 'terraform_image_id='$image_id")
}
}
stage('Terraform apply'){
container('terraform-az') {
// Apply the plan
sh """
cd terraform-plans/create-vmss-from-image
terraform apply -input=false -auto-approve "tfplan"
"""
}
}
stage('Upload tfstate'){
container('terraform-az') {
// Upload the state of the plan to Azure Blob Storage
sh (script: "cd terraform-plans/create-vmss-from-image && tar -czvf ~/workspace/${env.JOB_NAME}/$deployment'.tar.gz' .")
sh "pwd"
azureUpload blobProperties: [cacheControl: '', contentEncoding: '', contentLanguage: '', contentType: '', detectContentType: true], containerName: '<container-name>', fileShareName: '', filesPath: '${deployment}.tar.gz', storageCredentialId: '<jenkins-storage-id>', storageType: 'blobstorage'
}
}
}
catch (err) {
currentBuild.result = "FAILURE"
throw err
}
}
}