-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
104 lines (80 loc) · 3.63 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
93
94
95
96
97
98
99
100
101
102
103
104
pipeline {
parameters {
booleanParam(name: 'destroy', defaultValue: false, description: 'Destroys the resources created by the terraform')
}
environment {
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
}
agent any
// tools {
// // Define the Terraform tool installation
// terraform 'Terraform'
// }
stages {
stage('Git Clone') {
steps {
// git credentialsId: 'sajalgitcred', url: 'https://github.com/sajalrasto/GSynChallenge.git'
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: 'sajalgitcred', url: 'https://github.com/sajalrasto/GSynChallenge.git']])
}
}
// stage('Install Terraform') {
// // when {
// // // Only run this stage for the master branch
// // branch 'main'
// // }
// steps {
// // Download the Terraform binary
// // sh 'curl -LO https://releases.hashicorp.com/terraform/latest/terraform-linux-amd64'
// // // Make the Terraform binary executable
// // sh 'chmod +x terraform-linux-amd64'
// // // Move the Terraform binary to the PATH
// // sh 'sudo mv terraform-linux-amd64 /usr/local/bin/terraform'
// // sh 'sudo yum install -y yum-utils shadow-utils'
// // sh 'sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo'
// // sh 'sudo yum -y install terraform'
// sh 'wget https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linux_amd64.zip'
// sh 'unzip terraform_1.5.7_linux_amd64.zip'
// sh 'chmod +x terraform'
// sh 'sudo mv terraform /usr/local/bin/terraform'
// }
// }
// stage('AWS CLI install') {
// steps {
// // download awscli
// sh 'curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"'
// // unzip awscli
// sh 'unzip awscliv2.zip'
// //install
// sh 'sudo ./aws/install'
// }
// }
stage('Plan Terraform') {
steps {
// Set the Terraform workspace
sh 'terraform init -backend-config=backend.tfvars -reconfigure -input=false'
// Run Terraform plan
sh 'terraform plan -out=plan.out'
}
}
stage('Apply Terraform') {
steps {
script{
if ( params.destroy == false ) {
// Apply the Terraform plan
sh 'terraform apply -input=false -auto-approve'
} else {
// Destroy Terraform
sh 'terraform destroy -input=false -auto-approve'
}
}
}
}
}
post {
always {
// Archive the Terraform state file
archiveArtifacts artifacts: 'terraform.tfstate'
}
}
}