forked from jruels/tf-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
66 lines (56 loc) · 1.62 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
pipeline {
agent any
environment {
SVC_ACCOUNT_KEY = credentials('jenkins-gcp')
PROJECT_ID = 'infra-tf-20210722-student3xinn'
DEFAULT_LOCAL_TMP = 'tmp/'
ANSIBLE_USER = 'ubuntu'
HOME = '/tmp'
}
stages {
stage('Cleanup') {
steps {
cleanWs(deleteDirs: true)
}
}
stage('Checkout') {
steps {
checkout scm
sh 'mkdir -p creds'
sh 'echo $SVC_ACCOUNT_KEY | base64 -di > ./creds/jenkins-sa.json'
}
}
stage('Install Terraform') {
steps {
sh 'curl -o terraform.zip https://releases.hashicorp.com/terraform/0.14.5/terraform_0.14.5_linux_amd64.zip'
sh 'unzip terraform.zip'
}
}
stage('TF Plan') {
steps {
sh './terraform init -reconfigure'
sh './terraform plan -var project_id=$PROJECT_ID -var jenkins_workers_project_id=$PROJECT_ID -out myplan'
}
}
stage('Approval') {
steps {
script {
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ])
}
}
}
stage('TF Apply') {
steps {
sh './terraform apply -input=false myplan'
}
}
stage('Run Ansible playbook') {
steps {
withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'cicd-ssh-key', keyFileVariable: 'KEY')]) {
sh 'sleep 20'
sh 'ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook playbook.yml -i tf.gcp.yml --private-key ${KEY} -b -u $ANSIBLE_USER'
}
}
}
}
}