-
-
Notifications
You must be signed in to change notification settings - Fork 21
165 lines (152 loc) · 6.1 KB
/
dev_deploy.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
---
name: Deploy/Destroy Branch Dev Environment
on:
pull_request:
types: [labeled, closed]
env:
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.HUSHLINE_DEV_TF_TOKEN }}
GITHUB_TOKEN: ${{ github.token }}
WORKSPACE_NAME: hushline-dev-${{ github.head_ref }}
TF_PROJECT_HUSH_LINE_DEV: prj-iEruEQFmaNTCRAtA
DEV_TF_PATH: terraform/dev
DO_APP_NAME: dev-${{ github.head_ref }}
HUSHLINE_INFRA_REPO: scidsg/hushline-infra
HUSHLINE_INFRA_REF: main
jobs:
deploy:
if: ${{ github.event.action == 'labeled' && github.event.label.name == 'deploy' }}
runs-on: ubuntu-latest
steps:
- name: checkout terraform
uses: actions/checkout@v4
with:
repository: ${{ env.HUSHLINE_INFRA_REPO }}
ref: ${{ env.HUSHLINE_INFRA_REF }}
token: ${{ secrets.HUSHLINE_INFRA_TOKEN }}
- name: Create workspace
run: |
set -e
resp=$(curl -s -o /dev/null -w "%{http_code}" \
-X GET https://app.terraform.io/api/v2/organizations/science-and-design/workspaces/${{ env.WORKSPACE_NAME }} \
-H 'Content-Type: application/vnd.api+json' \
-H 'Authorization: Bearer ${{ secrets.HUSHLINE_DEV_TF_TOKEN }}')
echo "Got $resp when looking up workspace"
if [[ "$resp" == '404' ]]; then
echo "Attempting to create workspace..."
resp=$(curl -X POST https://app.terraform.io/api/v2/organizations/science-and-design/workspaces \
-H 'Content-Type: application/vnd.api+json' \
-H 'Authorization: Bearer ${{ secrets.HUSHLINE_DEV_TF_TOKEN }}' \
-d '{
"data": {
"type": "workspaces",
"attributes": {
"working-directory": "${{ env.DEV_TF_PATH }}",
"name": "${{ env.WORKSPACE_NAME }}",
"auto-destroy-activity-duration": "14d",
"execution-mode": "remote",
"source-name": "scidsg/hushline dev deploy"
},
"relationships": {
"project": {
"data": {
"id": "${{ env.TF_PROJECT_HUSH_LINE_DEV }}"
}
}
}
}
}')
workspace_id=$(echo $resp | jq -r '.data.id')
echo "Created workspace '${{ env.WORKSPACE_NAME }}' with id '$workspace_id'"
echo "Tagging workspace with the 'dev' tag..."
curl -X POST https://app.terraform.io/api/v2/workspaces/${workspace_id}/relationships/tags \
-H 'Content-Type: application/vnd.api+json' \
-H 'Authorization: Bearer ${{ secrets.HUSHLINE_DEV_TF_TOKEN }}' \
-d '{
"data": [
{
"type": "tags",
"attributes": {
"name": "dev"
}
}
]
}'
fi
- name: Plan test infrastrucutre
uses: dflook/[email protected]
with:
path: ${{ env.DEV_TF_PATH }}
workspace: ${{ env.WORKSPACE_NAME }}
add_github_comment: true
variables: |
branch = "${{ github.head_ref }}"
name = "${{ env.DO_APP_NAME }}"
- name: Apply test infrastrucutre
uses: dflook/[email protected]
with:
path: ${{ env.DEV_TF_PATH }}
workspace: ${{ env.WORKSPACE_NAME }}
variables: |
branch = "${{ github.head_ref }}"
name = "${{ env.DO_APP_NAME }}"
- name: terraform output
uses: dflook/[email protected]
id: tf-outputs
with:
path: ${{ env.DEV_TF_PATH }}
workspace: ${{ env.WORKSPACE_NAME }}
- name: comment app url
uses: actions/[email protected]
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: ':rocket: App successfully deployed to ${{ steps.tf-outputs.outputs.app_live_url }}!'
})
destroy:
if: ${{ (github.event.action == 'labeled' && github.event.label.name == 'destroy') || github.event.action == 'closed' }}
runs-on: ubuntu-latest
steps:
- name: checkout terraform
uses: actions/checkout@v4
if: contains(github.event.pull_request.labels.*.name, 'deploy')
with:
repository: ${{ env.HUSHLINE_INFRA_REPO }}
ref: ${{ env.HUSHLINE_INFRA_REF }}
token: ${{ secrets.HUSHLINE_INFRA_TOKEN }}
- name: destroy worspace
uses: dflook/[email protected]
if: contains(github.event.pull_request.labels.*.name, 'deploy')
id: first_try
continue-on-error: true
with:
path: ${{ env.DEV_TF_PATH }}
workspace: ${{ env.WORKSPACE_NAME }}
variables: |
branch = "${{ github.head_ref }}"
name = "${{ env.DO_APP_NAME }}"
- name: retry destroy worspace
uses: dflook/[email protected]
if: ${{ steps.first_try.outputs.failure-reason == 'destroy-failed' }}
with:
path: ${{ env.DEV_TF_PATH }}
workspace: ${{ env.WORKSPACE_NAME }}
variables: |
branch = "${{ github.head_ref }}"
name = "${{ env.DO_APP_NAME }}"
- name: remove deploy label
uses: actions-ecosystem/[email protected]
if: contains(github.event.pull_request.labels.*.name, 'deploy')
with:
github_token: ${{ github.token }}
labels: |
deploy
- name: remove destroy label
uses: actions-ecosystem/[email protected]
if: contains(github.event.pull_request.labels.*.name, 'destroy')
with:
github_token: ${{ github.token }}
labels: |
destroy