Skip to content

Commit

Permalink
Deploys static assets to an s3 bucket (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-carey authored Jan 21, 2022
1 parent 2784a4a commit e328b13
Show file tree
Hide file tree
Showing 17 changed files with 708 additions and 24 deletions.
25 changes: 25 additions & 0 deletions .github/actions/use-local-actions/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---

name: 'Use local actions'
description: |
Replaces `uses` within actions that use other actions within this repo.
If we do not replace with local actions, we will end up using whatever
version is tagged. Currently, we are marking all internal actions with
whatever the current major version is. Though we can technically use
multiple versions, we would like to avoid that.
Example replacement:
- uses: shopsmart/github-actions/actions/npm-setup@v1
Replaced with
- uses: ./actions/npm-setup
runs:
using: 'composite'
steps:
- name: 'Use local actions'
shell: bash
run: $GITHUB_ACTION_PATH/use-local-actions.sh
19 changes: 19 additions & 0 deletions .github/actions/use-local-actions/use-local-actions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

function use-local-actions() {
set -eo pipefail

for file in actions/*/action.yml; do
echo "[DEBUG] Setting internal actions to local references for $file" >&2
sed -i.bak \
's/uses: shopsmart\/github-actions\/actions\/\(.*\)@.*/uses: .\/actions\/\1/g' \
"$file"
done
}

if [ "${BASH_SOURCE[0]}" = "$0" ]; then
set -u

use-local-actions "${@:-}"
exit $?
fi
43 changes: 43 additions & 0 deletions .github/workflows/test-deploy-static-assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
###
# This test of the action is very limited. If anything, this tests
# that the action is not invalid; however, it does not test any of
# the functionality of downloading release assets or uploading to s3
# bucket without aws credentials.
##
---

name: 'Run deploy-static-assets action'

on:
pull_request:
paths:
- actions/deploy-static-assets/*
- actions/unpack-archive/*

defaults:
run:
shell: bash

jobs:
test-deploy-static-assets-action:
name: 'Uses the deploy-static-assets action'
runs-on: ubuntu-latest
steps:
- name: 'Checkout actions'
uses: actions/checkout@v2

- name: 'Use local actions'
uses: ./.github/actions/use-local-actions

- name: 'Create dummy files'
run: |
mkdir -p static-assets
echo "<html><head></head><body>Hello world</body></html>" > static-assets/index.html
echo "head p { color: black; }" > static-assets/style.css
tar -zcvf static-assets.tgz -C static-assets/ .
rm -rf static-assets/
- name: 'Run deploy static assets'
uses: ./actions/deploy-static-assets
with:
pattern: 'static-assets.tgz'
14 changes: 8 additions & 6 deletions .github/workflows/test-npm-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ jobs:
name: 'Uses the npm-run action'
runs-on: ubuntu-latest
steps:
-
name: 'Checkout actions'
- name: 'Checkout actions'
uses: actions/checkout@v2
-
name: 'Checkout hello world project'

- name: 'Use local actions'
uses: ./.github/actions/use-local-actions

- name: 'Checkout hello world project'
uses: actions/checkout@v2
with:
repository: mike-carey/hello-world-nodejs
path: ./hello-world
-
name: 'Run npm tests'

- name: 'Run npm tests'
uses: ./actions/npm-run
with:
node-version: '14'
Expand Down
15 changes: 7 additions & 8 deletions .github/workflows/test-npm-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@ jobs:
name: 'Uses the npm-setup action'
runs-on: ubuntu-latest
steps:
-
name: 'Checkout actions'
- name: 'Checkout actions'
uses: actions/checkout@v2
-
name: 'Checkout hello world project'

- name: 'Checkout hello world project'
uses: actions/checkout@v2
with:
repository: mike-carey/hello-world-nodejs
path: ./hello-world
-
name: 'Run npm setup'

- name: 'Run npm setup'
uses: ./actions/npm-setup
with:
node-version: '14'
working-directory: ./hello-world
-
name: 'Run npm tests'

- name: 'Run npm tests'
working-directory: ./hello-world
run: npm run test
39 changes: 39 additions & 0 deletions .github/workflows/test-unpack-archive.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
###
# This test of the action is very limited. If anything, this tests
# that the action is not invalid; however, it does not test any of
# the functionality of downloading release assets or uploading to s3
# bucket without aws credentials.
##
---

name: 'Run unpack-archive action'

on:
pull_request:
paths:
- actions/unpack-archive/*

defaults:
run:
shell: bash

jobs:
test-unpack-archive-action:
name: 'Uses the unpack-archive action'
runs-on: ubuntu-latest
steps:
- name: 'Checkout actions'
uses: actions/checkout@v2

- name: 'Create dummy files'
run: |
mkdir -p static-assets
echo "<html><head></head><body>Hello world</body></html>" > static-assets/index.html
echo "head p { color: black; }" > static-assets/style.css
tar -zcvf static-assets.tgz -C static-assets/ .
rm -rf static-assets/
- name: 'Run unpack archive'
uses: ./actions/unpack-archive
with:
filename: 'static-assets.tgz'
18 changes: 8 additions & 10 deletions .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ jobs:
name: 'Pre-commit check'
runs-on: ubuntu-latest
steps:
-
name: 'Checkout'
- name: 'Checkout'
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: 'Run pre-commit'

- name: 'Run pre-commit'
uses: pre-commit/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -31,12 +30,11 @@ jobs:
name: 'Run tests'
runs-on: ubuntu-latest
steps:
-
name: 'Checkout'
- name: 'Checkout'
uses: actions/checkout@v2
-
name: 'Install bats'

- name: 'Install bats'
run: brew install bats-core
-
name: 'Run action tests'

- name: 'Run action tests'
run: bats -r actions/*/*.bats
117 changes: 117 additions & 0 deletions actions/deploy-static-assets/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---

name: 'Deploy static assets'
description: 'Deploys static assets to an s3 bucket (currently the only supported option)'

inputs:
pattern:
description: |
If downloading release assets from github, the pattern for the assets file.
Otherwise, the path to the static assets. If it is an archive, it will unpacked.
required: true

# GH release Options
github-token:
description: 'The github token to allow for searching for release assets'
type: string
default: ${{ github.token }}
tag:
description: 'The github release tag to pull that assets from'
default: ''

# AWS (S3) Options
aws-access-key-id:
description: 'The AWS access key id to log into ECR with'
default: ''
aws-secret-access-key:
description: 'The AWS secret access key to log into ECR with'
default: ''
aws-region:
description: 'The AWS region to log into if using ECR'
default: 'us-east-1'
role-to-assume:
description: 'Allows one to configure the assume role'
default: ''
role-duration-seconds:
description: 'Allows one to configure the duration of assume role'
default: 1200

s3-bucket:
description: 'The name of the s3 bucket to upload the assets to'
default: ''
s3-bucket-path:
description: 'The path within the s3 bucket to upload the assets to'
default: ''
s3-tags:
description: |
Key value pairs to attach to each s3 object.
Example:
s3-tags: |
version=v1
owner=carl
default: ''

runs:
using: 'composite'
steps:
- name: 'Get info'
id: info
env:
TAG: ${{ inputs.tag }}
AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
shell: bash
run: ${{ github.action_path }}/static-assets-info.sh "${{ inputs.pattern }}"

- name: 'Install gh cli'
shell: bash
run: brew install gh
# Only if we are downloading assets from gh release
if: inputs.tag != ''

- name: 'Download static assets'
shell: bash
run: gh -R "${{ github.repository }}" release download -p "${{ inputs.pattern }}" "${{ inputs.tag }}"
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
# Only if we are downloading assets from gh release
if: inputs.tag != ''

- name: 'Unpack release assets'
id: unpack
uses: shopsmart/github-actions/actions/unpack-archive@v1
with:
filename: ${{ inputs.pattern }}
destination: static-assets

- name: 'Configure AWS credentials'
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ inputs.aws-access-key-id }}
aws-secret-access-key: ${{ inputs.aws-secret-access-key }}
aws-region: ${{ inputs.aws-region }}
role-to-assume: ${{ inputs.role-to-assume }}
role-duration-seconds: ${{ inputs.role-duration-seconds }}
# Only if we are using AWS S3
if: steps.info.outputs.configure-aws-credentials == 'true'

