Create GitHub Actions Workflow for Pushing to gh‐pages - FearlessSolutions/publish-playwright GitHub Wiki

[!important] All of the files in this section should be created in the main branch.

Create a GitHub Actions Workflow for Pushing to gh-pages

Create the file .github/workflows/push-to-gh-pages.yml that will checkout gh-pages, download the artifacts to the deploy locations, pushes them to gh-pages, and displays the new url for the addition to the GitHub summary so it will display nicely. You must update the url to match your GitHub Pages url.

# 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
          git commit -am "${{ inputs.COMMIT_MSG }}"
          git pull origin gh-pages --rebase -s recursive -X ours
          git push
          echo "New URL: https://fearlesssolutions.github.io/publish-playwright/${{ inputs.URL_PATH }}" >> "$GITHUB_STEP_SUMMARY"