-
Notifications
You must be signed in to change notification settings - Fork 0
251 lines (220 loc) · 12.2 KB
/
pr-preview.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
name: PR preview
on:
pull_request:
types: [opened, synchronize, closed]
paths:
- 'emit-ch4plume-v1/**'
- 'noaa-cpfp-point/**'
- 'nist-interface/**'
- 'goes-plume-viewer/**'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
S3_BUCKET: ghgc-custom-interfaces-develop
PR_STRING: pr-preview-${{ github.event.number }}
jobs:
build_and_deploy:
if: ${{ github.event.action != 'closed' }}
environment: development
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 'v20.9.0'
- name: Install Yarn
run: npm install -g yarn
- name: Determine changed files
id: changes
run: |
git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1
git diff --name-only origin/${{ github.event.pull_request.base.ref }} > changed_files.txt
cat changed_files.txt
# Initialize CHANGED_WEBSITES as an empty array
CHANGED_WEBSITES=()
# Check for changed folders and add them to the array
if grep -q 'emit-ch4plume-v1/' changed_files.txt; then
CHANGED_WEBSITES+=('emit-ch4plume-v1')
fi
if grep -q 'noaa-cpfp-point/' changed_files.txt; then
CHANGED_WEBSITES+=('noaa-cpfp-point')
fi
if grep -q 'nist-interface/' changed_files.txt; then
CHANGED_WEBSITES+=('nist-interface')
fi
if grep -q 'goes-plume-viewer/' changed_files.txt; then
CHANGED_WEBSITES+=('goes-plume-viewer')
fi
# Convert array to comma-separated string and set it as an environment variable
CHANGED_WEBSITES_STRING=$(IFS=, ; echo "${CHANGED_WEBSITES[*]}")
echo "CHANGED_WEBSITES=$CHANGED_WEBSITES_STRING" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.DEPLOYMENT_ROLE_ARN }}
role-session-name: ${{ github.repository_owner}}
aws-region: us-west-2
- name: Validate required variables and secrets
run: |
IFS=',' read -r -a WEBSITES <<< "${{ env.CHANGED_WEBSITES }}"
for WEBSITE in "${WEBSITES[@]}"; do
if [ "$WEBSITE" == "emit-ch4plume-v1" ]; then
missing_vars=()
# Check for required variables
[ -z "${{ vars.MAP_STYLE }}" ] && missing_vars+=("vars.MAP_STYLE")
[ -z "${{ secrets.MAP_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.MAP_ACCESS_TOKEN")
[ -z "${{ vars.PUBLIC_URL_EMIT }}" ] && missing_vars+=("vars.PUBLIC_URL_EMIT")
[ -z "${{ secrets.GEOAPIFY_APIKEY }}" ] && missing_vars+=("secrets.GEOAPIFY_APIKEY")
# If any variables are missing, print them and exit with an error
if [ ${#missing_vars[@]} -ne 0 ]; then
echo "Error: The following required variables are missing:"
printf '%s\n' "${missing_vars[@]}"
exit 1
fi
elif [ "$WEBSITE" == "noaa-cpfp-point" ]; then
missing_vars=()
# Check for required variables
[ -z "${{ vars.PUBLIC_URL_NOAA }}" ] && missing_vars+=("vars.PUBLIC_URL_NOAA")
[ -z "${{ secrets.MAP_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.MAP_ACCESS_TOKEN")
# If any variables are missing, print them and exit with an error
if [ ${#missing_vars[@]} -ne 0 ]; then
echo "Error: The following required variables are missing:"
printf '%s\n' "${missing_vars[@]}"
exit 1
fi
elif [ "$WEBSITE" == "nist-interface" ]; then
missing_vars=()
# Check for required variables
[ -z "${{ secrets.REACT_APP_MAPBOX_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.REACT_APP_MAPBOX_ACCESS_TOKEN")
[ -z "${{ secrets.REACT_APP_MAPBOX_STYLE_URL }}" ] && missing_vars+=("secrets.REACT_APP_MAPBOX_STYLE_URL")
[ -z "${{ vars.FEATURES_API_URL }}" ] && missing_vars+=("vars.FEATURES_API_URL")
# If any variables are missing, print them and exit with an error
if [ ${#missing_vars[@]} -ne 0 ]; then
echo "Error: The following required variables are missing:"
printf '%s\n' "${missing_vars[@]}"
exit 1
fi
elif [ "$WEBSITE" == "goes-plume-viewer" ]; then
missing_vars=()
# Check for required variables
[ -z "${{ secrets.REACT_APP_MAPBOX_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.REACT_APP_MAPBOX_TOKEN")
[ -z "${{ vars.REACT_APP_MAPBOX_STYLE_URL }}" ] && missing_vars+=("vars.REACT_APP_MAPBOX_STYLE_URL")
[ -z "${{ vars.REACT_APP_BASE_PATH_GOES }}" ] && missing_vars+=("vars.REACT_APP_BASE_PATH_GOES")
# If any variables are missing, print them and exit with an error
if [ ${#missing_vars[@]} -ne 0 ]; then
echo "Error: The following required variables are missing:"
printf '%s\n' "${missing_vars[@]}"
exit 1
fi
fi
done
shell: bash
- name: Build and deploy
run: |
IFS=',' read -r -a WEBSITES <<< "${{ env.CHANGED_WEBSITES }}"
COMMENT="### ✅ Deploy Preview for *custom-interfaces* is ready! \n \n"
for WEBSITE in "${WEBSITES[@]}"; do
if [ "$WEBSITE" == "emit-ch4plume-v1" ]; then
echo MAP_STYLE="${{ vars.MAP_STYLE }}" >> ./emit-ch4plume-v1/.env
echo MAP_ACCESS_TOKEN="${{ secrets.MAP_ACCESS_TOKEN }}" >> ./emit-ch4plume-v1/.env
echo PUBLIC_URL="${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}" >> ./emit-ch4plume-v1/.env
echo STAGE="production" >> ./emit-ch4plume-v1/.env
cd ./emit-ch4plume-v1
GEOAPIFY_APIKEY="${{ secrets.GEOAPIFY_APIKEY }}" node update_data.js
npm install
npm run deploy
cd ..
aws s3 sync emit-ch4plume-v1/dist s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}/
COMMENT+="- 🛰️ emit-ch4plume-v1: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}/index.html \n \n"
elif [ "$WEBSITE" == "noaa-cpfp-point" ]; then
echo PUBLIC_URL="${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}" >> ./noaa-cpfp-point/.env
echo MAPBOX_ACCESS_TOKEN="${{ secrets.MAP_ACCESS_TOKEN }}" >> ./noaa-cpfp-point/.env
cd ./noaa-cpfp-point
npm install
PUBLIC_URL="${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}" npm run production
cd ..
aws s3 sync noaa-cpfp-point/dist s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/
aws s3 sync noaa-cpfp-point/data s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/data/
COMMENT+="- 🗼 noaa-cpfp-point: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/index.html"
elif [ "$WEBSITE" == "nist-interface" ]; then
NIST_PATH=$(echo "${{ vars.PUBLIC_URL_NIST }}/${{ env.PR_STRING }}" | sed -E 's|^https?://[^/]+||; s|/*$|/|')
echo REACT_APP_MAPBOX_TOKEN="${{ secrets.REACT_APP_MAPBOX_ACCESS_TOKEN }}" >> ./nist-interface/.env
echo REACT_APP_MAPBOX_STYLE_URL="${{ secrets.REACT_APP_MAPBOX_STYLE_URL }}" >> ./nist-interface/.env
echo REACT_APP_FEATURES_API_URL="${{ vars.FEATURES_API_URL }}" >> ./nist-interface/.env
echo REACT_APP_PUBLIC_URL="${NIST_PATH}" >> ./nist-interface/.env
URL_VALUE=$(grep "REACT_APP_PUBLIC_URL" ./nist-interface/.env)
echo "The value of REACT_APP_PUBLIC_URL is: ${URL_VALUE}"
cd ./nist-interface
yarn
yarn build
cd ..
aws s3 sync nist-interface/build s3://${{ env.S3_BUCKET }}${NIST_PATH}
COMMENT+="- 🗼 nist-interface: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${NIST_PATH}index.html"
elif [ "$WEBSITE" == "goes-plume-viewer" ]; then
GOES_PATH=$(echo "${{ vars.PUBLIC_URL_GOES }}/${{ env.PR_STRING }}" | sed -E 's|^https?://[^/]+||; s|/*$|/|')
echo >> ./goes-plume-viewer/.env # add a new line to append the below variables
echo REACT_APP_MAPBOX_TOKEN="${{secrets.REACT_APP_MAPBOX_ACCESS_TOKEN}}" >> ./goes-plume-viewer/.env
echo REACT_APP_MAPBOX_STYLE_URL="${{vars.REACT_APP_MAPBOX_STYLE_URL}}" >> ./goes-plume-viewer/.env
echo REACT_APP_BASE_PATH="${{vars.REACT_APP_BASE_PATH_GOES}}" >> ./goes-plume-viewer/.env
echo REACT_APP_PUBLIC_URL="${GOES_PATH}" >> ./goes-plume-viewer/.env
URL_VALUE=$(grep "REACT_APP_PUBLIC_URL" ./goes-plume-viewer/.env)
echo "The value of REACT_APP_PUBLIC_URL is: ${URL_VALUE}"
cd ./goes-plume-viewer
yarn
CI=false yarn build
cd ..
aws s3 sync goes-plume-viewer/build s3://${{ env.S3_BUCKET }}${GOES_PATH}
COMMENT+="- 🛰️ goes-plume-viewer: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${GOES_PATH}index.html"
fi
echo "COMMENT=${COMMENT}" >> $GITHUB_ENV
done
- name: Add/update PR comment
id: find-comment
run: |
COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
COMMENTS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" $COMMENTS_URL)
COMMENT_ID=$(echo $COMMENTS | jq -r '.[] | select(.user.login == "github-actions[bot]") | .id')
if [ -z "$COMMENT_ID" ]; then
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -X POST -d "{\"body\":\"${{ env.COMMENT }}\"}" "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
else
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -X PATCH -d "{\"body\":\"${{ env.COMMENT }}\"}" "https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
clean_up:
if: ${{ github.event.action == 'closed' }}
environment: development
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
pull-requests: write
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.DEPLOYMENT_ROLE_ARN }}
role-session-name: ${{ github.repository_owner }}
aws-region: us-west-2
- name: Delete built websites from S3
run: |
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}/ --recursive
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/ --recursive
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NIST }}/${{ env.PR_STRING }}/ --recursive
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_GOES }}/${{ env.PR_STRING }}/ --recursive
- name: Remove PR comment
run: |
COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
COMMENTS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" $COMMENTS_URL)
COMMENT_ID=$(echo $COMMENTS | jq -r '.[] | select(.user.login == "github-actions[bot]") | .id')
if [ -n "$COMMENT_ID" ]; then
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -X DELETE -d "{\"body\":\"${{ env.COMMENT }}\"}" "https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}