Problem with sending Get request from jenkins pipeline job from Jenkins slave - jenkins

I am using HTTP-request in Jenkins pipeline job to send Get request from Jenkins slave, the response code is 200, response content is null, but if I send the request from Jenkins master, I can get response content correctly, how can I solve this problem? below is the command I used HTTP-request in Jenkins pipeline
httpRequest acceptType: 'APPLICATION_JSON',
authentication: env.MY_CREDENTIAL,
contentType: 'APPLICATION_JSON',
url: env.MyURI,
wrapAsMultipart: false

I see nothing wrong with your request but as you commented, looks like the response is string?
You can add consoleLogResponseBody to see how the response looks like.
def response = httpRequest(
authentication: env.MY_CREDENTIAL,
consoleLogResponseBody: true,
url: env.MyURI,
wrapAsMultipart: false
)
And you should be able to parse it simply like this
def json = readJSON(text: response.content)

Related

Jenkins build: notify Bitbucket cloud

I'm using Jenkins 2.346.2
The repository is located on bitbucket.org (cloud, not local server).
I want the build status to be sent to bitbucket and to be displayed as the PR build status.
I'm trying the plugin: https://plugins.jenkins.io/bitbucket-build-status-notifier/
The configuration is (multibranch pipeline project):
def notifyBitbucket(String state) {
notifyBitbucket(
commitSha1: 'a0e5012be0e8e89d122cc773a964c0en3a1a656b2',
credentialsId: 'jenkins_bitbucket_ssh',
disableInprogressNotification: false,
considerUnstableAsSuccess: true,
ignoreUnverifiedSSLPeer: true,
buildStatus: state,
buildName: 'Performance Testing',
buildUrl: 'https://bitbucket.org',
includeBuildNumberInKey: false,
prependParentProjectKey: false,
projectKey: '',
stashServerBaseUrl: 'https://bitbucket.org')
}
But what I get is a returned bitbucket page saying 'Resource not found'.
Currently, the only credentials I can use to connect to bitbucket is SSH key pair.
And they work okay for pulling the code. I'm trying to use this key for the notification plugin as well. Is this wrong?
Could anyone let me know how to specify the path to the project in this case, please?
One option you can consider is using the Bitbucket API, which would remove the need for an external plugin. The endpoint you need to call is:
${BITBUCKET_API_HEAD}/commit/${env.COMMIT_HASH}/statuses/build
More on this in the documentation. Here is how I have done it:
httpRequest([
acceptType : 'APPLICATION_JSON',
authentication : '<credentials>',
contentType : 'APPLICATION_JSON',
httpMode : 'POST',
requestBody : '''{
"key":"<unique-key>",
"name":"PR-Branch-Build",
"url":"<path-to-jenkins-build>/''' + env.BUILD_NUMBER + '''/pipeline",
"description":"Build status: '''+ BUILD_STATUS +'''",
"state":"'''+ BUILD_STATUS +'''"
}''',
responseHandle : 'NONE',
url : "${BITBUCKET_API_HEAD}/commit/${env.COMMIT_HASH}/statuses/build",
validResponseCodes: '200,201'
])

Unable to get the payload from GitHub web hook trigger in jenkins pipeline

I have configured a Github web hook with the below settings:
Payload URL: https:///github-webhook/
Content Type: application/x-www-form-urlencoded
Events : Pushes, Pull Requests
The Jenkins job that I have, is a pipeline job that has the below enabled:
Build Trigger: GitHub hook trigger for GITScm polling
With the above configuration, I see that in response to an event ie; push/PR in GitHub, the jenkins job gets triggered successfully. In GitHub, under Recent Deliveries for the web hook, I see the details of the payload and a successful Response of 200.
I am trying to get the payload in Jenkins Pipeline for further processing. I need some details eg: PR URL/PR number, refs type, branch name etc for conditional processing in the Jenkins Pipeline.
I tried accessing the "payload" variable (as mentioned in other stack overflow posts and the documentations available around) and printing it as part of the pipeline, but I have had no luck yet.
So my question is, How can I get the payload from GitHub web hook trigger in my Jenkins Pipeline ?
You need to select Content type: application/json in your webhook in GitHub. Then you would be able to access any variable from the payload GitHub sends as follows: $. pull_request.url for pr url, for example.
Unsure if this is possible.
With the GitHub plugin we use (Pipeline Github), PR number is stored in the variable CHANGE_ID.
PR URL is pretty easy to generate given the PR number. Branch name is stored in the variable BRANCH_NAME. In case of pull requests, the global variable pullRequest is populated with lots of data.
Missing information can be obtained from Github by using their API. Here's an example of checking if PR is "behind", you can modify that to your specific requirements:
def checkPrIsNotBehind(String repo) {
withCredentials([usernamePassword(credentialsId: "<...>",
passwordVariable: 'TOKEN',
usernameVariable: 'USER')]) {
def headers = ' -H "Content-Type: application/json" -H "Authorization: token $TOKEN" '
def url = "https://api.github.com/repos/<...>/<...>/pulls/${env.CHANGE_ID}"
def head_sha = sh (label: "Check PR head SHA",
returnStdout: true,
script: "curl -s ${url} ${headers} | jq -r .head.sha").trim().toUpperCase()
println "PR head sha is ${head_sha}"
headers = ' -H "Accept: application/vnd.github.v3+json" -H "Authorization: token $TOKEN" '
url = "https://api.github.com/repos/<...>/${repo}/compare/${pullRequest.base}...${head_sha}"
def behind_by = sh (label: "Check PR commits behind",
returnStdout: true,
script: "curl -s ${url} ${headers} | jq -r .behind_by").trim().toUpperCase()
if (behind_by != '0') {
currentBuild.result = "ABORTED"
currentBuild.displayName = "#${env.BUILD_NUMBER}-Out of date"
error("The head ref is out of date. Please update your branch.")
}
}
}

