-
Notifications
You must be signed in to change notification settings - Fork 0
158 lines (136 loc) · 5.79 KB
/
deploy-bundle-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
name: Deploy Bundle Preview
on:
pull_request:
branches:
- '*'
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Configure Git Credentials
uses: de-vri-es/setup-git-credentials@v2
with:
credentials: ${{ secrets.GIT_CREDENTIALS }}
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16.20.2
- name: Install Dependencies
run: npm ci
- name: Extract Branch Names
shell: bash
run: |
# transform branch names in form of `refs/heads/main` to `main`
draft_branch=$(basename ${{ github.event.pull_request.head.ref }})
echo "draft_branch=$draft_branch" >> $GITHUB_OUTPUT
id: extract_branch
- name: Build UI Bundle Preview
run: |
set -o pipefail
gulp lint |& tee $GITHUB_WORKSPACE/build.log
UI_ROOT_PATH_PREFIX=${{ github.event.repository.name }}/${{ steps.extract_branch.outputs.draft_branch }} \
gulp preview:build |& tee $GITHUB_WORKSPACE/build.log
- name: Check Build Result
id: logFail
if: failure()
run: |
MULTILINE_LOG=$(cat $GITHUB_WORKSPACE/build.log)
echo "BUILD_FAILURE<<EOF" >> $GITHUB_ENV
echo $MULTILINE_LOG >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Create Build Success Comment
if: ${{ success() && github.event.pull_request.number }}
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body: |
UI bundle preview build successful! :white_check_mark:
Deploying preview to GitHub Pages.
reactions: rocket
- name: Create Build Failure Comment
if: ${{ failure() && github.event.pull_request.number }}
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body: |
UI bundle preview build failure! :x:
> ${{ env.BUILD_FAILURE }}
reactions: confused
- name: Find Comment
if: ${{ success() && github.event.pull_request.number }}
uses: peter-evans/find-comment@v2
id: fc
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'mlr'
body-includes: UI bundle preview build successful!
direction: last
- name: Deploy to GitHub Pages
if: success()
run: |
git clone https://github.com/$GITHUB_REPOSITORY.git pages
cd pages
git checkout gh-pages
# If there was previously a build for the preview, then remove it
# so we get a clean build. This is needed in case a follow up
# build of the same pull request contains content deletions.
rm -rf ${{ steps.extract_branch.outputs.draft_branch }}
mkdir -p ${{ steps.extract_branch.outputs.draft_branch }}
cp -r ../public/* ${{ steps.extract_branch.outputs.draft_branch }}/.
# Records the repository that originally triggered the build so we can post back
# comments upon clean up of a stale draft if it still has an open pull request.
echo "${{ github.event.repository.full_name }}" > ${{ steps.extract_branch.outputs.draft_branch }}/.github_source_repository
git add .
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git commit --allow-empty -m "Auto-deployed from GitHub Actions"
git push -u origin gh-pages
- name: Obtain GitHub Pages build URL
if: success()
run: |
sleep 5 # Allow time for build to initiate
build_url=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.COMMENT_GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" 'https://api.github.com/repos/${{ github.event.repository.full_name }}/pages/builds/latest' \
| jq -r .url)
echo "url=$build_url" >> $GITHUB_OUTPUT
id: ghpages_build
- name: Wait for Github Pages deployment
if: success()
run: |
for i in {1..60}; do
build_status=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.COMMENT_GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" '${{ steps.ghpages_build.outputs.url }}' \
| jq -r .status)
if [ "$build_status" == "built" ]; then echo "Deploy is complete."
exit 0
else
echo "Deploy is not complete. Status: $build_status. Retrying in 10 seconds..."
sleep 10
fi
done
echo "Deploy is still not complete after approximately 10 minutes."
exit 1
- name: Get GitHub Pages Preview URL
if: success()
shell: bash
run: |
echo "url=https://riptano.github.io/${{ github.event.repository.name }}/${{ steps.extract_branch.outputs.draft_branch }}/" >> $GITHUB_OUTPUT
id: draft_url
- name: Update comment
if: ${{ steps.fc.outputs.comment-id != '' }}
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ secrets.COMMENT_GITHUB_TOKEN }}
comment-id: ${{ steps.fc.outputs.comment-id }}
body: |
Deployment successful! [View preview](${{ steps.draft_url.outputs.url }})
reactions: hooray