-
Notifications
You must be signed in to change notification settings - Fork 1
107 lines (107 loc) · 4.06 KB
/
deploy.yaml
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
name: deploy
on:
push:
branches:
- main
pull_request:
workflow_dispatch:
env:
AWS_REGION: us-west-2
jobs:
validate:
name: Validate
runs-on: ubuntu-latest
outputs:
status: ${{ steps.envcheck.outputs.status }}
steps:
- name: Checkout
uses: actions/checkout@v3
- id: envcheck
name: Check environment variables
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
INDENT_WEBHOOK_SECRET: ${{ secrets.INDENT_WEBHOOK_SECRET }}
run: |
status="deploy"
if [ "$AWS_ACCESS_KEY_ID" == "" ] ||
[ "$AWS_SECRET_ACCESS_KEY" == "" ] ||
[ "$INDENT_WEBHOOK_SECRET" == "" ]
then
status="skip"
echo "Missing required enviroment variable(s)"
fi
echo "status=$status" >> $GITHUB_OUTPUT
terraform:
name: Terraform
needs: validate
if: needs.validate.outputs.status == 'deploy'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
aws-region: ${{ env.AWS_REGION }}
- name: Add profile credentials to ~/.aws/credentials
run: |
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} --profile default
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} --profile default
aws configure set aws_region ${{ env.AWS_REGION }} --profile default
- name: Terraform Format
id: fmt
run: terraform fmt -check -diff
- name: Terraform Init
id: init
run: terraform init
- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: terraform plan -input=false -no-color
continue-on-error: true
env:
TF_VAR_indent_webhook_secret: ${{ secrets.INDENT_WEBHOOK_SECRET }}
TF_VAR_auto_approval_duration: ${{ secrets.AUTO_APPROVAL_DURATION }}
- uses: actions/github-script@0.9.0
if: github.event_name == 'pull_request'
env:
PLAN: |-
terraform
${{ steps.plan.outputs.stdout }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`${process.env.PLAN}\`\`\`
</details>
*Actor: @${{ github.actor }}, Event: \`${{ github.event_name }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
run: terraform apply -input=false -auto-approve
env:
TF_VAR_indent_webhook_secret: ${{ secrets.INDENT_WEBHOOK_SECRET }}
TF_VAR_auto_approval_duration: ${{ secrets.AUTO_APPROVAL_DURATION }}
- name: Terraform Output
if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
run: terraform output
env:
TF_VAR_indent_webhook_secret: ${{ secrets.INDENT_WEBHOOK_SECRET }}
TF_VAR_example_api_key: ${{ secrets.EXAMPLE_API_KEY}}