forked from akannan1087/myInfra2021Repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Jenkinsfile.prod
82 lines (73 loc) · 2.39 KB
/
Jenkinsfile.prod
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
pipeline {
agent any
environment {
AWS_CREDENTIALS = credentials('aws-networknuts') // Jenkins credential ID for AWS credentials
}
stages {
stage('Setup') {
steps {
// Set AWS credentials from Jenkins credentials
withCredentials([usernamePassword(credentialsId: 'aws-networknuts', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY')]) {
sh """
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set default.region ap-south-1
"""
}
}
}
stage('Checkout') {
steps {
// Checkout your source code repository
git branch: "master",
url: "https://github.com/arytmw/jenkins-terraform.git"
}
}
stage('Terraform Init') {
steps {
// Ensure Terraform is initialized
script {
sh "terraform init"
}
}
}
stage('Terraform Validate') {
steps {
// Validate Terraform configurations
script {
sh 'terraform validate'
}
}
}
stage('Terraform Plan') {
steps {
// Generate and show Terraform execution plan
script {
sh 'terraform plan -out=tfplan'
}
}
}
stage('Terraform Apply') {
steps {
// Apply Terraform changes (with auto-approval)
script {
sh 'terraform apply -auto-approve tfplan'
}
}
}
stage('Upload State to S3') {
steps {
// Upload the Terraform state file to S3
script {
sh 'aws s3 cp tfplan s3://jenkins-networknuts/'
}
}
}
stage('Deploy') {
steps {
// Execute deployment scripts or commands (if needed)
sh 'echo "Terraform Deployment complete"'
}
}
}
}