Skip to content

Using a GitHub Action with WebHooks to Dispatch a Jenkins Job for a Specific PR and pass it Parameters

TerrenceMcGuinness-NOAA edited this page Aug 12, 2024 · 1 revision

Seting up a GitHub Action to dispatch an event to trigger a Jenkins job for a specific PR, passing parameters to the Jenkins pipeline.

Step-by-Step Plan

  1. Create a GitHub Action Workflow:

    • Create a GitHub Action workflow that dispatches an event with parameters when a specific PR is selected.
  2. Set Up a Webhook in GitHub:

    • Configure a webhook in your GitHub repository to send events to your Jenkins server.
  3. Configure Jenkins to Handle Webhook Events and Parameters:

    • Set up Jenkins to listen for the webhook events, extract the parameters, and trigger the appropriate jobs.

Detailed Steps

1. Create a GitHub Action Workflow

Create a .github/workflows/dispatch.yml file in your repository with the following content:

name: Dispatch Event

on:
  workflow_dispatch:
    inputs:
      pr_number:
        description: 'Pull Request Number'
        required: true
      param1:
        description: 'Parameter 1'
        required: true
      param2:
        description: 'Parameter 2'
        required: true

jobs:
  dispatch:
    runs-on: ubuntu-latest
    steps:
      - name: Dispatch event to Jenkins
        run: |
          curl -X POST -H "Content-Type: application/json" \
          -d '{
                "event_type": "trigger-jenkins",
                "client_payload": {
                  "pr_number": "${{ github.event.inputs.pr_number }}",
                  "param1": "${{ github.event.inputs.param1 }}",
                  "param2": "${{ github.event.inputs.param2 }}"
                }
              }' \
          ${{ secrets.GITHUB_API_URL }}/repos/${{ github.repository }}/dispatches \
          -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}"

This workflow allows you to manually trigger it from the GitHub Actions tab, specifying the PR number and parameters.

2. Set Up a Webhook in GitHub

  1. Go to your GitHub repository.
  2. Navigate to Settings > Webhooks > Add webhook.
  3. Set the Payload URL to your Jenkins server URL followed by /github-webhook/.
  4. Set the Content type to application/json.
  5. Select the Let me select individual events option and choose Repository dispatch.

3. Configure Jenkins to Handle Webhook Events and Parameters

  1. Install Necessary Plugins:

    • Ensure you have the GitHub Integration Plugin and the Generic Webhook Trigger Plugin installed in Jenkins.
  2. Configure Jenkins Job:

    • In Jenkins, create or configure your Multi-Branch Pipeline job.
    • In the job configuration, under Branch Sources, add your GitHub repository.
    • Under Build Triggers, check Generic Webhook Trigger.
  3. Update Jenkinsfile:

    • Modify your Jenkinsfile to handle the dispatched event and extract the parameters.

Example Jenkinsfile Update

pipeline {
    agent any

    triggers {
        GenericTrigger(
            genericVariables: [
                [key: 'event_type', value: '$.event_type'],
                [key: 'pr_number', value: '$.client_payload.pr_number'],
                [key: 'param1', value: '$.client_payload.param1'],
                [key: 'param2', value: '$.client_payload.param2']
            ],
            causeString: 'Triggered by GitHub dispatch event

',


            token: 'your-token-here',
            printContributedVariables: true,
            printPostContent: true,
            regexpFilterText: '$event_type',
            regexpFilterExpression: 'trigger-jenkins'
        )
    }

    stages {
        stage('Build') {
            steps {
                echo "Building PR #${env.pr_number} with param1: ${env.param1} and param2: ${env.param2}"
                // Add your build steps here
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
                // Add your test steps here
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
                // Add your deploy steps here
            }
        }
    }
}

Summary

By following these steps, you can use GitHub Actions to dispatch events and GitHub Webhooks to trigger Jenkins jobs for a specific PR while passing parameters to the Jenkins pipeline. This setup allows you to manually trigger Jenkins jobs from the GitHub Actions tab, specifying the PR number and any necessary parameters.