-
Notifications
You must be signed in to change notification settings - Fork 188
218 lines (189 loc) · 9.64 KB
/
deploy-pr.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
name: Deploy Website
on:
workflow_run:
workflows:
- Build Website
types:
- completed
jobs:
deploy:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
steps:
# Needed to Built pennylane.ai-react
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
- name: Download Build Context
uses: XanaduAI/cloud-actions/download-github-workflow-artifact@main
with:
workflow_run_id: ${{ github.event.workflow_run.id }}
artifact_name_regex: '^pr_info$'
github_token: ${{ github.token }}
# If file is missing, then likely the PR build was skipped
- name: Check if Build Context file exists
id: build_context
env:
context_artifact_file_name: pr_info.zip
run: |
if test -f "$context_artifact_file_name"; then
echo "result=$context_artifact_file_name" >> $GITHUB_OUTPUT
fi
- name: Unpack Build Information
if: steps.build_context.outputs.result != ''
run: |
unzip ${{ steps.build_context.outputs.result }}
- name: Read Build Information
id: read_build_info
if: steps.build_context.outputs.result != ''
uses: actions/github-script@v6
with:
script: |
let fs = require('fs');
const buildData = fs.readFileSync('pr_info.json');
return JSON.parse(buildData);
- name: Parse Pull Request Event Information
id: pr_info
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
run: |
PR_ID=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.id')
PR_ID_NO_QUOTE="${PR_ID%\"}"
PR_ID_NO_QUOTE="${PR_ID_NO_QUOTE#\"}"
echo "pr_id => $PR_ID_NO_QUOTE"
echo "pr_id=$PR_ID_NO_QUOTE" >> $GITHUB_OUTPUT
echo "pr_site=https://${{ secrets.AWS_WEBSITE }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/$PR_ID_NO_QUOTE/qml/demonstrations/" >> $GITHUB_OUTPUT
PR_REF=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.ref')
PR_REF_NO_QUOTE="${PR_REF%\"}"
PR_REF_NO_QUOTE="${PR_REF_NO_QUOTE#\"}"
echo "pr_ref => $PR_REF_NO_QUOTE"
echo "pr_ref=$PR_REF_NO_QUOTE" >> $GITHUB_OUTPUT
PR_REF_NAME=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.ref_name')
PR_REF_NAME_NO_QUOTE="${PR_REF_NAME%\"}"
PR_REF_NAME_NO_QUOTE="${PR_REF_NAME_NO_QUOTE#\"}"
echo "pr_ref_name => $PR_REF_NAME_NO_QUOTE"
echo "pr_ref_name=$PR_REF_NAME_NO_QUOTE" >> $GITHUB_OUTPUT
- name: Create GitHub Deployment
id: deployment
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: XanaduAI/cloud-actions/manage-gh-deployments@main
with:
environment: preview
deployment_stage: in_progress
deployment_ref: ${{ steps.pr_info.outputs.pr_ref }}
deployment_description: QML doc deployment from pull request ${{ steps.pr_info.outputs.pr_id }}
deployment_log_url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
deployment_url: ${{ steps.pr_info.outputs.pr_site }}
- name: Clone PennyLane.ai-react
if: steps.build_context.outputs.result != ''
uses: actions/checkout@v4
with:
repository: XanaduAI/pennylane.ai-react
ref: main
path: pennylane.ai-react
token: ${{ secrets.PL_SITE_DEV_QML_REACT_PAT }}
- name: Clone QML
if: steps.build_context.outputs.result != ''
uses: actions/checkout@v4
with:
ref: ${{ steps.pr_info.outputs.pr_ref }} # This is the current commit SHA at HEAD of branch of the pull request
path: qml-${{ steps.pr_info.outputs.pr_ref_name }}
# We will not build the entire pennylane website as part of this pipline
# This step will update the pages so that only /qml/* is built
- name: Setup PennyLane.ai-react
if: steps.build_context.outputs.result != ''
env:
QML_REPO_DIR: qml-${{ steps.pr_info.outputs.pr_ref_name }}
run: |
rm pennylane.ai-react/apps/pennylane-website/gatsby-node.ts
cp "${QML_REPO_DIR}/.github/workflows/gatsby-node.ts" pennylane.ai-react/apps/pennylane-website/gatsby-node.ts
cd pennylane.ai-react/apps/pennylane-website/src/pages
# Find all files under apps/pennylane-website/src/pages and deletes them (exception for 404 page and ./qml/ directory)
find . -maxdepth 1 -not -name '404.tsx' -not -path '.' -not -path './qml' -not -path './qml/*' -exec rm -rf "{}" \;
# Copy the index file from /qml/index.tsx one directory higher
cp qml/index.tsx ./index.tsx
# Update import links in the newly copied index.tsx
# This line updates `../../` -> to -> `../`. Removes one of the `../`
# This is needed since the file has been copied one directory higher
sed -i "s/'..\/..\//'..\//" index.tsx
- name: Download HTML
if: steps.build_context.outputs.result != ''
uses: XanaduAI/cloud-actions/download-github-workflow-artifact@main
with:
workflow_run_id: ${{ github.event.workflow_run.id }}
artifact_name_regex: ^html\-.*$
github_token: ${{ github.token }}
- name: Unpack HTML
if: steps.build_context.outputs.result != ''
env:
DEMOS_UNPACK_DIR: pennylane.ai-react/apps/pennylane-website/demos_data
run: |
mkdir -p $DEMOS_UNPACK_DIR
for f in html-*.zip; do
unzip -o -d $DEMOS_UNPACK_DIR $f
done
- name: Copy over metadata files from pull request
if: steps.build_context.outputs.result != ''
env:
METADATA_SOURCE_DIR: ${{ github.workspace }}/qml-${{ steps.pr_info.outputs.pr_ref_name }}
METADATA_DEST_DIR: ${{ github.workspace }}/pennylane.ai-react/apps/pennylane-website/demos_data/metadata
run: |
mkdir "$METADATA_DEST_DIR"
cd "$METADATA_SOURCE_DIR"
find demonstrations -name "*.metadata.json" -type f | xargs cp -vt $METADATA_DEST_DIR
ls -ltrh "$METADATA_DEST_DIR"
- name: Setup Demos and Build
if: steps.build_context.outputs.result != ''
id: qml_demos_build
env:
BASE_PATH: /${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/${{ steps.pr_info.outputs.pr_id }}
run: |
cd pennylane.ai-react
yarn install
yarn setup-demos
yarn build-pr-preview --filter=pennylane-website...
echo 'build_dir=pennylane.ai-react/apps/pennylane-website/public' >> $GITHUB_OUTPUT
- name: Upload HTML (Pull Request)
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run:
aws s3 sync ${{ steps.qml_demos_build.outputs.build_dir }} s3://${{ secrets.AWS_PR_S3_BUCKET_ID }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/${{ steps.pr_info.outputs.pr_id }}/ --delete
- name: Comment on PR
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: XanaduAI/cloud-actions/create-and-update-pull-request-comment@main
with:
github_token: ${{ github.token }}
pull_request_number: ${{ steps.pr_info.outputs.pr_id }}
comment_body: |
**Thank you for opening this pull request.**
You can find the built site [at this link](${{ steps.pr_info.outputs.pr_site }}).
**Deployment Info:**
- Pull Request ID: \`${{ steps.pr_info.outputs.pr_id }}\`
- Deployment SHA: \`${{ steps.pr_info.outputs.pr_ref }}\`
(The \`Deployment SHA\` refers to the latest commit hash the docs were built from)
**Note:** It may take several minutes for updates to this pull request to be reflected on the deployed site.
- name: Update Deployment (success)
if: success() && github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: XanaduAI/cloud-actions/manage-gh-deployments@main
with:
environment: preview
deployment_stage: success
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
deployment_ref: ${{ steps.pr_info.outputs.pr_ref }}
deployment_description: QML doc deployment from pull request ${{ steps.pr_info.outputs.pr_id }}
deployment_log_url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
deployment_url: ${{ steps.pr_info.outputs.pr_site }}
- name: Update Deployment (failure)
if: failure() && github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: XanaduAI/cloud-actions/manage-gh-deployments@main
with:
environment: preview
deployment_stage: failure
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
deployment_ref: ${{ steps.pr_info.outputs.pr_ref }}
deployment_description: QML doc deployment from pull request ${{ steps.pr_info.outputs.pr_id }}
deployment_log_url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
deployment_url: ${{ steps.pr_info.outputs.pr_site }}