Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Invoke GitHub Actions Workflow from the Jenkins Pipeline?

Is there any method or solution to invoke the GitHub Actions Workflow from the Jenkins Declarative Pipeline?

What the Jenkinsfile should include to call said action?

like image 848
Hussain Avatar asked Nov 23 '25 20:11

Hussain


1 Answers

You should be able to call the API to Create a workflow dispatch event.
See "How to trigger a GitHub action with an HTTP request" from Riku Rouvila

Pre-requisites

  1. Generate a GitHub Token: Create a GitHub token with the necessary scopes enabled. Create this token from your GitHub account under Settings > Developer settings > Personal access tokens > Generate new token.

  2. Set up Your GitHub Repository: your GitHub repository must contain the GitHub Actions workflow file (.yml or .yaml) in the .github/workflows directory.

  3. Jenkins Setup: Jenkins must be installed and configured with necessary plugins such as "Pipeline" to support the Declarative Pipeline.

Jenkinsfile Script

I will use a try-catch block to add an error handling mechanisms and to capture HTTP errors that may occur during the API request.

pipeline {
    agent any 

    stages {
        stage('Invoke GitHub Actions Workflow') {
            steps {
                script {
                    try {
                        def url = "https://api.github.com/repos/YOUR_GITHUB_USERNAME/YOUR_REPOSITORY_NAME/actions/workflows/YOUR_WORKFLOW_FILE_NAME.yml/dispatches"
                        def response = sh(script: 'curl -X POST -H "Accept: application/vnd.github.v3+json" -H "authorization: Bearer YOUR_GITHUB_TOKEN" -d \'{"ref":"YOUR_BRANCH_NAME"}\' "${url}"', returnStdout: true).trim()
                        echo "Response: ${response}"
                    } catch (Exception e) {
                        echo "Failed to invoke GitHub Actions Workflow: ${e.getMessage()}"
                        currentBuild.result = 'FAILURE'
                    }
                }
            }
        }
    }
}

In the script:

  • Replace YOUR_GITHUB_USERNAME, YOUR_REPOSITORY_NAME, YOUR_WORKFLOW_FILE_NAME.yml, YOUR_GITHUB_TOKEN, and YOUR_BRANCH_NAME with the appropriate values.
  • YOUR_GITHUB_TOKEN should preferably be stored as a Jenkins secret to ensure security.

Verify the output in the Jenkins console post-job execution to confirm successful invocation of the GitHub Actions workflow. And Use Jenkins credentials management to handle your secrets securely.


I can trigger the action this way, but I get an empty response; how can I get the result of an action triggered this way?"

Getting the result of the GitHub Actions workflow that was triggered involves a multi-step process because triggering a workflow dispatch event via the API does not directly return the workflow run's details.
You would need to poll GitHub API for the workflow run's status until it completes. That would involve

  1. Trigger the Workflow: As shown above, trigger the workflow using the GitHub API via a curl command in your Jenkins pipeline.

  2. Retrieve the Workflow Run ID: After triggering the workflow, you would need to retrieve the ID of the workflow run to get its details. That can be done by querying the GitHub API for the list of workflow runs for the repository. You might have to introduce a slight delay before this step to allow GitHub to register the new workflow run.

  3. Poll for Workflow Completion: Once you have the workflow run ID, keep polling the GitHub API at regular intervals to check the status of the workflow run until it completes.

  4. Retrieve the Workflow Result: After the workflow run completes, get the workflow run details using the GitHub API to retrieve the final result of the run.

Here is a simplified Jenkinsfile that demonstrates the above strategy, using the "Pipeline Utility Steps" plugin in Jenkins to use the readJSON function:

pipeline {
    agent any 

    stages {
        stage('Invoke GitHub Actions Workflow and Get Result') {
            steps {
                script {
                    try {
                        // Step 1: Trigger the Workflow
                        def url = "https://api.github.com/repos/YOUR_GITHUB_USERNAME/YOUR_REPOSITORY_NAME/actions/workflows/YOUR_WORKFLOW_FILE_NAME.yml/dispatches"
                        sh(script: 'curl -X POST -H "Accept: application/vnd.github.v3+json" -H "authorization: Bearer YOUR_GITHUB_TOKEN" -d \'{"ref":"YOUR_BRANCH_NAME"}\' "${url}"', returnStdout: true).trim()

                        // Step 2 & 3: Get Workflow Run ID and Poll for Completion
                        def workflowRunsUrl = "https://api.github.com/repos/YOUR_GITHUB_USERNAME/YOUR_REPOSITORY_NAME/actions/runs"
                        def workflowRunID
                        def status = "queued"
                        
                        // Introduce a delay before polling for workflow run ID
                        sleep time: 10, unit: 'SECONDS'
                        
                        while (status == "queued" || status == "in_progress") {
                            def response = sh(script: "curl -H 'Accept: application/vnd.github.v3+json' -H 'authorization: Bearer YOUR_GITHUB_TOKEN' ${workflowRunsUrl}", returnStdout: true).trim()
                            def runs = readJSON text: response
                            workflowRunID = runs.workflow_runs[0].id
                            status = runs.workflow_runs[0].status

                            if (status == "queued" || status == "in_progress") {
                                sleep time: 10, unit: 'SECONDS'
                            }
                        }
                        
                        // Step 4: Retrieve the Workflow Result
                        def workflowRunDetailUrl = "${workflowRunsUrl}/${workflowRunID}"
                        def detailResponse = sh(script: "curl -H 'Accept: application/vnd.github.v3+json' -H 'authorization: Bearer YOUR_GITHUB_TOKEN' ${workflowRunDetailUrl}", returnStdout: true).trim()
                        def workflowRunDetails = readJSON text: detailResponse
                        
                        echo "Workflow Run Details: ${workflowRunDetails}"
                        
                    } catch (Exception e) {
                        echo "Failed to invoke GitHub Actions Workflow or retrieve results: ${e.getMessage()}"
                        currentBuild.result = 'FAILURE'
                    }
                }
            }
        }
    }
}
  • We are using a while loop to keep polling the GitHub API until the workflow run status changes from "queued" or "in_progress" to a final status.
  • The readJSON utility is used to parse the JSON responses returned from the GitHub API to extract the necessary details such as workflow run ID and status.
  • Replace YOUR_GITHUB_USERNAME, YOUR_REPOSITORY_NAME, YOUR_WORKFLOW_FILE_NAME.yml, YOUR_GITHUB_TOKEN, and YOUR_BRANCH_NAME with the actual values.
  • Adjust the sleep time as per your workflow's typical execution time to avoid unnecessary API calls.
like image 51
VonC Avatar answered Nov 26 '25 23:11

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!