header

Github Actions Remote Trigger

GitHub actions is a popular option to automate all common software workflows such as test, build and deploy. Jenkins was the most popular option for doing the same tasks; with that said, a challenge to connect two platforms will be: How to invoke GitHub action workflows from a Jenkins pipeline. In this technical post, we will go over this approach and we will describe what we need to do. The first step is to create a fine-grained token to get access with some privileges to the target GitHub repository. You can do this in Settings > Developer settings > Personal access tokens > Fine-grained tokens > Generate new token with these permissions:

Repository Permission

Read access to:

  • Actions variables
  • Deployments
  • Environments
  • Metadata
  • Secrets

Read and Write access to:

  • Action
  • Code (Contents)
  • Workflows

Then, you will need to add a repository_dispatch event as you can see in this example:

name: Playwright Tests
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  repository_dispatch:
  schedule:
    - cron:  '0 21 * * *'
env:
  APPLITOOLS_API_KEY: ${{ secrets.APPLITOOLS_API_KEY }}
  USERNAME: ${{ secrets.USERNAME }}
  PASSWORD: ${{ secrets.PASSWORD }}
jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        node-version: 18
    - name: Install dependencies
      run: npm ci
    - name: Install Playwright Browsers
      run: npx playwright install --with-deps
    - name: Run Playwright visual tests
      run: npx playwright test visual --project chromium
    - name: Run Playwright e2e tests
      run: npx playwright test e2e
    - uses: actions/upload-artifact@v3
      if: always()
      with:
        name: playwright-report
        path: playwright-report/
        retention-days: 30

The last step is to create a HTTP request

curl --request POST \
  --url 'https://api.github.com/repos/${USERNAME}/${YOUR_REPOSITORY}/dispatches' \
  --header 'Accept: application/vnd.github+json' \
  --header 'Authorization: Bearer ${TOKEN}' \
  --data '{"event_type": "Called from API request"}'

where:

  • ${USERNAME} GitHub username
  • ${YOUR_REPOSITORY} Repository with workflow you want to run
  • ${TOKEN} Your fine-grained generated token

Invoke from Jenkins Pipeline

If you want to execute this cURL command from a declarative Jenkins pipeline, you can do it in this way:

stage ('Testing') {
  steps {
    echo 'Invoke GitHub Actions Workflow'
    script {
      try {
        sh """curl -X POST https://api.github.com/repos/josdem/playwright-vetlog/dispatches \
        --header "Accept: application/vnd.github.v3+json" \
        --header "Authorization: Bearer ${token}" \
        --data '{"event_type": "Called from Jenkins"}'"""
      } catch (Exception e) {
        echo "Failed to invoke GitHub Actions Workflow: ${e.getMessage()}"
      }
    }
    echo 'Done!'
  }
}

To browse a project with this implementation go here and here to download the project:

git clone git@github.com:josdem/playwright-vetlog.git
git clone git@github.com:josdem/vetlog-pipeline.git

Return to the main article

comments powered by Disqus