Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2: Fix GitHub Actions deploy #447

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
cd ../codap-data-interactives/onboarding
npm install
cd ../../codap
npm run build:travis
npm run build:ci
# - name: Run Tests
# run: npm run test -- --runInBand
# cypress:
Expand Down Expand Up @@ -92,7 +92,13 @@ jobs:
gem install s3_website -v 3.4.0
npm ci --legacy-peer-deps
- name: Build
run: npm run build
run: |
git clone https://github.com/concord-consortium/codap-data.git ../codap-data
git clone https://github.com/concord-consortium/codap-data-interactives.git ../codap-data-interactives
cd ../codap-data-interactives/onboarding
npm install
cd ../../codap
npm run build:ci
- name: Deploy
run: ./bin/s3_deploy.sh
env:
Expand Down
118 changes: 89 additions & 29 deletions bin/s3_deploy.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,93 @@
#!/bin/sh
SRC_DIR='dist/travis'
DISTRIBUTION_ID='E6OL76123FRO6'

# this will deploy the current public folder to a subfolder in the s3 bucket
# the subfolder is the name of the TRAVIS_BRANCH
if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
echo "skipping deploy to S3: this is a pull request"
exit 0
#!/bin/bash

# Follow instructions in this guide to setup an S3 & Cloudfront distribution:
# https://docs.google.com/document/d/1EacCSUhaHXaL8ll8xjcd4svyguEO-ipf5aF980-_q8E

# Typically this is the Project name.
# The trailing slash is important
# Can be set to an empty string for working at the top level of the bucket
S3_BUCKET_PREFIX='codap-dev/'
# AWS CloudFront distribution domain
DISTRIBUTION_DOMAIN='codap-dev.concord.org'
# name of branch to deploy to root of site
ROOT_BRANCH='production'
# Bucket to deploy to, typically this is 'model-resources', but some projects
# have their own buckets
S3_BUCKET='models-resources'
# location of built files
SRC_DIR='dist/ci'

# exit when any command fails
set -e

# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command exited with code $?."' EXIT

# extract current TAG if present
# the 2> is to prevent error messages when no match is found
# the || echo prevents script exit when it doesn't match
CURRENT_TAG=`git describe --tags --exact-match $GITHUB_SHA 2> /dev/null || echo ''`

