-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathJenkinsfile
53 lines (48 loc) · 1.79 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
pipeline {
agent any
parameters {
booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?')
choice(name: 'action', choices: ['apply', 'destroy'], description: 'Select the action to perform')
}
environment {
AWS_ACCESS_KEY_ID = credentials('aws-access-key-id')
AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key')
AWS_DEFAULT_REGION = 'ap-south-1'
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/CodeSagarOfficial/jenkins-scripts.git'
}
}
stage('Terraform init') {
steps {
sh 'terraform init'
}
}
stage('Plan') {
steps {
sh 'terraform plan -out tfplan'
sh 'terraform show -no-color tfplan > tfplan.txt'
}
}
stage('Apply / Destroy') {
steps {
script {
if (params.action == 'apply') {
if (!params.autoApprove) {
def plan = readFile 'tfplan.txt'
input message: "Do you want to apply the plan?",
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
}
sh 'terraform ${action} -input=false tfplan'
} else if (params.action == 'destroy') {
sh 'terraform ${action} --auto-approve'
} else {
error "Invalid action selected. Please choose either 'apply' or 'destroy'."
}
}
}
}
}
}