Skip to content

Add Github preview deployment workflow #24

Add Github preview deployment workflow

Add Github preview deployment workflow #24

Workflow file for this run

name: Preview
on:
pull_request:
types:
- opened
- reopened
- synchronize
- converted_to_draft
jobs:
dev-deploy:
runs-on: ubuntu-latest
environment:
name: development
url: https://dev-design.va.gov/${{ github.event.number }}
# TODO: make these build steps reusable with deploy.yml as a resuable workflow
# https://docs.github.com/en/actions/sharing-automations/reusing-workflows#creating-a-reusable-workflow
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Use Node.js 16.x
uses: actions/setup-node@v2
with:
node-version: 16.x
cache: 'yarn'
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7.5 # Not needed with a .ruby-version file
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- run: yarn install
- run: yarn run build
- run: bundle exec jekyll build --config _config.yml,jekyll-configs/dev.yml
- name: Make BUILD.txt file
# The -e flag enables the interpretation of the \n newline character
run: |
echo -e "REF=${{ github.sha }}\n\
BUILD_ID=${{ github.run_id }}\n\
BUILDTIME=$(date)" > _site/BUILD.txt
# We are taking these extra steps due to some differences between Jekyll and AWS S3.
# To access a .html file served from S3, the URL needs to have the .html extension.
# We're removing the extension to make the URLs prettier.
# More context:
# https://simpleit.rocks/ruby/jekyll/tutorials/having-pretty-urls-in-a-jekyll-website-hosted-in-amazon-s3/
- name: Remove .html extension on non-index files
run: |
find _site/ -type f ! -iname 'index.html' -iname '*.html' \
-print0 | while read -d $'\0' f; do mv "$f" "${f%.html}"; done
# Update internal site links to use the github.event.number so that they link correctly in the preview site.
# Otherwise, the links will point to the production site.
- name: Update HTML href, src, and action attributes
run: |
find "_site" -type f -name "*.html" | while read -r file; do
sed -i.bak -E 's/(href|src|action)=\"\/([^"]*)\"/\1="\/${{ github.event.number }}\/\2"/g' "$file"
# Remove backup files created by sed
rm "$file.bak"
done
shell: bash
# Fix duplicate github.event.number in href and src attributes
- name: Fix duplicates in HTML href, src, and action attributes
run: |
find "_site" -type f -name "*.html" | while read -r file; do
sed -i.bak -E 's/href="\/${{ github.event.number }}\/${{ github.event.number }}\/([^"]*)"/href="\/${{ github.event.number }}\/\1"/g' "$file"
sed -i.bak -E 's/src="\/${{ github.event.number }}\/${{ github.event.number }}\/([^"]*)"/src="\/${{ github.event.number }}\/\1"/g' "$file"
sed -i.bak -E 's/action="\/${{ github.event.number }}\/${{ github.event.number }}\/([^"]*)"/action="\/${{ github.event.number }}\/\1"/g' "$file"
done
shell: bash
# Update internal site links to use the github.event.number so that they link correctly in the preview site.
- name: Update HTML href attributes
run: |
find "_site" -type f -name "*.html" | while read -r file; do
sed -i.bak -E 's|href="https://design.va.gov|href="https://dev-design.va.gov/${{ github.event.number }}|g' "$file"
# Remove backup files created by sed
rm "$file.bak"
done
shell: bash
- uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "us-gov-west-1"
- name: Sync extensionless html files with correct type
run: |
aws s3 sync _site s3://dev-design.va.gov/${{ github.event.number }} \
--acl public-read \
--delete \
--exclude "storybook/*" \
--exclude "*.*" \
--content-type "text/html"
- name: Sync remaining files
run: |
aws s3 sync _site s3://dev-design.va.gov/${{ github.event.number }} \
--acl public-read \
--delete \
--exclude "*" \
--include "*.*" \
--exclude "storybook/*"