HTTP Request using Jenkins Pipeline Job - jenkins

I have been trying to do HTTP request in Jenkins Pipeline Job using HTTPRequest plugin
script{def response = httpRequest authentication: 'CredentialsID', url: "https://host/api/project-summary/kj/?kj=123"}
I have created Credentials with the ID 'CredentialID' in Jenkins.
I am getting the following error when I build the job-
I have already referred to this question on StackOverflow but it didn't help.
Could someone point out the mistake in this or is there some other approach that I can use to make HTTP Request from Jenkins Pipeline Job?
UPDATE: I have also created the Credentials with the ID - 'CredentialsID' in jenkins as shown below:

Related

How to invoke a jenkins pipeline A in another jenkins pipeline B in separate Jenkins master?

Important is separate Jenkins master.
I expecting something like
script { build job: 'https://second_instance_jenkins.mycompany.com/jobs/alt_build_job', parameters: [] }
However this does not work, I cannot connect to other master server, neither I know how to properly AUTH using this syntax. There are tons of examples how to invoke another job within same Jenkins, but not at separate independent Jenkins master!
Thanks.
You could enable the option Trigger builds remotely to trigger your job through the Jenkins API. Once you check the option, you will get the url you need to call in order to trigger the build
Use the following URL to trigger build remotely: JENKINS_URL/job/BranchDiffTests/build?token=TOKEN_NAME
Detailed instructions can be found here.
Once you have the job properly configured, you can send a GET request with the token as a parameter, and have the job executed remotely. Use Bash, Powershell, or whatever scripting language you use within your pipeline to create an API call to this endpoint, and once the call is made, the job should start running.
Found it.
I have to use Parameterized-Remote-Trigger as described here
https://www.jenkins.io/doc/pipeline/steps/Parameterized-Remote-Trigger/
I also going to post my working pipeline step. I wish there are more examples in manual.
steps {
triggerRemoteJob auth: CredentialsAuth(credentials: '7f89634523-1b42-440d-8053-aa3d523523441d'),
enhancedLogging: true,
job: 'Build/my-integration',
remoteJenkinsUrl: 'https://second_instance_jenkins.mycompany.com/',
useCrumbCache: true,
httpGetReadTimeout: 600,
httpPostReadTimeout: 600,
useJobInfoCache: true,
parameters: '''
PROJECT_NAME=core
BUILD_TYPE=Debug
PLATFORM=Linux
'''
}

Jenkins job should receive deployment status from Spinnaker pipeline (success/error)

I am new to use the spinnaker. I have set up the spinnaker with the helm chart in my k8s cluster. Everything is working fine. Now I have a new requirement to set up the notification for the spinnaker pipeline (Success/failure). But the requirement is tricky.
I read out about Spinnaker and Jenkins to post the success/failure status. Spinnaker has a notification mechanism to notify for each stage/pipeline about success/failure. We can set up this notification. Jenkins has the same mechanism. But the question is when the spinnaker pipeline successfully completed then it triggers the Jenkins job to post the spinnaker pipeline status on the portal. I have created a Jenkins stage that is successful triggers the Jenkins job but it is not catching the status or events from the spinnaker pipeline. In case, job spinnaker pipeline failed, it will not trigger the Jenkins to post the failure message. I tried to find out the solution but unable to do it. If anyone knows how to fix this issue, Please guide me or write me the solution in detail here.
Thanks
You do not need to create Jenkins stage in the same pipeline which you are monitoring. You can create additional Pipeline in Spinnaker which receive trigger from target pipeline. All you need is configure trigger in that new pipeline:
"triggers": [
{
"application": "demo-app",
"enabled": true,
"pipeline": "demo-pipe",
"status": [
"successful",
"failed"
],
"type": "pipeline"
}
]
You can extract required information (e.g. status) about upstream pipeline from this trigger.
If post the pipeline status on the internal portal in your terms means to send an HTTP Request than you should consider a Webhook Stage.
In runtime you have whole parent pipeline context in your downstream and can get it status with expression like ${trigger.parentExecution.status} to provide it in your request.

403 Error in Gitlab connection with Jenkins

I tried connecting Jenkins with Gitlab, while using System hooks in the Gitlab repository.
It is throwing me the following error :
Hook executed successfully but returned HTTP 403 window.location.replace('/login?from=%2Fjenkins%2Fproject%2FContinious_Integration');
Authentication required Hook executed successfully but returned HTTP 403 window.location.replace('/login?from=%2Fjenkins%2Fproject%2FContinious_Integration'); Authentication required
Open Jenkins global configuration -> require authorization for /project endpoint
Copy userid and API key for a Jenkins user who has authorization to
run the job
Create a webhook on GitLab to trigger the job,
Use HTTP basic auth (Below format)
user:apikey#gitlab/project/jobname
This worked for me..!