# Extract the branch or tag name from the GITHUB_REF
# it should either be: refs/head/branch-name or
# or refs/tags/v1.2.3
# since we ought to know if this is a branch or tag based on the ref
# we could simplify the CURRENT_TAG approach above
BRANCH_OR_TAG=${GITHUB_REF#refs/*/}
echo branch or tag: $BRANCH_OR_TAG

# strip PT ID from branch name for branch builds
DEPLOY_DIR_NAME=$BRANCH_OR_TAG
PT_PREFIX_REGEX="^([0-9]{8,}-)(.+)$"
PT_SUFFIX_REGEX="^(.+)(-[0-9]{8,})$"
if [[ $DEPLOY_DIR_NAME =~ $PT_PREFIX_REGEX ]]; then
DEPLOY_DIR_NAME=${BASH_REMATCH[2]}
fi
if [[ $DEPLOY_DIR_NAME =~ $PT_SUFFIX_REGEX ]]; then
DEPLOY_DIR_NAME=${BASH_REMATCH[1]}
fi

# tagged builds deploy to /version/TAG_NAME
if [ "$BRANCH_OR_TAG" = "$CURRENT_TAG" ]; then
mkdir -p _site/version
S3_DEPLOY_DIR="version/$BRANCH_OR_TAG"
DEPLOY_DEST="_site/$S3_DEPLOY_DIR"
# in this case we are going to deploy this code to a subfolder of version
# So ignore everything except this folder.
# Currently this only escapes `.`
S3_DEPLOY_DIR_ESCAPED=$(sed 's/[.]/[&]/g;' <<<"$S3_DEPLOY_DIR")
IGNORE_ON_SERVER="^(?!$S3_BUCKET_PREFIX$S3_DEPLOY_DIR_ESCAPED/)"

# root branch builds deploy to root of site
elif [ "$BRANCH_OR_TAG" = "$ROOT_BRANCH" ]; then
DEPLOY_DEST="_site"
# in this case we are going to deploy this branch to the top level
# so we need to ignore the version and branch folders
IGNORE_ON_SERVER="^$S3_BUCKET_PREFIX(version/|branch/)"

if [ "$TRAVIS_BRANCH" = 'master' ]; then
mv $SRC_DIR _site
INVAL_PATH="/index.html"
# branch builds deploy to /branch/BRANCH_NAME
else
# the 2> is to prevent error messages when no match is found
CURRENT_TAG=`git describe --tags --exact-match $TRAVIS_COMMIT 2> /dev/null`
if [ "$TRAVIS_BRANCH" = "$CURRENT_TAG" ]; then
echo "skipping deploy to S3: this is a release tag"
exit 0
else
# this is a branch build
mkdir -p _site/branch
DEPLOY_DIR=branch/$TRAVIS_BRANCH
INVAL_PATH="/branch/$TRAVIS_BRANCH/index.html"
fi
mv $SRC_DIR _site/$DEPLOY_DIR
export DEPLOY_DIR
mkdir -p _site/branch
S3_DEPLOY_DIR="branch/$DEPLOY_DIR_NAME"
DEPLOY_DEST="_site/$S3_DEPLOY_DIR"
# in this case we are going to deploy this code to a subfolder of branch
# So ignore everything except this folder.
# Currently this only escapes `.`
S3_DEPLOY_DIR_ESCAPED=$(sed 's/[.]/[&]/g;' <<<"$S3_DEPLOY_DIR")
IGNORE_ON_SERVER="^(?!$S3_BUCKET_PREFIX$S3_DEPLOY_DIR_ESCAPED/)"
fi
s3_website push --site _site
# explicit CloudFront invalidation to workaround s3_website gem invalidation bug
# with origin path (https://github.com/laurilehmijoki/s3_website/issues/207).
# aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths $INVAL_PATH

# used by s3_website.yml
export S3_BUCKET_PREFIX
export IGNORE_ON_SERVER
export DISTRIBUTION_DOMAIN
export S3_BUCKET

# copy files to destination
mv $SRC_DIR $DEPLOY_DEST

# deploy the site contents; increase memory for s3_website gem
echo Deploying "$BRANCH_OR_TAG" to "$S3_BUCKET:$S3_BUCKET_PREFIX$S3_DEPLOY_DIR"...
JAVA_TOOL_OPTIONS="-Xms1g -Xmx2g" s3_website push --site _site
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"build:bundle-prod": "NODE_ENV=production webpack --progress",
"build:cfm:comment": "echo Usage: `npm run build:cfm` builds CFM for CODAP. Assumes that CFM repository named 'cloud-file-manager' and CODAP repository named 'codap' are siblings.",
"build:cfm": "cd ../cloud-file-manager && npm run build:codap && cd ../codap && ./bin/recordCFMVersion",
"build:ci": "npm run build:bundle-prod && bin/makeCodapZip --languages=en ci",
"build:dev:comment": "echo Usage: `npm run build:dev` performs a local development build",
"build:dev": "npm run build:bundle-dev && npm run build:local",
"build:embedded:comment": "echo Usage: `npm run build:embedded` builds a CODAP embedded mode package named embeddedCODAP",
Expand All @@ -26,7 +27,6 @@
"build:parser": "pegjs --cache --export-var 'DG.formulaParser' 'apps/dg/formula/formulaGrammar.pegjs' 'apps/dg/formula/formulaParser.js'",
"build:production:comment": "Usage: `npm run build:production` performs a local production build",
"build:production": "npm run build:bundle-prod && npm run build:local",
"build:travis": "npm run build:bundle-prod && bin/makeCodapZip --languages=en travis",
"clean": "npm-run-all clean:tmp clean:bundle",
"clean:all": "npm-run-all clean:tmp clean:bundle clean:dependencies",
"clean:bundle": "rimraf apps/dg/resources/build",
Expand Down
37 changes: 11 additions & 26 deletions s3_website.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
s3_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
s3_secret: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
s3_bucket: models-resources
s3_key_prefix: codap-dev
s3_bucket: <%= ENV['S3_BUCKET'] %>
# only add the s3_key_prefix if there is one
<% if ENV['S3_BUCKET_PREFIX'] != '' %>
# Everywhere else the s3 prefix needs a trailing slash
# However s3_website config doesn't need one
s3_key_prefix: <%= ENV['S3_BUCKET_PREFIX'].sub(/\\/$/, '') %>
<% end %>
s3_endpoint: us-east-1
gzip: true

cloudfront_distribution_id: E6OL76123FRO6
cloudfront_invalidate_root: true
cloudfront_wildcard_invalidation: true

<% if ENV['TRAVIS_BRANCH'] == 'master' %>
# in this case we are going to deploy this branch to the top level of the domain
# so we need to ignore the version and branch folders
ignore_on_server: ^codap-dev/(version/|branch/)
<% else %>
# in this case we are going to deploy this code to a subfolder of either the branch
# or version folder. So ignore everything except this folder.
ignore_on_server: ^(?!codap-dev/<%= Regexp.escape(ENV['DEPLOY_DIR']) %>/)
<% end %>
ignore_on_server: <%= ENV['IGNORE_ON_SERVER'] %>
max_age:
"codap-dev/*": 600 # 10 minutes
"codap-dev/version/*": 31536000 # 1 year
"codap-dev/branch/*": 0

cloudfront_distribution_config:
aliases:
quantity: 1
items:
- codap-dev.concord.org
"<%= ENV['S3_BUCKET_PREFIX'] %>*": 600 # 10 minutes
"<%= ENV['S3_BUCKET_PREFIX'] %>version/*": 31536000 # 1 year
"<%= ENV['S3_BUCKET_PREFIX'] %>branch/*": 0