forked from ine-labs/AWSGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
95 lines (79 loc) · 2.49 KB
/
tf-apply-main.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
name: 'Terraform Apply'
on:
workflow_dispatch:
inputs:
module:
type: choice
description: "Select which module to deploy"
options:
- module-1
- module-2
required: true
permissions: write-all
jobs:
terraform:
name: 'Terraform'
runs-on: ubuntu-latest
environment: production
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: "us-east-1"
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set Account ID
id: account
run: |
echo "::set-output name=ACCOUNT_ID::$(aws sts get-caller-identity --query Account --output text)"
- name: Check previous AWSGoat Deployment
id: check
run: |
echo $ACCOUNT_ID
aws s3api head-object --bucket do-not-delete-awsgoat-state-files-${{ steps.account.outputs.ACCOUNT_ID }} --key terraform.tfstate
continue-on-error: true
- name: Exit if previous deployment exists
if: steps.check.outcome == 'success'
run: |
echo "A Previous AWSGoat deployment exists, run the Terraform Destroy Action"
exit 1
# Initialize a new or existing Terraform working directory
- name: Terraform Init
run: |
cd modules/${{ github.event.inputs.module }}
terraform init
# Installs boto3
- uses: actions/setup-python@v2
with:
python-version: 3
- name: install
run: |
pip install boto3
- name: Terraform Plan
id: plan
run: |
cd modules/${{ github.event.inputs.module }}
terraform plan -input=false
continue-on-error: false
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
run: |
cd modules/${{ github.event.inputs.module }}
terraform apply -auto-approve -input=false
continue-on-error: false
# Copy tfstate file to s3 bucket
- name: Copy terraform.tfstate file to s3bucket
if: always()
run: |
cd modules/${{ github.event.inputs.module }}
aws s3 cp ./terraform.tfstate s3://do-not-delete-awsgoat-state-files-${{ steps.account.outputs.ACCOUNT_ID }}/terraform.tfstate
# Terraform Output the API Gateway url
- name: Application URL
run: |
cd modules/${{ github.event.inputs.module }}
terraform output