Skip to content

Commit

Permalink
Test: continuing to test deploy action (allow on pull request too)
Browse files Browse the repository at this point in the history
(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.
  • Loading branch information
bhousel committed Nov 22, 2023
1 parent e9678b4 commit 1b4cf9b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
30 changes: 21 additions & 9 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
4 changes: 3 additions & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion dist/latest.html
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 6 additions & 2 deletions modules/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 15 additions & 12 deletions scripts/predeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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);

0 comments on commit 1b4cf9b

Please sign in to comment.