This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 185
210 lines (192 loc) · 9.15 KB
/
generate-preview-link.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
name: Generate preview link
permissions:
actions: write
checks: write
contents: write
deployments: write
pull-requests: write
statuses: write
on:
workflow_run:
workflows: ['Pre-generate preview link']
types:
- completed
env:
NODE_OPTIONS: '--max-old-space-size=8192'
concurrency:
group: cloudflare-pages-build-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
jobs:
build_to_cloudflare_pages:
runs-on: Ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: Checkout to repo
uses: actions/checkout@v3
with:
ref: ${{ github.event.workflow_run.head_sha }}
- name: Download artifact
id: download-artifact
uses: dawidd6/action-download-artifact@v2
with:
workflow_conclusion: success
run_id: ${{ github.event.workflow_run.id }}
name: 'pr-${{ github.event.workflow_run.id }}'
path: .pr
- name: Retrieve pull request
id: pr_information
run: |
echo "issue_number=$(cat .pr/NR)" >> $GITHUB_OUTPUT
- name: 'Generate action link comment'
id: generate_action_url
uses: actions/github-script@v3
with:
github-token: ${{ github.token }}
script: |
const action_url = "${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}"
const comment = [
'| Name | Result |',
'| :--- | :------ |',
`| **Build status** | Building 🔨 |`,
`| **Action URL** | [Visit Action](${action_url}) |`,
''
].join('\n')
core.setOutput("comment", comment);
- name: Post Cloudflare Pages Preview comment
uses: marocchino/sticky-pull-request-comment@v2
with:
header: Cloudflare Pages Preview Comment
number: ${{steps.pr_information.outputs.issue_number}}
message: ${{steps.generate_action_url.outputs.comment}}
recreate: true
- name: Verify user organization
id: verify_user_organization
run: |
echo "Verifying user's organization..."
user=$(cat .pr/USERNAME)
response=$(curl -s -L \
-w "%{http_code}" \
-o /dev/null -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.PERSONAL_ACCESS_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/orgs/binary-com/memberships/$user")
if [ $response != "200" ]; then
echo "User is not a member of binary-com organization."
exit 1
else
echo "User is a member of binary-com organization."
echo "Proceeding to build and deploy for the pull request: https://github.com/binary-com/deriv-com/pull/$(cat ./NR)"
fi
- name: Setup node
uses: actions/setup-node@v2
- name: Create npmrc file
shell: bash
run: echo "@deriv-com:registry=https://npm.pkg.github.com" >> .npmrc
- name: Setup install read-only token for deriv-com org
shell: bash
run: echo '//npm.pkg.github.com/:_authToken=${{ secrets.READ_DERIV_COM_ORG_PACKAGES }}' >> .npmrc
- name: Get build output from master cache
uses: actions/cache/restore@v3
with:
key: master-cache-public
restore-keys: |
master-cache-public-replica
path: |
.cache
public
- name: Get cached dependencies
id: cache-npm
uses: actions/cache/restore@v3
with:
path: node_modules
key: npm-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
run: npm ci
- name: Build project
id: build-project
env:
GATSBY_ENV: staging
GATSBY_GROWTHBOOK_CLIENT_KEY: ${{ secrets.GATSBY_GROWTHBOOK_CLIENT_KEY }}
GATSBY_GROWTHBOOK_DECRYPTION_KEY: ${{ secrets.GATSBY_GROWTHBOOK_DECRYPTION_KEY }}
GATSBY_RUDDERSTACK_STAGING_KEY: ${{ secrets.GATSBY_RUDDERSTACK_STAGING_KEY }}
GATSBY_RUDDERSTACK_PRODUCTION_KEY: ${{ secrets.GATSBY_RUDDERSTACK_PRODUCTION_KEY }}
GATSBY_STRAPI_TOKEN: ${{ secrets.GATSBY_STRAPI_TOKEN }}
GATSBY_GOOGLE_TAG_MANAGER_TRACKING_ID: ${{ secrets.GATSBY_GOOGLE_TAG_MANAGER_TRACKING_ID }}
GATSBY_TRUSTPILOT_API_KEY: ${{ secrets.GATSBY_TRUSTPILOT_API_KEY }}
GATSBY_HOTJAR_ID: ${{ secrets.GATSBY_HOTJAR_ID }}
run: npm run build
- name: Publish to Cloudflare Pages
id: publish-to-pages
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_TEST_LINKS_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_TEST_LINKS_ACCOUNT_ID }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
run: |
echo "Installing Wrangler CLI"
npm i -g wrangler
echo "Deploying build to Cloudflare Pages"
directory='public'
projectName='deriv-com-preview-links'
branch=$(echo "$HEAD_BRANCH" | head -c 20 | sed 's/[\/_\.]/-/g; s/[^a-zA-Z0-9]$/1/')
cf_preview_url=$(wrangler pages deploy $directory --project-name=$projectName --branch=$branch > log.txt 2>&1; echo $?)
echo "------"
preview_url=https://$branch.deriv-com-preview-links.pages.dev
cat log.txt
if grep -q "Deployment complete" log.txt; then
echo "preview_url=$preview_url" >> "$GITHUB_OUTPUT"
echo $preview_url > .pr/PREVIEW_URL
else
echo "Deployment to Cloudflare Pages failed."
exit 1
fi
- name: 'Generate preview link comment'
if: success()
id: generate_preview_url
uses: actions/github-script@v3
with:
github-token: ${{ github.token }}
script: |
const action_url = "${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}"
const preview_url = "${{steps.publish-to-pages.outputs.preview_url}}"
const comment = [
`**Preview Link**: ${preview_url}`,
'| Name | Result |',
'| :--- | :------ |',
`| **Build status** | Completed ✅ |`,
`| **Preview URL** | [Visit Preview](${preview_url}) |`,
`| **Action URL** | [Visit Action](${action_url}) |`,
''
].join('\n')
core.setOutput("comment", comment);
- name: 'Generate failure comment'
if: failure()
id: generate_failure_comment
uses: actions/github-script@v3
with:
github-token: ${{ github.token }}
script: |
const action_url = "${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}"
const comment = [
'| Name | Result |',
'| :--- | :------ |',
`| **Build status** | Failed ❌ |`,
`| **Action URL** | [Visit Action](${action_url}) |`,
''
].join('\n')
core.setOutput("comment", comment);
- name: Post Cloudflare Pages Preview comment
if: success() || failure()
uses: marocchino/sticky-pull-request-comment@v2
with:
header: Cloudflare Pages Preview Comment
number: ${{steps.pr_information.outputs.issue_number}}
message: ${{steps.generate_preview_url.outputs.comment || steps.generate_failure_comment.outputs.comment }}
recreate: true
- name: Upload PR information to artifact
uses: actions/upload-artifact@v2
with:
name: 'pr-${{github.run_id}}'
path: .pr