Parameterized remote job via triggerRemoteJob() function using Tokens

Tried to search a few sites, including Parameterized remote job is triggered but console says failure
I am attempting to migrate a token based job from existing (using curl) method of calling remote job to plugin based call as follows:
Remote Jenkins Setup: (myserver:8080) Job: MyPipelineFirstJob
Under Job configuration : Build Triggers --> "Trigger builds remotely (e.g., from scripts)" --> Authentication Token --> 108801
Existing job: On Local Jenkins:
curl -v --silent -X POST http://myserver:8080/job/MyPipelineFirstJob/buildWithParameters --data token=108801 --data RELEASE=9.2 --data ARCHITECTURE=ppc64le --data IP=9.99.999.99
New job on local Jenkins:
Now, I need to translate the above to use parameterized-remote-trigger-plugin. So Apart from Remote Host, etc, I have chosen the Auth type as follows in the Global configuration: "Parameterized Remote Trigger Configuration"
"Enable 'build token root' support" is unchecked -- Do not know what this means
Authentication --> Bearer Token Authentication
I see a WARNING message as "Address looks good, but a connection could not be established."
I am calling the below funciton to trigger the remote job:
def handle = triggerRemoteJob(remoteJenkinsName: 'Perf_Jenkins_Server', job: 'MyPipelineFirstJob/buildByToken/buildWithParameters', auth: "108801", parameters: 'RELEASE=HMC9.2.951.2,ARCHITECTURE=ppc64le,HMC_MACHINE=9.99.999.9998')
I have passed the string "108801" based on this site https://www.jenkins.io/doc/pipeline/steps/Parameterized-Remote-Trigger/ which says:
BearerTokenAuth
token (optional)
Type: String
Build Failure: With the above configuration, when build the job, I get this error:
22:07:12 java.lang.ClassCastException: class org.jenkinsci.plugins.ParameterizedRemoteTrigger.pipeline.RemoteBuildPipelineStep.setAuth() expects class org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.Auth2 but received class java.lang.String
22:07:12 at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:492)
22:07:12 at org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:429)
22:07:12 at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:331)
22:07:12 at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:269)
22:07:12 at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
22:07:12 at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
22:07:12 at sun.reflect.GeneratedMethodAccessor493.invoke(Unknown Source)
22:07:12 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
22:07:12 at java.lang.reflect.Method.invoke(Method.java:508)
So, I tried to remove the auth field, and passed it as part of parameters:
def handle = triggerRemoteJob(remoteJenkinsName: 'Perf_Jenkins_Server', job: 'MyPipelineFirstJob/buildByToken/buildWithParameters', parameters: 'token="108801",RELEASE="HMC9.2.951.2",ARCHITECTURE=ppc64le,HMC_MACHINE="9.99.999.9998"')
Note: I have also attempted to add double quotes around the parameter values. Having made these changes, and attempt to build, I get the following error:
22:19:12 ################################################################################################################
22:19:12 Parameterized Remote Trigger Configuration:
22:19:12 - job: MyPipelineFirstJob/buildByToken/buildWithParameters
22:19:12 - remoteJenkinsName: Perf_Jenkins_Server
22:19:12 - parameters: [token="108801",RELEASE="HMC9.2.951.2",ARCHITECTURE=ppc64le,HMC_MACHINE="9.99.999.998"]
22:19:12 - blockBuildUntilComplete: true
22:19:12 - connectionRetryLimit: 5
22:19:12 - trustAllCertificates: false
22:19:12 ################################################################################################################
22:19:12 Connection to remote server failed [404], waiting to retry - 10 seconds until next attempt. URL: http://myserver:8080/job/MyPipelineFirstJob/job/buildByToken/job/buildWithParameters/api/json, parameters:
22:19:22 Retry attempt #1 out of 5
22:19:22 Connection to remote server failed [404], waiting to retry - 10 seconds until next attempt. URL: http://myserver:8080/job/MyPipelineFirstJob/job/buildByToken/job/buildWithParameters/api/json, parameters:
22:19:32 Retry attempt #2 out of 5
Did you notice the additional "job" word : "buildByToken/job/buildWithParameters" in the above o/p? Not sure why!
Questions:
Is the Authentication type of "Bearer Token Authentication" the correct option that matches with the requirement of existing method?
Have I passed the parameters correctly?
How to overcome the failures seen above?
Found the solution: The parameters needs to be separated by a new line. Not a comma or space. So, I added '\n' char between each parameter as shown below and it worked!
def handle = triggerRemoteJob(remoteJenkinsName: 'Perf_Jenkins_Server', job: 'MyPipelineFirstJob', parameters: 'token=108801\nRELEASE=9.2.951.2\nARCHITECTURE=x86_64\nMACHINE_IP="9.99.999.998')
Ref: The below link has an example that uses "\n" as parameter separator.
https://github.com/jenkinsci/parameterized-remote-trigger-plugin/blob/master/README_PipelineConfiguration.md
Note: The above link refers to Snippet Generator. However, that Generator doesn't support "triggerRemoteJob" yet! May be, I would have solved my issue faster!
Jenkins Version: Jenkins 2.249.1
Parameterized Remote Trigger Plugin Version: 3.1.5.1

