Skip to content

Deploy Website

Deploy Website #7426

Workflow file for this run

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@v3
with:
node-version: 18
- 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 }}