Skip to content

Commit

Permalink
Updated GitHub Actions workflow to update dev / prod QML sites on mer…
Browse files Browse the repository at this point in the history
…ge (#498)

* feat: Added ability to deploy to prod / dev environments

* Fixed incorrect secret for build dir
  • Loading branch information
rashidnhm authored May 26, 2022
1 parent ab09f3c commit 9816a6d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
NUM_WORKERS: 15

concurrency:
group: ${{ github.ref }}
group: build-docs-${{ github.ref }}
cancel-in-progress: true

jobs:
Expand Down Expand Up @@ -141,7 +141,9 @@ jobs:
uses: actions/cache@v3
with:
path: demos
key: gallery-${{ hashFiles('matrix_info.txt') }}-${{ github.ref_name }}
key: gallery-${{ hashFiles('matrix_info.txt') }}-${{ github.ref_name }}-${{ github.sha }}
restore-keys: |
gallery-${{ hashFiles('matrix_info.txt') }}-${{ github.ref_name }}-
- name: Sphinx Cache
uses: actions/cache@v3
Expand Down Expand Up @@ -181,7 +183,7 @@ jobs:
--verbose
- name: Upload Html
if: github.event_name == 'pull_request' && matrix.offset == 0
if: matrix.offset == 0
uses: actions/upload-artifact@v3
with:
name: html-${{ matrix.offset }}.zip
Expand All @@ -194,7 +196,7 @@ jobs:
# built artifact. This is done as a performance boost.
# The step above this is executed by only one worker which uploads all static content.
- name: Upload Demo Html
if: github.event_name == 'pull_request' && matrix.offset != 0
if: matrix.offset != 0
uses: actions/upload-artifact@v3
with:
name: html-${{ matrix.offset }}.zip
Expand Down
49 changes: 43 additions & 6 deletions .github/workflows/deploy-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ on:
jobs:
deploy:
runs-on: ubuntu-20.04
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
if: github.event.workflow_run.conclusion == 'success'
steps:
- name: Download PR Info
if: github.event.workflow_run.event == 'pull_request'
uses: actions/github-script@v6
with:
script: |
Expand All @@ -33,11 +34,13 @@ jobs:
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_info.zip`, Buffer.from(download.data));
- name: Unpack PR Info
if: github.event.workflow_run.event == 'pull_request'
run: |
unzip pr_info.zip
- name: Read PR Info
id: read_pr_info
if: github.event.workflow_run.event == 'pull_request'
uses: actions/github-script@v6
with:
script: |
Expand All @@ -47,12 +50,13 @@ jobs:
- name: Parse PR Info
id: pr_info
if: github.event.workflow_run.event == 'pull_request'
run: |
PR_ID=$(echo '${{ steps.read_pr_info.outputs.result }}' | jq '.id')
PR_ID_NO_QUOTE="${PR_ID%\"}"
PR_ID_NO_QUOTE="${PR_ID_NO_QUOTE#\"}"
echo "::set-output name=pr_id::$PR_ID_NO_QUOTE"
echo "::set-output name=pr_site::https://${{ secrets.AWS_WEBSITE }}/${{ secrets.AWS_BUCKET_BUILD_DIR }}/$PR_ID_NO_QUOTE/index.html"
echo "::set-output name=pr_site::https://${{ secrets.AWS_WEBSITE }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/$PR_ID_NO_QUOTE/index.html"
PR_REF=$(echo '${{ steps.read_pr_info.outputs.result }}' | jq '.ref')
PR_REF_NO_QUOTE="${PR_REF%\"}"
Expand All @@ -66,6 +70,7 @@ jobs:
- name: Get Build IDs
id: build_ids
if: github.event.workflow_run.event == 'pull_request'
uses: actions/github-script@v6
with:
script: |
Expand All @@ -82,8 +87,17 @@ jobs:
});
return buildIds
- name: Get Branch name (on Push)
id: push_branch_info
if: github.event.workflow_run.event == 'push'
run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/}
echo "${BRANCH_NAME^^}"
echo "::set-output name=branch_name::${BRANCH_NAME^^}"
- name: Create Deployment
id: deployment
if: github.event.workflow_run.event == 'pull_request'
uses: actions/github-script@v6
with:
result-encoding: string
Expand Down Expand Up @@ -180,15 +194,38 @@ jobs:
unzip -o -d website $f
done
- name: Upload HTML
- name: Upload HTML (Pull Request)
if: github.event.workflow_run.event == 'pull_request'
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 website s3://${{ secrets.AWS_BUCKET_ID }}/${{ secrets.AWS_BUCKET_BUILD_DIR }}/${{ steps.pr_info.outputs.pr_id }}/ --delete
aws s3 sync website s3://${{ secrets.AWS_PR_S3_BUCKET_ID }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/${{ steps.pr_info.outputs.pr_id }}/ --delete

- name: Upload HTML (Push to master / dev)
if: github.event.workflow_run.event == 'push'
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
BUCKET_ACTIONS_ID: ${{ format('AWS_ENV_S3_BUCKET_{0}', steps.push_branch_info.outputs.branch_name) }}
run: |
echo "Got actions Bucket ID: ${{ env.BUCKET_ACTIONS_ID }}"
AWS_BUCKET_NAME=${{ secrets[env.BUCKET_ACTIONS_ID] }}
aws s3 sync website s3://$AWS_BUCKET_NAME/qml/ --delete
- name: Invalidate Cloudfront cache (Prod)
if: github.event.workflow_run.event == 'push' && steps.push_branch_info.outputs.branch_name == 'master'
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 cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths /qml/*
- name: Comment on PR
if: github.event.workflow_run.event == 'pull_request'
uses: actions/github-script@v6
with:
script: |
Expand Down Expand Up @@ -238,7 +275,7 @@ jobs:
}
- name: Update Deployment (success)
if: success()
if: success() && github.event.workflow_run.event == 'pull_request'
uses: actions/github-script@v6
env:
DEPLOYMENT_STAGE: success
Expand All @@ -259,7 +296,7 @@ jobs:
});
- name: Update Deployment (failure)
if: failure()
if: failure() && github.event.workflow_run.event == 'pull_request'
uses: actions/github-script@v6
env:
DEPLOYMENT_STAGE: failure
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/teardown-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ jobs:
- name: Check if deployment exists
id: check_deployment
run: |
num_obj=$(aws s3 ls s3://${{ secrets.AWS_BUCKET_ID }}/${{ secrets.AWS_BUCKET_BUILD_DIR }}/${{ github.event.pull_request.number }}/index.html --summarize | grep "Total Objects: " | sed 's/[^0-9]*//g')
num_obj=$(aws s3 ls s3://${{ secrets.AWS_PR_S3_BUCKET_ID }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/${{ github.event.pull_request.number }}/index.html --summarize | grep "Total Objects: " | sed 's/[^0-9]*//g')
echo "::set-output name=exists::$num_obj"
- name: Teardown
if: steps.check_deployment.outputs.exists != '0'
run: |
aws s3 rm s3://${{ secrets.AWS_BUCKET_ID }}/${{ secrets.AWS_BUCKET_BUILD_DIR }}/${{ github.event.pull_request.number }} --recursive
aws s3 rm s3://${{ secrets.AWS_PR_S3_BUCKET_ID }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/${{ github.event.pull_request.number }} --recursive
- name: Deactivate Deployment
uses: actions/github-script@v6
Expand Down

0 comments on commit 9816a6d

Please sign in to comment.