Sponsored Link
Looking for a unified way to  viewmonitor, and  debug Playwright Test Automation runs?
Try the Playwright Dashboard by today. Use the coupon code PWSOL10 for a 12 month 10% discount.

How do I make a GitHub Action trigger Playwright Tests to run in a different repo?

These same steps can be used to programatically kick off Github Actions job in any repo regardless of the Test Automation Framework.

If you ever find yourself with your Playwright Test Automation code in a separate repository than the service or application under test, have no fear! This post will cover how to trigger runs via Github actions across repositories.

There are a few ways to accomplish this but the easiest most simplest way is to utilize github actions, specifically the peter-evans/repository-dispatch@v2 action. This GitHub action allows you to easily utilize the Repository Dispatch API in order to kick off GitHub actions that are in other repositories.

The only prerequisite is you will need to create is a Personal Access Token (PAT) with the repos scope.

The steps to create your Personal Access Token can be found here. The scopes that need to be allowed are found below.

Once saved you will want to save your PAT, in as a Github Action Secret within the Application repository under Settings. Within left hand side of settings expand Secrets and view the Actions page

Repository > Settings > Secrets > Actions

And add the PAT with the name REPO_ACCESS_TOKEN

Adding PAT to Action secrets

Building out the GitHub Action yml files

Once that is done you will just need to build out your .yml files to initiate the Repository Dispatch, and your test repository to Run on repository_dispatch. Below are example Github action yml files for both the service/application repository (where you want to initiate a test run from) along with the receiving repository to run the tests.

This first section includes a yml example of the service or application and a link to an actual GitHub repo where you can see this in action.

build-deploy.yml from my-service repository

name: My Service Build and Deploy
      - develop
    name: Staging Deploy
    runs-on: ubuntu-latest
      - name: Logging the Staging release
        run: echo "This run is the staging deployment"
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set URL to pass to Tests
        run: |
          echo "ENVIRONMENT=http://my-site.com" >> $GITHUB_ENV
      - name: Build and Deploy
        run: |
          echo "Build and Deploy"
      - name: Kick Off Playwright Test Job
        uses: peter-evans/repository-dispatch@v2
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          repository: bmayhew/my-tests
          event-type: run-my-tests
          client-payload: '{"github": ${{ toJson(github) }}}'

GitHub Action Run Example

GitHub my-site repo

Quick note on this line client-payload: '{"github": ${{ toJson(github) }}}', This allows you to pass all the details from the current github run (status, environment variables, anything you want to set or pass in from previous steps in the job to the test job. Ways I've used this include, sending a preview environment URL, a github comment id, setting what type of feedback/test I was (happy path or full run), etc.

This section is an example of what you would include in your Playwright Test repository. The specific lines to note include when to run the GitHub action on and creating the type which is the name from the event-type that was sent from the service/application GitHub action.

    types: ["run-my-tests"]

run-tests.yml from my-tests repository

name: Playwright Automated Tests
        description: "URL to run tests against"
        required: true
        default: https://my-site.com
    types: ["run-my-tests", "run-my-tests-2"]
    timeout-minutes: 60
    runs-on: ubuntu-latest
      ENVIRONMENT: ${{ github.event.inputs.environment }}${{ github.event.client_payload.github.event.inputs.environment }}
      - name: Run Playwright tests
        run: |
          echo "Environment: $ENVIRONMENT"
          echo "The github event is: ${{ github.event_name }}"
          echo "Run Test Here"
          echo "Below steps in action provide what you have access to use in your scripts as needed"
      - name: Dump GitHub context
        id: github_context_step
        # The below prints out all info you have available to use in any reporting or test running from the other repo
        run: echo '${{ toJSON(github) }}' 

GitHub Action Run Example

GitHub my-test repo

In order to access any of the details that were sent over in the client_payload you have to call

${{ github.event.client_payload.github.INFO_YOU_WANT }}

If your not sure what you want running the command

echo '${{ toJSON(github) }}'

will echo out in the github action a json object with all the different key value pairs you have access to from within the GitHub action.

If you want to get fancy you could continue to utilize the Repository Dispatch functionality to make a call back to the original Service/Application repository with a status, have some automation to do different things based on if the tests passed or failed. Example you could add a label to a specific pull request or create an action that would add a comment to a pull/request stating the status of the tests (you would have to persist the original pull request ID from the client_payload).

The sky's the limit as far as what you can do with this functionality across repositories. I hope you found this helpful! If you come up with any creative ways to utilize Repository Dispatch or you found this helpful, reach out and let me know on Twitter @butchmayhew  or consider buying me a cup of coffee.