how to sent file(zip) using httprequest plugin jenkins

I have a requirement to upload zip file to appDynamics, i need to use the httpsrequest plugin for that from my jenkins pipeline
upload request for appdynamics:
curl -v -H Content-Type:application/octet-stream --upload-file UISampleApp.app.dSYM.zip --user Example account:Example-License-Key-4e8ec2ae6cfe https://api.eum-appdynamics.com/v2/account/Example+account/ios-dsym
we are using a shell to execute the above request now but I am trying to find out how to sent multiple zip files using httpsRequest plugin
Following Code worked for me :
def response = httpRequest(acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_ZIP',
customHeaders : [[name: "authorization" , value : "${authToken}"],[name: 'x-username' , value: 'admin']],
httpMode: 'POST', ignoreSslErrors: true,
multipartName: '<fileName>', timeout: 900,
responseHandle: 'NONE', uploadFile: "<filePath>",
url: "${url}")
Looks like httprequest plugin does not support uploading zip file. This is my observation.
I think upload will use Content-Type: multipart/form-data. But httpRequest plugin is not supporting this type. However it supports APPLICATION_OCTETSTREAM(ContentType.APPLICATION_OCTET_STREAM)
Could you post output from your curl?

Artifactory build options for maven build and issues

I am new to Artifactory and I find that there is a lot of documentation on the different ways to get artifacts to Artifactory but nothing that is complete.
For example:
The pipeline plugin used by jenkins:
sample code:
node {
def server = Artifactory.newServer url: SERVER_URL, credentialsId: CREDENTIALS
def rtMaven = Artifactory.newMavenBuild()
stage 'Build'
git url: 'https://github.com/jfrogdev/project-examples.git'
stage 'Artifactory configuration'
rtMaven.tool = MAVEN_TOOL // Tool name from Jenkins configuration
rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server
rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server
def buildInfo = Artifactory.newBuildInfo()
stage 'Exec Maven'
rtMaven.run pom: 'maven-example/pom.xml', goals: 'clean install', buildInfo: buildInfo
stage 'Publish build info'
server.publishBuildInfo buildInfo
}
I am not sure how to set up some variables like CREDENTIALS. specially if I want to use api key not user id and password.
Also, if I want to use the Artifactory rest API to build and promote my project(Maven Build). should I be using:
curl -X PUT "http://localhost:8080/artifactory/api/build" -H "Content-Type: application/json" --upload-file build.json
Where build.json is the sample JSON at https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API under Build Upload.
If i use the API do i still need the above jenkins plugin code or can just use the API?
Where do I pass my credentials (Userid and APi key) in the curl command?
If any experienced user could guide me with these questions that will be of great help.

Resources