Skip to content

Deploy Static Site

Deploy Static Site #40

Workflow file for this run

name: 'Deploy Static Site'
on:
workflow_dispatch:
inputs:
env:
description: Deploy where?
required: false
default: 'stage'
type: choice
options:
- stage
- prod
static_repo_ref:
description: Which branch or tag?
required: true
default: 'main'
type: 'string'
workflow_call:
inputs:
env:
description: Deploy where?
required: false
default: 'stage'
type: 'string'
static_repo_ref:
description: Which branch or tag?
required: true
default: 'main'
type: 'string'
jobs:
deploy_static_site:
name: Deploy Static Site
permissions:
contents: read
id-token: write
runs-on: codebuild-dpc-static-site-${{ github.run_id }}-${{ github.run_attempt }}
env: # note: "sandbox" env is used for "stage" environment, "prod" is for "prod"
AWS_ENV: ${{ inputs.env == 'prod' && 'prod' || 'sandbox' }}
steps:
- name: "Checkout code"
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
repository: 'CMSgov/dpc-static-site'
ref: ${{ inputs.static_repo_ref }}
- name: "Set Version"
env:
STATIC_REPO_REF: ${{ inputs.static_repo_ref }}
run: |
echo "version: $STATIC_REPO_REF" >> _version_config.yml
- name: "Add dirs"
run: mkdir -p _site && mkdir -p .jekyll-cache
- name: Assert Ownership
run: sudo chmod -R 777 .
- name: 'Build Image'
run: docker build . -f Dockerfiles/Dockerfile.static_site -t static_site
- name: 'Build Site'
env:
JEKYLL_ENV: ${{ inputs.env }}
run: |
docker run \
-e JEKYLL_ENV=$JEKYLL_ENV \
-v ./_site:/dpc-site-static/_site \
-v ./.jekyll-cache:/dpc-site-static/.jekyll-cache \
--rm static_site
- name: AWS Credentials
uses: aws-actions/configure-aws-credentials@b47578312673ae6fa5b5096b330d9fbac3d116df # v4.2.1
with:
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: arn:aws:iam::${{ secrets.PROD_ACCOUNT }}:role/delegatedadmin/developer/dpc-${{ env.AWS_ENV }}-github-actions
- name: Set env vars from AWS params
uses: cmsgov/cdap/actions/aws-params-env-action@main
env:
AWS_REGION: ${{ vars.AWS_REGION }}
with:
params: |
SONAR_HOST_URL=/sonarqube/url
SONAR_TOKEN=/sonarqube/token
TARGET_BUCKET=/dpc/${{ env.AWS_ENV }}/static_site
- name: Run quality gate scan
if: ${{ inputs.env == 'stage' }}
uses: sonarsource/sonarqube-scan-action@fd88b7d7ccbaefd23d8f36f73b59db7a3d246602 # v6.0.0
with:
args:
-Dsonar.projectKey=bcda-dpc-static-site
-Dsonar.sources=.
-Dsonar.working.directory=./sonar_workspace
-Dsonar.branch.name=${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}
-Dsonar.projectVersion=${{ github.ref_name == 'main' && github.sha || 'branch' }}
-Dsonar.qualitygate.wait=true
-Dsonar.ci.autoconfig.disabled=true
- name: "Sync _site"
run: aws s3 sync _site/ s3://"$TARGET_BUCKET"/ $(["${{ inputs.target_environment }}" == 'prod'] && echo "--delete")
- name: Upload html files without suffix with content-language set
run: |
for file in _site/*.html; do
suffixless=`basename ${file/.html}`
aws s3 cp $file s3://"$TARGET_BUCKET"/$suffixless --content-language text/html
done
- name: Invalidate Cloudfront cache
run: |
DISTRIBUTION_ID=`aws cloudfront list-distributions --query "DistributionList.Items[].{Id:Id, OriginId: Origins.Items[0].Id}[?OriginId=='$TARGET_BUCKET'].Id" --output text`
aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths '/*'
- uses: slackapi/slack-github-action@b0fa283ad8fea605de13dc3f449259339835fc52 # v2.1.0
name: Slack Success
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
# Sends to dpc-deploys
payload: |
channel: "CMC1E4AEQ"
attachments:
- color: good
text: "SUCCESS: <${{ github.server_url}}/${{ github.repository}}/actions/runs/${{ github.run_id }}|static site version> `${{ inputs.static_repo_ref }}` deployed to ${{ inputs.env }} environment."
mrkdown_in:
- text
- uses: slackapi/slack-github-action@b0fa283ad8fea605de13dc3f449259339835fc52 # v2.1.0
name: Slack failure
if: ${{ failure() }}
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
# Sends to dpc-deploys
payload: |
channel: "CMC1E4AEQ"
attachments:
- color: danger
text: "FAILURE: <${{ github.server_url}}/${{ github.repository}}/actions/runs/${{ github.run_id }}|static site version> `${{ inputs.static_repo_ref }}` deployed to ${{ inputs.env }} environment."
mrkdown_in:
- text