-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (100 loc) · 5.01 KB
/
aws_ec2_golden.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
---
name: AWS EC2 Golden Files
on:
workflow_dispatch:
pull_request:
paths:
- .github/workflows/aws_ec2_golden.yml
- .tool-versions
- aws/ec2/terraform/**
push:
branches:
- main
paths:
- aws/ec2/terraform/**
# limit to a single execution per actor of this workflow
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
AWS_PROFILE: infex
AWS_REGION: eu-west-2
TF_PATH: ${{ github.workspace }}/aws/ec2/terraform
GOLDEN_PATH: ${{ github.workspace }}/aws/ec2/test/golden
jobs:
compare:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
################## Checkout ##################
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
############# Tool Installations #############
- name: Install tooling using asdf
uses: asdf-vm/actions/install@05e0d2ed97b598bfce82fd30daf324ae0c4570e6 # v3
################## Secrets ###################
- name: Import Secrets
id: secrets
uses: hashicorp/vault-action@v3
with:
url: ${{ secrets.VAULT_ADDR }}
method: approle
roleId: ${{ secrets.VAULT_ROLE_ID }}
secretId: ${{ secrets.VAULT_SECRET_ID }}
exportEnv: false
secrets: |
secret/data/products/infrastructure-experience/ci/common AWS_ACCESS_KEY;
secret/data/products/infrastructure-experience/ci/common AWS_SECRET_KEY;
################ AWS Config ##################
# Official action does not support profiles
- name: Add profile credentials to ~/.aws/credentials
shell: bash
run: |
aws configure set aws_access_key_id ${{ steps.secrets.outputs.AWS_ACCESS_KEY }} --profile ${{ env.AWS_PROFILE }}
aws configure set aws_secret_access_key ${{ steps.secrets.outputs.AWS_SECRET_KEY }} --profile ${{ env.AWS_PROFILE }}
aws configure set region ${{ env.AWS_REGION }} --profile ${{ env.AWS_PROFILE }}
- name: Copy provider override
run: |
cp "${{ github.workspace }}/aws/ec2/test/fixtures/provider_override.tf" "${TF_PATH}/provider_override.tf"
- name: Run Terraform plan
working-directory: aws/ec2/terraform
run: |
terraform init
# We are hardcoding the AMI as it's a moving target
# Same for the SSH Key as we don't have one on the device and would otherwise omit values from the plan
terraform plan -var aws_ami="ami" -var generate_ssh_key_pair="true" -out=tfplan || true
- name: Convert Terraform plan to JSON
working-directory: aws/ec2/terraform
run: |
# Use jq to "pretty print" the JSON output to allow comparison
terraform show -json tfplan | jq > tfplan.json
- name: Extract planned values from Terraform plan
run: |
# extracting the planned values and their actual infra instead of diffing the whole document
# since we always plan against nothing, it includes all planned objects
# it does not contain sensetive data
jq --sort-keys '.planned_values.root_module' "${TF_PATH}/tfplan.json" > "${TF_PATH}/tfplan_extracted.json"
- uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
with:
name: plan-artifact-json
path: ${{ env.TF_PATH }}/tfplan_extracted.json
retention-days: 1
- name: Compare Terraform plan with golden file
working-directory: aws/ec2/terraform
run: |
delta "${GOLDEN_PATH}/tfplan.json" "${TF_PATH}/tfplan_extracted.json"
exit $?
- name: Post diff on PR
if: always() && github.event_name == 'pull_request'
uses: int128/diff-action@b1b67adf9e7a0a4faa46e38f9fc1936f789ecdab # v1
with:
base: ${{ env.GOLDEN_PATH }}//tfplan.json
head: ${{ env.TF_PATH }}/tfplan_extracted.json
comment-header: |
## Terraform golden plan diff
comment-footer: |
Check the delta diff in the [workflow run](${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}) for a detailed comparison.
If the changes are expected, you can use the uploaded artifact on the workflow to update the golden file on your branch.
Alternatively run `just regenerate-aws-ec2-golden-file` locally to update the golden file.