From 1b4cf9b38bdc0dec2d71e21e6d7a105bec91503c Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Wed, 22 Nov 2023 11:03:41 -0500 Subject: [PATCH] Test: continuing to test deploy action (allow on pull request too) (re: #1163) - This allows deploy action to run on pull requests too. - Also adjusts the environment variables and the BUILD_ID to make it easier to understand what the build is for. - It also embeds more information about the build in the Context. --- .github/workflows/deploy.yml | 30 +++++++++++++++++++++--------- dist/index.html | 4 +++- dist/latest.html | 4 +++- index.html | 4 +++- modules/Context.js | 8 ++++++-- scripts/predeploy.js | 27 +++++++++++++++------------ 6 files changed, 51 insertions(+), 26 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ddc1bee729..42e4e9f233 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,6 +6,8 @@ name: deploy on: push: branches: [ main, '*staging*', 'rapid-v1.x' ] + pull_request: + branches: [ main ] permissions: id-token: write # required to use OIDC authentication @@ -48,25 +50,35 @@ jobs: aws-region: us-west-2 - name: Setup environment vars 📋 - id: vars run: | - echo "YYYYMMDD=$(date +'%Y%m%d')" >> "$GITHUB_ENV" - echo "REVISION=$(git rev-parse --short ${{ github.sha }})" >> "$GITHUB_ENV" + echo "YYYYMMDD=$(date +'%Y%m%d')" >> "$GITHUB_ENV" + echo "BUILD_SHA=$(git rev-parse --short ${{ github.sha }})" >> "$GITHUB_ENV" - - name: Setup dist path 📋 + # On branch push, the BUILD_ID will be 'branch_name-revision', e.g. `main-d3adc0de` + # The revision is unique, so we save every one of these indefinitely. + - name: Setup BUILD_ID for push 🗂️ + if: ${{ github.event_name == 'push' }} run: | - echo "DISTPATH=${{ env.YYYYMMDD }}-${{ env.REVISION }}" >> "$GITHUB_ENV" + echo "BUILD_ID=${{ github.ref_name }}-${{ env.BUILD_SHA }}" >> "$GITHUB_ENV" + # On pull request, the BUILD_ID will be 'pull-request-#', e.g. `pull-request-1123` + # Subsequent changes to the pull request will overwrite this build. + - name: Setup BUILD_ID for pull request 🗂️ + if: ${{ github.event_name == 'pull_request' }} + run: | + echo "BUILD_ID=pull-request-${{ github.event.pull_request.number }}" >> "$GITHUB_ENV" + + # Rewrite the urls and embed build info in index.html - name: Predeploy index.html 🔨 run: node scripts/predeploy.js - name: Copy build to S3 📤 run: | - aws s3 cp dist s3://world.ai.rapid/rapid/${{ env.DISTPATH }} --no-progress --recursive - echo "Your build is here: https://rapideditor.org/rapid/${{ env.DISTPATH }}/index.html" + aws s3 sync dist s3://world.ai.rapid/rapid/${{ env.BUILD_ID }} --delete --only-show-errors + echo "Your build is here: https://rapideditor.org/rapid/${{ env.BUILD_ID }}/index.html" - name: Set as canary build 🐤 - if: ${{ github.ref_name == 'main' && github.event_name == 'push' }} + if: ${{ github.event_name == 'push' && github.ref_name == 'main' }} run: | - aws s3 cp dist/index.html s3://world.ai.rapid/canary --no-progress + aws s3 cp dist/index.html s3://world.ai.rapid/canary --only-show-errors echo "This is latest build on main, so it is available here: https://rapideditor.org/canary" \ No newline at end of file diff --git a/dist/index.html b/dist/index.html index ad3c2157e6..e6a3385cc5 100644 --- a/dist/index.html +++ b/dist/index.html @@ -45,7 +45,9 @@ const context = new Rapid.Context(); context.containerNode = container; context.assetPath = ''; - context.revision = ''; + context.buildID = ''; + context.buildSHA = ''; + context.buildDate = ''; context.apiConnections = [ { url: 'https://www.openstreetmap.org', diff --git a/dist/latest.html b/dist/latest.html index d76237bdd4..40699e6fb4 100644 --- a/dist/latest.html +++ b/dist/latest.html @@ -62,7 +62,9 @@ const context = new Rapid.Context(); context.containerNode = container; context.assetPath = 'https://cdn.jsdelivr.net/npm/@rapideditor/rapid@2/dist/'; - context.revision = ''; + context.buildID = ''; + context.buildSHA = ''; + context.buildDate = ''; context.apiConnections = [ { url: 'https://www.openstreetmap.org', diff --git a/index.html b/index.html index 59914a6c24..8e7cb858eb 100644 --- a/index.html +++ b/index.html @@ -45,7 +45,9 @@ const context = new Rapid.Context(); context.containerNode = container; context.assetPath = 'dist/'; - context.revision = ''; + context.buildID = ''; + context.buildSHA = ''; + context.buildDate = ''; context.apiConnections = [ { url: 'https://www.openstreetmap.org', diff --git a/modules/Context.js b/modules/Context.js index c6b5c6cf72..b61df1629b 100644 --- a/modules/Context.js +++ b/modules/Context.js @@ -29,9 +29,13 @@ export class Context extends EventEmitter { constructor() { super(); - this.version = '2.2.0b'; - this.revision = ''; this.privacyVersion = '20201202'; + this.version = '2.2.0-beta.0'; // see https://semver.org/ for examples + + // These may be set by our continuous deployment scripts, or left empty + this.buildID = ''; + this.buildSHA = ''; + this.buildDate = ''; this.maxCharsForTagKey = 255; this.maxCharsForTagValue = 255; diff --git a/scripts/predeploy.js b/scripts/predeploy.js index f553837bc3..dbcf3f0e61 100644 --- a/scripts/predeploy.js +++ b/scripts/predeploy.js @@ -2,7 +2,7 @@ import shell from 'shelljs'; // This script should normally only be run from a GitHub deploy action. -// It rewrites the urls in `index.html` to instead point to a unique 'distpath' in our S3 bucket. +// It rewrites the urls in `index.html` to instead point to a unique 'buildID' in our S3 bucket. // Then we can just copy this `index.html` around and things will just work. // This is how we do deploys of Rapid. @@ -12,21 +12,24 @@ const mm = ('0' + (now.getUTCMonth() + 1)).slice(-2); const dd = ('0' + now.getUTCDate()).slice(-2); // Get these things from environment (fallbacks for testing) -const yyyymmdd = process.env.YYYYMMDD ?? `${yyyy}${mm}${dd}`; -const revision = process.env.REVISION ?? 'deadc0de'; // normally the git short hash -const distpath = process.env.DISTPATH ?? `${yyyymmdd}-${revision}`; +const yyyymmdd = process.env.YYYYMMDD ?? `${yyyy}${mm}${dd}`; +const buildSHA = process.env.BUILD_SHA ?? 'deadc0de'; // normally the git short hash +const buildID = process.env.BUILD_ID ?? `local-${buildSHA}`; let file = 'dist/index.html'; +let path = `/rapid/${buildID}`; // If you want to test this script, uncomment these lines to copy 'index.html' instead of modifying it in place. //shell.cp('-f', 'dist/index.html', 'dist/index-copy.html'); //file = 'dist/index-copy.html'; -// Update `index.html` in place to use the distpath urls -shell.sed('-i', 'dist/', `/rapid/${distpath}/`, file); -shell.sed('-i', 'img/', `/rapid/${distpath}/img/`, file); -shell.sed('-i', 'rapid.css', `/rapid/${distpath}/rapid.css`, file); -shell.sed('-i', 'rapid.js', `/rapid/${distpath}/rapid.js`, file); -shell.sed('-i', 'rapid.min.js', `/rapid/${distpath}/rapid.min.js`, file); -shell.sed('-i', "context.assetPath = ''", `context.assetPath = '/rapid/${distpath}/'`, file); -shell.sed('-i', "context.revision = ''", `context.revision = '${revision}'`, file); +// Update `index.html` in place to use the buildID urls +shell.sed('-i', 'dist/', `${path}/`, file); +shell.sed('-i', 'img/', `${path}/img/`, file); +shell.sed('-i', 'rapid.css', `${path}/rapid.css`, file); +shell.sed('-i', 'rapid.js', `${path}/rapid.js`, file); +shell.sed('-i', 'rapid.min.js', `${path}/rapid.min.js`, file); +shell.sed('-i', /context.assetPath.*;/, `context.assetPath = '${path}/';`, file); +shell.sed('-i', /context.buildID.*;/, `context.buildID = '${buildID}';`, file); +shell.sed('-i', /context.buildSHA.*;/, `context.buildSHA = '${buildSHA}';`, file); +shell.sed('-i', /context.buildDate.*;/, `context.buildDate = '${yyyymmdd}';`, file);