Skip to content

Create GitHub Actions for Pushing to gh pages

Tiffany Forkner edited this page Apr 2, 2025 · 3 revisions

Important

All of the files in this section should be created in the main branch.

Create a GitHub Action for Pushing to gh-pages

Create the file .github/workflows/push-to-gh-pages.yml that will checkout gh-pages, download FILE_*_ARTIFACT_ID to FILE_*_DEPLOY_TO location, commits the new files to gh-pages using the COMMIT_MSG, and displays the GitHub Pages url with the URL_PATH appended in the GitHub summary.

Warning

You must update the url to match your GitHub Pages url.

Note

The workflow takes in two files that can be deployed to two locations, this allows us to push the json and html version of reports.

# Adapted from https://dev.to/ysfaran/how-to-use-playwright-with-github-actions-and-github-pages-4gdl
name: Push to GitHub Pages

on:
  workflow_call:
    # the inputs take in two artifacts that can be deployed
    # to different locations, only the first file is required
    inputs:
      FILE_1_ARTIFACT_ID:
        type: string
        required: true
      FILE_1_DEPLOY_TO:
        type: string
        required: true
      FILE_2_ARTIFACT_ID:
        type: string
        required: false
      FILE_2_DEPLOY_TO:
        type: string
        required: false
      COMMIT_MSG:
        type: string
        required: true
      URL_PATH:
        type: string
        required: true

permissions:
  contents: write
  actions: read

jobs:
  push-to-pages:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/create-github-app-token@v1
        id: app-token
        with:
          app-id: ${{ vars.APP_ID }}
          private-key: ${{ secrets.PRIVATE_KEY }}
      - name: Get GitHub App User ID
        id: get-user-id
        run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}
      - run: |
          git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
          git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
      - name: Checkout GitHub Pages Branch
        uses: actions/checkout@v4
        with:
          ref: gh-pages
          token: ${{ steps.app-token.outputs.token }}
      - name: Download File 1
        uses: actions/download-artifact@v4
        with:
          name: ${{ inputs.FILE_1_ARTIFACT_ID }}
          path: ${{ inputs.FILE_1_DEPLOY_TO }}
      - name: Download File 2
        uses: actions/download-artifact@v4
        if: inputs.FILE_2_ARTIFACT_ID && inputs.FILE_2_DEPLOY_TO
        with:
          name: ${{ inputs.FILE_2_ARTIFACT_ID }}
          path: ${{ inputs.FILE_2_DEPLOY_TO }}
      - name: Push to GitHub Pages Branch
        run: |
          git add -A
          if [[ -z $(git status --porcelain) ]]; then
            # if there were not changes, do nothing
            echo "Nothing to commit" >> "$GITHUB_STEP_SUMMARY"
          else
            git commit -am "${{ inputs.COMMIT_MSG }}"
            git pull origin gh-pages --rebase -s recursive -X ours
            git push
            echo "New URL: [link to GitHub pages]/${{ inputs.URL_PATH }}" >> "$GITHUB_STEP_SUMMARY"
          fi
Clone this wiki locally