Hi,
I am trying to use Jenkinsfile at the root of the repository to merge the pull request into main/controller. Is this feasible or I need to use GitHub Actions or anything else?
This is a sample of my Jenkinssfile
pipeline {
agent any
parameters {
string(name: 'PR_NUMBER', defaultValue: '21', description: 'Pull Request Number to Merge')
}
environment {
GITHUB_TOKEN = credentials('GitHub_Credentials')
}
stages {
stage('Make HTTP Request') {
steps {
script {
def apiUrl = "https://api.github.com/repos/<USERNAME>/<REPOSITORY>/pulls/${params.PR_NUMBER}/merge"
echo "API URL: ${apiUrl}"
def response = sh(script: "curl -X POST $apiUrl -H 'Authorization: token ${GITHUB_TOKEN}'", returnStatus: true)
echo "HTTP Response Code: ${response}"
def responseBody = sh(script: "curl -X POST $apiUrl -H 'Authorization: token ${GITHUB_TOKEN}'", returnStdout: true).trim()
echo "Response Body: ${responseBody}"
if (response == 0) {
echo "HTTP request succeeded."
} else {
error("HTTP request failed with status code: ${response}")
}
}
}
}
stage('Merge Pull Request') {
steps {
script {
def prNumber = params.PR_NUMBER.toInteger()
def apiUrl = "https://api.github.com/repos/<USERNAME>/<REPOSITORY>/pulls/${params.PR_NUMBER}/merge"
def response = httpRequest(
url: apiUrl,
httpMode: 'POST',
authentication: 'GitHub_Credentials',
ignoreSslErrors: true
)
if (response.status == 200) {
echo "Pull request #${prNumber} merged successfully."
} else {
echo "Failed to merge pull request #${prNumber}."
error("HTTP Status: ${response.status}")
}
}
}
}
}
}
Needless to say it is coming with this error (from Jenkins)
hudson.AbortException: Fail: Status code 404 is not in the accepted range: 100:399 while calling https://api.github.com/repos/<USERNAME>/r<REPOSITORY>/pulls/24/merge
at jenkins.plugins.http_request.HttpRequestExecution.responseCodeIsValid(HttpRequestExecution.java:470)
at jenkins.plugins.http_request.HttpRequestExecution.processResponse(HttpRequestExecution.java:480)
at jenkins.plugins.http_request.HttpRequestExecution.authAndRequest(HttpRequestExecution.java:376)
at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:284)
Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 2708548f-f633-41ea-bac2-f371be2d2d8c
Caused: java.lang.IllegalStateException
at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:287)
at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:80)
at hudson.remoting.LocalChannel.call(LocalChannel.java:47)
at jenkins.plugins.http_request.HttpRequestStep$Execution.run(HttpRequestStep.java:404)
at jenkins.plugins.http_request.HttpRequestStep$Execution.run(HttpRequestStep.java:383)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Finished: FAILURE
Thanks