Skip to content

Commit

Permalink
Add scripts and actions for #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Clark committed Apr 11, 2024
1 parent 4186a15 commit c14e6af
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Build

on: pull_request

jobs:
build:
name: Build
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.12.0]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run build
93 changes: 93 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Deploy

on:
push:
branches: [ main ]

permissions:
id-token: write # required to use OIDC authentication
contents: read # required to checkout the code from the repo

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout 📥
uses: actions/checkout@v2

- name: Use Node.js 20.12.0 ⚙️
uses: actions/setup-node@v1
with:
node-version: 20.12.0

- name: Setup vars 📋
id: vars
run: |
echo ::set-output name=distdir::$(git rev-parse --short ${{ github.sha }})-20.x-dist
- name: Install 🎯
run: yarn install

- name: Build 🏃
run: npm run build --env CI
- name: Upload artifacts 📤
uses: actions/upload-artifact@v2
with:
name: build-artifacts
path: site/dist
if-no-files-found: error

deploy:
name: Deploy
runs-on: ubuntu-latest
needs: build

steps:
- name: Checkout 📥
uses: actions/checkout@v2

- name: Download artifacts 📥
uses: actions/download-artifact@v2
with:
name: build-artifacts
path: site/dist

- name: Configure AWS credentials 🔐
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::851725248912:role/aria-cicd-role
role-duration-seconds: 900 # the ttl of the session, in seconds.
aws-region: us-east-2

- name: Setup vars 📋
id: vars
run: |
echo ::set-output name=identifier::$(git rev-parse --short ${{ github.sha }})-20.x
echo ::set-output name=distdir::$(git rev-parse --short ${{ github.sha }})-20.x-dist
- name: Prep files 🔨
run: npm run aws_deploy
env:
IDENTIFIER: ${{ steps.vars.outputs.identifier }}
DISTDIR: ${{ steps.vars.outputs.distdir }}

- name: Copy new index file to S3 📤
run: aws s3 cp $DISTDIR/index.html s3://aria-site/aria/$IDENTIFIER-aria.html --no-progress
env:
IDENTIFIER: ${{ steps.vars.outputs.identifier }}
DISTDIR: ${{ steps.vars.outputs.distdir }}

- name: Copy new dist dir to S3 📤
run: aws s3 cp $DISTDIR s3://aria-site/aria/$DISTDIR --recursive --no-progress
env:
IDENTIFIER: ${{ steps.vars.outputs.identifier }}
DISTDIR: ${{ steps.vars.outputs.distdir }}

- name: Deployment complete! 🚀
run: |
echo "Your build is at: https://d13285jxgcxetl.cloudfront.net/aria/${{ steps.vars.outputs.distdir }}/index.html"
1 change: 1 addition & 0 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"aws_deploy": "python3 scripts/deploy.py",
"preview": "vite preview"
},
"dependencies": {
Expand Down
62 changes: 62 additions & 0 deletions site/scripts/deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright(c) Meta Platforms, Inc. and affiliates.

import errno
import os
import shutil
import subprocess
import sys

"""Script to upload Aria app to S3 bucket
Usage: python3 aws_deploy.py
Assumptions:
1) You have the aws cli installed, and it can get access to credentials
1a) This can be anything specified here: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
1b) For github actions, you should have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY set as secrets, with AWS_DEFAULT_REGION in the environment
2) You have installed python 3.
3) You are running from within the root directory of the repo (and therefore need to look inside the 'site' folder to start work)
4) Environment variables NODE_VERSION, DISTDIR, and IDENTIFIER are defined
See .github/workflows/deploy.yml 'Setup Vars' step for how those env vars are created.
"""

def deploy():
os.chdir('site')
print("Calculating build hash and distdir...")
output = subprocess.run(
["git", "rev-parse", "--short", "HEAD"], capture_output=True
)
hash = output.stdout.decode("utf-8").strip()
print("\ngithash: " + hash)
identifier = os.environ["IDENTIFIER"]
distdir = os.environ["DISTDIR"]
print("\ndistdir: " + distdir)
# Blow away the previous dir, if any.
if os.path.exists(distdir):
print(f"Previous distribution dir {distdir} found, removing.")
shutil.rmtree(distdir)
print(f"\nCreating dist folder {distdir}")
try:
os.mkdir(distdir)
except OSError as err:
if err.errno == errno.EEXIST:
print(f"{distdir} already exists")
elif err.errno == errno.EACCES:
print(f"{distdir} permission denied")
raise
print("\nCopying dist folder")
subprocess.check_call(args=f"cp -a ./dist/* {distdir}", shell=True)
print("\nPrepping index.html with correct asset, css, and javascript paths")
index = "dist/index.html"
newindex = os.path.join(distdir, "index.html")
with open(index, "r") as input:
with open(newindex, "w+") as output:
# Massage the paths appropriately
for s in input:
s = (
s.replace("/assets/", f"/aria/{distdir}/assets/")
s.replace("/favicon.png", f"/aria/{distdir}/favicon.png")
)
output.write(s)

if __name__ == "__main__":
deploy()

0 comments on commit c14e6af

Please sign in to comment.