How to make a parameterized multibranch pipeline trigger?

Can you please give me an example of how should the Jenkinsfile config look to be triggered by a curl http trigger-buildwithparameters? I have tried:
properties([parameters(
[string(defaultValue: 'nothing',
description: 'gitcommit',
name: 'somecommit')])])
but it seems that this pipeline won't be triggered by buildwithparameters with a #somecommit parameter. Can bitbucket branch source/multibranch pipelines be triggered by a remote http calls with passed parameters?

How to build with parameters in Jenkins from Gitlab push?

I have GitLab Community Edition 8.15.2 successfully trigger pipeline projects in Jenkins 2.32.1 using a webhook. I want the gitlab push to trigger a build with parameters but the parameter value is null when it comes through so the build fails.
The gitlab webhook looks like:
http://jenkins.server:8080/project/project-a/buildWithParameters?MYPARAM=foo
In my pipeline project I echo the parameter value out with
echo "MYPARAM: ${MYPARAM}"
and it's not set to anything. Any ideas on where I've gone wrong?
UPDATE
The actual code I'm using in the pipeline is:
node {
try {
echo "VM_HOST: ${VM_HOST}"
echo "VM_NAME: ${VM_NAME}"
stage('checkout') {
deleteDir()
git 'http://git-server/project/automated-build.git'
}
stage('build') {
bat 'powershell -nologo -file Remove-MyVM.ps1 -VMHostName %VM_HOST% -VMName "%VM_NAME%" -Verbose'
}
...
}
}
The parameter VM_HOST has a default value but VM_NAME doesn't. In my Console output in Jenkins I can see:
[Pipeline] echo
VM_HOST: HyperVHost
[Pipeline] echo
VM_NAME:
I have been struggling with this for weeks. I had it working once, but I couldn't get it to work again, untill today. And the solution was mindblowingly obvious ofcourse...
Automatically for each pipeline job I ticked the following box:
Build when a change is pushed to GitLab. GitLab CI Service URL:
http://jenkins.dev:8080/project/MyProject
Then from GitLab I used the webhook to trigger the above.
Like you I tried to add /buildWithParameters and tried many other things that didn't work.
The problem was, I ticked the wrong checkbox!
Since I trigger the build from a GitLab webhook, the above checkbox (build when a...) does not have to be checked at all.
What needs to be checked is:
Trigger builds remotely (e.g., from scripts)
That checkbox provides you with a new URL:
Use the following URL to trigger build remotely:
JENKINS_URL/job/MyProject/build?token=TOKEN_NAME or
/buildWithParameters?token=TOKEN_NAME
Like all the documentation I came along states and as you can see, the URL now no longer starts with /project, but with /job instead!
So tick that box and change your URL accordingly:
http://jenkins.server:8080/**job**/project-a/buildWithParameters?token=TOKEN_NAME&MYPARAM=foo
Least I want to mention the token:
In the GitLab webhook there is a seperate field for "token", which states:
Use this token to validate received payloads. It will be sent with the request in the X-Gitlab-Token HTTP header.
So, the token provided there will be sent along the request as a HTTP header.
This is the token which can be provided globally in the Jenkins setup.
The token you must provide in the Jenkins job when ticking the box Use the following URL to trigger build remotely must be send in the URL as GET parameter, just like the example shows.
Final note: personally I have never got this working completely, because I don't get the Jenkins CSRF protection off my back. Disabling it gives me another error. However, hopefully the above does fix the problem for you and others.
GitLab plugin does not allow you to pass arbitrary parameters. In their project there is an open issue for it that deserves to be upvoted.
My convoluted solution was to use the desired values for the push trigger as the default parameters of the job. Then I used the Parameterized Scheduler plugin to use other values in the scheduled executions.
The problem is that I got a bad usability for the job when it was manually run, since the default parameters were appropriate for the push hook.
I found the solution here https://www.jittagornp.me/blog/jenkins-gitlab-webhook/
I verified it with Jenkins 2.263.1 and GitLab Community Edition 13.6.1
Your webhook url will look like
https://hunter:11a403302a4f01b9b4975c0ac27441a5cc#jenkinsservername.com/job/yourjenkinsproject/buildWithParameters?token=Aju9ryHUu6t7W8wLSeCWtY2bWjzQduYNPyY7B3gs&yourparam=yourvalue
"hunter" ist your username in Jenkins.
The following is the Jenkins API Token you have to create in your Jenkins User Managment independent of the project.
The last Token is the one you specify in the jenkins project options under "Trigger builds remotely (e.g., from scripts)"
The last thing is to add your Parameter and value to the url with &param=value

Resources