forked from bcgov/startup-sample-project-aws-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
133 lines (111 loc) · 4.47 KB
/
tf-plan.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
name: Terraform Plan
on:
workflow_run:
workflows:
- Pull Request
types:
- completed
env:
IMAGE_ID: ${{ secrets.AWS_ECR_URI }}
TF_VERSION: 1.0.5
TG_VERSION: 0.28.7
TG_SRC_PATH: terraform
TFC_WORKSPACE: dev
jobs:
plan:
name: terraform-plan
runs-on: ubuntu-20.04
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Check out the repo
uses: actions/checkout@v2
with:
ref: ${{ github.event.workflow_run.head_sha }}
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.TF_VERSION }}
cli_config_credentials_token: ${{ secrets.TFC_TEAM_TOKEN }}
- uses: peter-murray/[email protected]
with:
terragrunt_version: ${{ env.TG_VERSION }}
- name: Terragrunt Plan
id: tg_plan
working-directory: ${{ env.TG_SRC_PATH }}/${{ env.TFC_WORKSPACE }}
env:
TF_CLI_ARGS: -no-color
app_image: ${{ env.IMAGE_ID }}:${{ github.event.workflow_run.head_sha }}
run: terragrunt run-all plan --terragrunt-non-interactive | tee /tmp/tg_plan
continue-on-error: true
- name: Setup Graphviz
uses: ts-graphviz/setup-graphviz@v1
- name: Create inframap
id: create-inframap
run: |
curl -s -L -o inframap-linux-amd64.tar.gz https://github.com/cycloidio/inframap/releases/download/v0.5.2/inframap-linux-amd64.tar.gz
tar xzf inframap-linux-amd64.tar.gz
chmod +x inframap-linux-amd64
TG_CACHE_BACKEND_TF=$(find ${{ env.TG_SRC_PATH }}/${{ env.TFC_WORKSPACE }} -name backend.tf)
TG_CACHE_DIR=$(dirname -- "$TG_CACHE_BACKEND_TF")
./inframap-linux-amd64 generate --external-nodes=false --clean=false --hcl "$TG_CACHE_DIR" | dot -Tpng > /tmp/inframap.png
- name: Upload inframap
id: inframap
run: |
IMGBBURL=$(curl -s -L -X POST "https://api.imgbb.com/1/upload?key=${{ secrets.IMGBB_API_TOKEN }}" --form "image=@/tmp/inframap.png" | jq -r '.data.url')
echo "::set-output name=imgurl::$IMGBBURL"
- name: Get Terraform Plan Output
id: plan
uses: juliangruber/read-file-action@v1
with:
path: /tmp/tg_plan
- name: Download artifact
uses: actions/github-script@v3
with:
script: |
var artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr"
})[0];
var download = await github.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
- run: unzip pr.zip
- name: Update Pull Request
uses: actions/github-script@v3
env:
SUCCESS: '\u2705'
FAILURE: '\u274C'
INFRAMAP_URL: ${{ steps.inframap.outputs.imgurl }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
var fs = require('fs');
var issue_number = Number(fs.readFileSync('./NR'));
const plan = `${{ steps.tg_plan.outcome }}` == 'success' ? `${{ env.SUCCESS }}` : `${{ env.FAILURE }}`;
const output = `#### Terraform Plan: ${plan}
<details>
<summary>Show</summary>
\`\`\`terraform
${{ steps.plan.outputs.content }}
\`\`\`
</details>
#### Architecture Diagram
![img](${process.env.INFRAMAP_URL})
*Pusher:* @${{ github.actor }}, *Action:* [${process.env.GITHUB_WORKFLOW}: #${process.env.GITHUB_RUN_NUMBER}](https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID})`;
github.issues.createComment({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terragrunt Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1