- name: 'Upload static assets'
shell: bash
run: $GITHUB_ACTION_PATH/upload-static-assets.sh "${{ steps.unpack.outputs.destination }}"
env:
S3_BUCKET: ${{ inputs.s3-bucket }}
S3_BUCKET_PATH: ${{ inputs.s3-bucket-path }}
# Only if we are using AWS S3
if: steps.info.outputs.configure-aws-credentials == 'true'

- name: 'Tag static assets'
shell: bash
run: $GITHUB_ACTION_PATH/tag-static-assets.sh "${{ steps.unpack.outputs.destination }}"
env:
S3_BUCKET: ${{ inputs.s3-bucket }}
S3_BUCKET_PATH: ${{ inputs.s3-bucket-path }}
S3_TAGS: ${{ inputs.s3-tags }}
# Only if we are using AWS S3
if: steps.info.outputs.configure-aws-credentials == 'true' && inputs.s3-tags != ''
45 changes: 45 additions & 0 deletions actions/deploy-static-assets/static-assets-info.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bats

load static-assets-info.sh

function setup() {
export AWS_ACCESS_KEY_ID=''
export AWS_SECRET_ACCESS_KEY=''
}

function teardown() {
:
}

@test "it should configure aws credentials" {
export AWS_ACCESS_KEY_ID='foo'
export AWS_SECRET_ACCESS_KEY='bar'

run static-assets-info

[ "$status" -eq 0 ]
[[ "$output" =~ .*::set-output\ name=configure-aws-credentials::true ]]
}

@test "it should not configure aws credentials" {
run static-assets-info

[ "$status" -eq 0 ]
[[ "$output" =~ .*::set-output\ name=configure-aws-credentials::false ]]
}

@test "it should error because access key id is missing" {
export AWS_SECRET_ACCESS_KEY='bar'

run static-assets-info

[ "$status" -ne 0 ]
}

@test "it should error because secret access key is missing" {
export AWS_ACCESS_KEY_ID='foo'

run static-assets-info

[ "$status" -ne 0 ]
}
Loading

0 comments on commit e328b13

Please sign in to comment.