-
Notifications
You must be signed in to change notification settings - Fork 1
112 lines (96 loc) · 4.21 KB
/
vpc-infra-pipeline.yml
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
105
106
107
108
109
110
111
112
name: 'VPC resources pipeline'
on:
workflow_dispatch:
inputs:
logLevel:
description: 'VPC resources infra'
required: false
default: 'initiate'
action:
required: true
description: 'Create or Destroy'
default: 'Create'
env:
required: true
description: 'qa or test or prod'
env:
tf_version: '0.13.5'
tf_working_dir_app: './deployment/vpc/'
aws_region: 'us-east-1'
environment: ${{ github.event.inputs.env }}
jobs:
terraform:
name: 'VPC-Resources-Deployment-Pipeline-${{ github.event.inputs.action }}'
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Get the current branch name
shell: bash
run: echo "::set-output name=branch::${GITHUB_REF#refs/heads/}"
id: extract_branch
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v2
# Configure AWS Credentials using IAM Role in the GitHub secrets
- name: AWS IAM Assume Role
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.aws_region }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
- name: Run aws cli to fetch Account Id
id: aws_details
run: |
ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
echo AWS_ACCOUNT=$ACCOUNT_ID >> $GITHUB_ENV
CURRENT_REGION=$(aws ec2 describe-availability-zones --query 'AvailabilityZones[0].[RegionName]' --output text)
echo AWS_REGION=$CURRENT_REGION >> $GITHUB_ENV
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
- name: Terraform Init For VPC Resources
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_working_dir: ${{ env.tf_working_dir_app }}
tf_actions_subcommand: 'init'
tf_actions_comment: false
args: '-backend-config="config/${{ env.environment }}-backend-config.config" -backend-config="bucket=${{ env.environment }}-tfstate-${{ env.AWS_ACCOUNT }}-${{ env.AWS_REGION }}"'
- name: 'Terraform Validate'
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_subcommand: 'validate'
tf_actions_working_dir: ${{ env.tf_working_dir_app }}
tf_actions_comment: false
- name: Terraform Plan VPC Resources
if: github.event.inputs.action == 'Create'
id: app-plan
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_working_dir: ${{ env.tf_working_dir_app }}
tf_actions_comment: false
tf_actions_subcommand: 'plan'
args: '-var="environment=${{ env.environment }}" -var-file="${{ env.environment }}.tfvars"'
- name: Terraform Apply For VPC Resources
if: steps.app-plan.outputs.tf_actions_plan_has_changes == 'true' && github.event.inputs.action == 'Create'
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_working_dir: ${{ env.tf_working_dir_app }}
tf_actions_subcommand: 'apply'
tf_actions_comment: false
args: '-auto-approve -var="environment=${{ env.environment }}" -var-file="${{ env.environment }}.tfvars"'
- name: Terraform Destroy For VPC Resources
if: github.event.inputs.action == 'Destroy'
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: ${{ env.tf_version }}
tf_actions_working_dir: ${{ env.tf_working_dir_app }}
tf_actions_subcommand: 'destroy'
tf_actions_comment: false
args: '-auto-approve -var="environment=${{ env.environment }}" -var-file="${{ env.environment }}.tfvars"'