randytarampi/me

View on GitHub
.github/workflows/deploy.pages.yml

Summary

Maintainability
Test Coverage
name: deploy (GitHub Pages)

on:
  workflow_call:
    inputs:
      application_name:
        description: 'The application to deploy'
        required: false
        default: www
        type: string
      deployment_environment:
        description: 'The targeted `DEPLOYMENT_ENVIRONMENT`'
        required: false
        default: dev
        type: string
    secrets:
      AWS_ACCESS_KEY_ID:
        required: true
      AWS_SECRET_ACCESS_KEY:
        required: true
      GH_PAGES_DEPLOYMENT_TOKEN:
        required: true

  workflow_dispatch:
    inputs:
      application_name:
        description: 'The application to deploy'
        required: true
        default: www
        type: choice
        options:
          - www
          - jsonresume-theme
      deployment_environment:
        description: 'The targeted `DEPLOYMENT_ENVIRONMENT`'
        required: true
        default: dev
        type: choice
        options:
          - dev
          - prd
    secrets:
      AWS_ACCESS_KEY_ID:
        required: true
      AWS_SECRET_ACCESS_KEY:
        required: true
      GH_PAGES_DEPLOYMENT_TOKEN:
        required: true

jobs:
  deploy:
    name: Run `yarn deploy`
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_REGION: us-east-1
      DEPLOY: true
      APPLICATION_NAME: ${{ github.event.inputs.application_name || inputs.application_name }}
      DEPLOYMENT_ENVIRONMENT: ${{ github.event.inputs.deployment_environment || inputs.deployment_environment }}
      GITHUB_ACTIONS_AUTHOR_EMAIL: github@randytarampi.ca
      GITHUB_ACTIONS_AUTHOR_NAME: deploy.pages
    runs-on:
      - ubuntu-latest
    concurrency: deploy-${{ github.ref }}
    steps:
      - name: Set variables
        id: variables
        run: |
          echo "::set-output name=application_name::$APPLICATION_NAME"
          echo "::set-output name=deployment_environment::$DEPLOYMENT_ENVIRONMENT"
          echo "NODE_ENV=$DEPLOYMENT_ENVIRONMENT" >> $GITHUB_ENV
          echo "PRINTABLE_PUPPETEER_NO_SANDBOX=$CI" >> $GITHUB_ENV
          echo "CPU_ARCH=$RUNNER_ARCH" >> $GITHUB_ENV
          echo "OS_NAME=$RUNNER_OS" >> $GITHUB_ENV
          echo "BRANCH=$GITHUB_REF_NAME" >> $GITHUB_ENV
          echo "COMMIT_SHA=$GITHUB_SHA" >> $GITHUB_ENV
          echo "BUILD_NUMBER=$GITHUB_RUN_ID" >> $GITHUB_ENV
          echo "JOB_NUMBER=$GITHUB_RUN_ATTEMPT" >> $GITHUB_ENV

      - name: Run `git checkout`
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Setup node@12
        uses: actions/setup-node@v2
        with:
          node-version: "12"
      - name: Setup dependencies on build runner
        run: |
          sudo apt-get update
          sudo apt-get install language-pack-ja japan* fonts-wqy-microhei ttf-wqy-microhei

      # NOTE-RT: Rerun `install` to make sure we've got our dependencies
      - name: Install `yarn`
        run: |
          npm install -g yarn@1
          yarn config set script-shell $(which bash)
      - name: Get Date
        id: get-date
        run: |
          echo "::set-output name=date::$(/bin/date -u "+%Y%m%d")"
        shell: bash
      - name: Restore `node_modules` cache
        uses: actions/cache@v2
        id: yarn-cache
        with:
          path: |
            node_modules
            */*/node_modules
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}-${{ steps.get-date.outputs.date }}
          restore-keys: |
            ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}-
            ${{ runner.os }}-yarn-
      - name: Actually run `yarn install --frozen-lockfile`
        run: yarn install --frozen-lockfile

      - name: Setup for deployment
        uses: chrnorm/deployment-action@releases/v1
        id: deployment
        with:
          token: ${{ github.token }}
          environment: ${{ steps.variables.outputs.deployment_environment }}
          description: '`${{ steps.variables.outputs.application_name }}` @ ${{ github.event.push.compare }}'
      - name: Build `docs`
        env:
          BABEL_ENV: production
        run: |
          yarn lerna run resume --scope=jsonresume-theme-*;
          yarn lerna run docs --scope="*/${{ steps.variables.outputs.application_name }}" --scope="*${{ steps.variables.outputs.application_name }}*"
      - name: Adjust `docs` for `dev`
        if: ${{ steps.variables.outputs.deployment_environment == 'dev' && steps.variables.outputs.application_name == 'www' }}
        run: |
          git config --local user.email "$GITHUB_ACTIONS_AUTHOR_EMAIL"
          git config --local user.name "$GITHUB_ACTIONS_AUTHOR_NAME"
          echo "www.dev.randytarampi.ca" > packages/${{ steps.variables.outputs.application_name }}/docs/CNAME;
          if [ -d "packages/${{ steps.variables.outputs.application_name }}/docs" ]; then git add --verbose --force --all packages/${{ steps.variables.outputs.application_name }}/docs; fi;
          if [[ -n $(git status -s) ]]; then git commit -m "release: Prepare to deploy \`${{ steps.variables.outputs.application_name }}\` to ${{ github.repository_owner }}.github.io.git"; fi;
      - name: Actually deploy (www/dev)
        if: ${{ steps.variables.outputs.deployment_environment }} == 'dev' && ${{ steps.variables.outputs.application_name }} == 'www'
        uses: JamesIves/github-pages-deploy-action@v4.3.0
        with:
          git-config-email: ${{ env.GITHUB_ACTIONS_AUTHOR_EMAIL }}
          git-config-name: ${{ env.GITHUB_ACTIONS_AUTHOR_NAME }} (www/dev)
          force: true
          clean: true
          branch: master
          folder: packages/${{ steps.variables.outputs.application_name }}/docs
          repository-name: ${{ github.repository_owner }}/${{ github.repository_owner }}.github.io
          token: ${{ secrets.GH_PAGES_DEPLOYMENT_TOKEN }}
        timeout-minutes: 10
      - name: Actually deploy (www/prd)
        if: ${{ steps.variables.outputs.deployment_environment == 'prd' && steps.variables.outputs.application_name == 'www' }}
        uses: JamesIves/github-pages-deploy-action@v4.3.0
        with:
          git-config-email: ${{ env.GITHUB_ACTIONS_AUTHOR_EMAIL }}
          git-config-name: ${{ env.GITHUB_ACTIONS_AUTHOR_NAME }} (www/prd)
          single-commit: true
          force: true
          clean: true
          branch: gh-pages
          folder: packages/${{ steps.variables.outputs.application_name }}/docs
        timeout-minutes: 10
      - name: Actually deploy (jsonresume-theme/prd)
        if: ${{ steps.variables.outputs.deployment_environment == 'prd' && steps.variables.outputs.application_name == 'jsonresume-theme' }}
        uses: JamesIves/github-pages-deploy-action@v4.3.0
        with:
          git-config-email: ${{ env.GITHUB_ACTIONS_AUTHOR_EMAIL }}
          git-config-name: ${{ env.GITHUB_ACTIONS_AUTHOR_NAME }} (jsonresult-theme/prd)
          force: true
          clean: true
          branch: master
          folder: packages/${{ steps.variables.outputs.application_name }}/docs
          repository-name: ${{ github.repository_owner }}/me.resume
          token: ${{ secrets.GH_PAGES_DEPLOYMENT_TOKEN }}
        timeout-minutes: 10
      - name: Handle deployment (success)
        if: success()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ github.token }}
          state: success
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}
      - name: Handle deployment (failure)
        if: failure()
        uses: chrnorm/deployment-status@releases/v1
        with:
          token: ${{ github.token }}
          state: failure
          deployment_id: ${{ steps.deployment.outputs.deployment_id }}