|
1 |
| - pipeline { |
| 1 | + |
| 2 | +pipeline { |
| 3 | + |
2 | 4 | parameters {
|
3 |
| - string(defaultValue: 'vijaysnode', description: 'select node', name: 'node') |
4 |
| - } |
| 5 | + string(name: 'environment', defaultValue: 'terraform', description: 'Workspace/environment file to use for deployment') |
| 6 | + booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?') |
5 | 7 |
|
6 |
| - agent { |
7 |
| - node { |
8 |
| - label params.node |
9 |
| - } |
10 |
| - } |
11 |
| - stages{ |
12 |
| - stage('terraform-Demo') { |
13 |
| - steps { |
14 |
| - script { |
15 |
| - dir("ta-setup") |
16 |
| - { |
17 |
| - git branch: 'tf-jenkins', url: 'https://github.com/easyawslearn/Terraform-Tutorial.git' |
18 |
| - sh 'cd EC2withJenkins ' |
19 |
| - sh 'terraform init -upgrade=true -get=true -input=false -force-copy' |
20 |
| - sh 'terraform workspace new "terraform-demo"' |
21 |
| - sh 'echo "INFO: New terraform-demo workspace added."' |
22 |
| - sh 'terraform workspace select terraform-demo' |
23 |
| - sh 'echo "INFO: Terraform -> Planning..."' |
24 |
| - sh 'terraform plan -out plan_terraform -lock=true' |
25 |
| - sh 'echo "INFO: Terraform -> Executing..."' |
26 |
| - sh 'terraform apply plan_plan_terraform' |
| 8 | + } |
| 9 | + |
| 10 | + |
| 11 | + environment { |
| 12 | + AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID') |
| 13 | + AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY') |
| 14 | + } |
| 15 | + |
| 16 | + agent any |
| 17 | + options { |
| 18 | + timestamps () |
| 19 | + ansiColor('xterm') |
| 20 | + } |
| 21 | + stages { |
| 22 | + stage('checkout') { |
| 23 | + steps { |
| 24 | + script{ |
| 25 | + dir("terraform") |
| 26 | + { |
| 27 | + git "https://github.com/easyawslearn/Terraform-Tutorial.git" |
| 28 | + } |
27 | 29 | }
|
28 |
| - } |
29 | 30 | }
|
30 |
| - } |
31 |
| - } |
| 31 | + } |
| 32 | + |
| 33 | + stage('Plan') { |
| 34 | + steps { |
| 35 | + sh 'pwd;ls ; terraform init -input=false' |
| 36 | + // sh 'terraform workspace new ${environment}' |
| 37 | + sh 'terraform workspace select ${environment};pwd;ls' |
| 38 | + sh "pwd;cd terraform/aws-instance-first-script ;terraform plan -input=false -out tfplan ;pwd;ls" |
| 39 | + sh 'pwd;cd terraform/aws-instance-first-script ;terraform show -no-color tfplan > tfplan.txt' |
| 40 | + } |
| 41 | + } |
| 42 | + stage('Approval') { |
| 43 | + when { |
| 44 | + not { |
| 45 | + equals expected: true, actual: params.autoApprove |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + steps { |
| 50 | + script { |
| 51 | + def plan = readFile 'tfplan.txt' |
| 52 | + input message: "Do you want to apply the plan?", |
| 53 | + parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] |
| 54 | + } |
| 55 | + } |
32 | 56 | }
|
| 57 | + |
| 58 | + stage('Apply') { |
| 59 | + steps { |
| 60 | + sh "pwd;cd terraform/aws-instance-first-script ; terraform apply -input=false tfplan" |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + } |
0 commit comments