-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathjenkinsfile01
89 lines (81 loc) · 3.06 KB
/
jenkinsfile01
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
pipeline {
{ node { label 'terraform-node' } }
environment {
TF_VERSION = '1.5.0' // Specify the Terraform version
TF_VAR_ENV = '' // Placeholder for environment variable to be set dynamically
}
parameters {
choice(name: 'ENV', choices: ['dev', 'qa', 'staging'], description: 'Select the environment to deploy to')
choice(name: 'deploy_choice', choices:['apply','destroy'],description:'The deployment type')
}
stages {
stage('1.Initialize') {
steps {
script {
// Set the environment variable based on the user choice
TF_VAR_ENV = params.ENV
// Select AWS credentials based on the environment
if (TF_VAR_ENV == 'dev') {
env.AWS_CREDENTIALS = 'aws-dev-credentials'
} else if (TF_VAR_ENV == 'qa') {
env.AWS_CREDENTIALS = 'aws-qa-credentials'
} else if (TF_VAR_ENV == 'staging') {
env.AWS_CREDENTIALS = 'aws-staging-credentials'
}
echo "Deploying to ${TF_VAR_ENV} environment"
}
}
}
stage('2. Terraform Init') {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.AWS_CREDENTIALS]]) {
sh """
terraform init -backend-config="path/to/backend-config-${TF_VAR_ENV}.tf"
"""
}
}
}
stage('3. Terraform Plan') {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.AWS_CREDENTIALS]]) {
sh """
terraform plan -var-file="vars/${TF_VAR_ENV}.tfvars"
"""
}
}
}
stage('4.Manual Approval') {
input {
message "Should we proceed?"
ok "Yes, we should."
parameters{
choice (name: 'Manual_Approval', choices: ['Approve','Reject'], description: 'Approve or Reject the deployment')
}
}
steps {
echo "Deployment ${Manual_Approval}"
}
}
stage('5.Terraform Deploy') {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.AWS_CREDENTIALS]]) {
sh """
terraform ${params.deploy_choice} -auto-approve -var-file="vars/${TF_VAR_ENV}.tfvars"
"""
}
}
}
}
post {
always {
echo "Cleaning up workspace..."
cleanWs()
}
success {
echo "Deployment to ${TF_VAR_ENV} was successful!"
}
failure {
echo "Deployment to ${TF_VAR_ENV} failed. Check the logs for details."
}
}
}