Skip to content

V2 Deploy PR

V2 Deploy PR #2547

Workflow file for this run

name: V2 Deploy PR
on:
workflow_run:
workflows:
- "V2 Build PR"
types:
- completed
permissions:
actions: read
pull-requests: write
contents: read
concurrency:
group: deploy-v2-demos-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
jobs:
# Step 1: Prepare the build context
prepare-build-context:
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- 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 }}
- 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@v7
with:
script: |
const fs = require('fs');
const buildData = fs.readFileSync('pr_info.json', 'utf8');
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: |
echo '${{ steps.read_build_info.outputs.result }}' | jq -r '.id' > pr_id.txt
echo '${{ steps.read_build_info.outputs.result }}' | jq -r '.ref' > pr_ref.txt
echo '${{ steps.read_build_info.outputs.result }}' | jq -r '.ref_name' > pr_ref_name.txt
echo '${{ steps.read_build_info.outputs.result }}' | jq -c '.updated_demos' > updated_demos.json
echo '${{ steps.read_build_info.outputs.result }}' | jq -c '.deleted_demos' > deleted_demos.json
echo "pr_id=$(cat pr_id.txt)" >> $GITHUB_OUTPUT
echo "pr_ref=$(cat pr_ref.txt)" >> $GITHUB_OUTPUT
echo "pr_ref_name=$(cat pr_ref_name.txt)" >> $GITHUB_OUTPUT
echo "updated_demos=$(cat updated_demos.json)" >> $GITHUB_OUTPUT
echo "deleted_demos=$(cat deleted_demos.json)" >> $GITHUB_OUTPUT
outputs:
pr_id: ${{ steps.pr_info.outputs.pr_id }}
pr_ref: ${{ steps.pr_info.outputs.pr_ref }}
pr_ref_name: ${{ steps.pr_info.outputs.pr_ref_name }}
updated_demos: ${{ steps.pr_info.outputs.updated_demos }}
deleted_demos: ${{ steps.pr_info.outputs.deleted_demos }}
# Step 2: Deploy the demos to SWC
deploy-preview-demos:
if: |
github.event.workflow_run.event == 'pull_request' &&
needs.prepare-build-context.result == 'success' &&
needs.prepare-build-context.outputs.pr_ref != '' &&
needs.prepare-build-context.outputs.pr_id != '' &&
needs.prepare-build-context.outputs.updated_demos != '[]'
uses: ./.github/workflows/v2-deploy-demos.yml
needs: prepare-build-context
with:
environment: 'swc-prod'
artifact-name: demo-build-${{ needs.prepare-build-context.outputs.pr_ref }}
workflow-run-id: ${{ github.event.workflow_run.id }}
pr_number: ${{ fromJson(needs.prepare-build-context.outputs.pr_id) }}
secrets: inherit
# Step 3: Create a comment on the PR with the demo links
# If build is successful, generate a comment with the demo link.
# If build fails, generate a comment indicating the failure.
generate-comment:
if: github.event.workflow_run.event == 'pull_request' && needs.prepare-build-context.outputs.pr_id != ''
runs-on: ubuntu-latest
needs: [prepare-build-context, deploy-preview-demos]
steps:
- name: Generate Markdown Comment for Successful Deployment
if: needs.deploy-preview-demos.result == 'success'
id: generate-markdown
run: |
comment="### Your preview is ready :tada:!\n\n"
comment+="You can view your changes [here](https://pennylane.ai/qml/demonstrations?pr=${{ needs.prepare-build-context.outputs.pr_id }})\n\n"
comment+="> Deployed at: $(date +'%Y-%m-%d %H:%M:%S') UTC\n\n"
echo "markdown=$comment" >> $GITHUB_OUTPUT
- name: Generate Markdown Comment for Failed Deployment
if: needs.deploy-preview-demos.result != 'success'
id: generate-failure-markdown
run: |
comment="### Deployment failed :x:\n\n"
comment+="There was an issue deploying your changes. Please check the related `v2-deploy-pr` logs for more details.\n\n"
comment+="If you need assistance, please reach out to the team.\n\n"
comment+="> Attempted deployment at: $(date +'%Y-%m-%d %H:%M:%S') UTC\n\n"
echo "markdown=$comment" >> $GITHUB_OUTPUT
- name: Comment on PR
id: comment-on-pr
uses: XanaduAI/cloud-actions/create-and-update-pull-request-comment@main
with:
github_token: ${{ secrets.github_token }}
pull_request_number: ${{ needs.prepare-build-context.outputs.pr_id }}
comment_body: ${{ steps.generate-markdown.outputs.markdown }}