How to trigger Jenkins builds remotely using URL with parameters - jenkins

I'm trying to trigger build in Jenkins remotely using URL.
the URL is : "http://myJenkinsURL/job/myProjectName/build?token=SOME_TOKEN&EXTID=lkjsdfljsdflkjsdfjklsdflkj"
The problem is when i try to trigger this build it redirect me to my Jenkins and ask me to insert the parameter but i already passed it over the URL.
Is there something that I'm missing in the process?

https://wiki.jenkins.io/display/JENKINS/Parameterized+Build has the gory, but it mentions that you want a URL like this (note buildWithParameters):
http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value
so your call might look like this:
curl -X POST "http://myJenkinsURL/job/myProjectName/buildWithParameters?token=SOME_TOKEN&EXTID=lkjsdfljsdflkjsdfjklsdflkj"

Related

How to get Script Path from each Jenkins Jobs using Groovy Console Script/Jenkins API

I have a requirement to fetch Script path from Jenkins Job .
Please find the below screenshot to understand the requirement more clearly.
I have checked in google to get some Groovy Console Script which lists details of each Job.
By using the jenkins.model.Jenkins.getJobNames() method ,i was able to get all Jobs in Jenkins , but actually the requirement is to get the Scriptpath value, mentioned inside each those jobs.
Which Jenkins Class/method can provide those details or is any other way to fetch those details using Jenkins API?
By reading the config.xml for each job and getting the Scriptpath tag from it will also give me the information needed, But I was looking for a REST method or Jenkins Groovy Script method which can give me this Script path Information for each Job.
Open your job configuration page.
Change the configure at the end of url to config.xml and press Enter
The new opening page loads xml content
Find Scriptpath value in tag <scriptPath>xxxxxx</scriptPath>
So you can send http requset to job's confim.xml url and parse the content to get the value programmatically

Trigger Jenkins job via Slack command - 403_client_error

I'm trying to do a pretty simple thing I believe - trigger a Jenkins job from a slash command in slack. I want to type /dump in slack and that should trigger particular jenkins job. I'm using Slack Slash command to achieve this and the setup is very simple - type command, type URL, select POST/GET method.
I tried two methods - first was to use Build Authorization Token Root Plugin following this tutorial - in this case I configured Slack slash command to make GET request on the URL: http://jenkins_url:8080/job/db-dump-setup/build?token=MY_TOKEN - If I put the URL to my browser, the job gets triggered, if I run the /dump command, slack responds with:
slackbot [3:41 PM]
Darn - that slash command didn't work (error message: `403_client_error`). Manage the command at text.
The second approach I tried was following this tutorial. In this case I created new user in Jenkins and got API token. When running this command from my terminal: curl -X POST http://USER:TOKEN#slack_url:8080/job/db-dump-setup/build, the job gets again triggered, however when configuring the Slack slash command with the same URL and making POST request, I get the same error: 403_client_error
I tried to google this specific error but didn't find anything useful, also there are no more details for the error or any logs which could help me to troubleshoot this further.
Anyone run into something similar and knows how could I get this resolved?
I was able to resolve this so answering my own question in a case someone else runs into this. In order to make the first case work (Using the GET request), I had to enable the "Allow anonymous read access" in Jenkins, in Manage Jenkins -> Configure Global Security as per the screenshot below.
You should not need to enable anonymous access.Your jenkins requires authentication. Update your url like http://user:auth-token#jenkins-host:port.Auth token can be generated at the following URL JENKINS_HOST/me/configure

Trigger Jenkins from gogs webhook with param

I have a webook url http://jenskinsserver/gogs-webhook/?job=build which will trigger the jenkins job on any event from gogs.
Now i want to trigger a parameterized job and i want the param to be sent via webhook url. How to add params in gogs webhook url?
You can simply add a Build Remote Trigger for this to happen. Go toBuild Triggers => Trigger builds remotely (e.g., from scripts) and set the API Token.
You have to configure the General => This project is parameterized and choose whatever you like from the parameters available.
Now in the url, http://jenskinsserver/gogs-webhook/buildWithParameters?token=<api token provided>&param1=value1&param2=value2 provide params like this and access them by using $param1, $param2 in the scripts.

Stash Pull Request Builder plugin for Jenkins custom variables in comments

I have the Stash pull request builder plugin working great in Jenkins, it see's a pull request and kicks off the Jenkins job which runs some tests and puts a generic comment on the pull request saying pass or fail.
The problem I'm having is when the plugin comments back to the pull request I'd like to use a variable I create in the post build section, I've tried creating the variable as an environment variable but it seems the comments part of the plugin can only see the built in Jenkins variables like ${BUILD_NUMBER} anything else just prints out the name directly.
I've had a look at envinject, but I'm not sure how to set a variable from it in the post build section, or even if the variables it creates would be seen by the Pull Request Builder Plugin.
I did a quick test with my Jenkins/Stash platform.
I have a job using this custom variable:
I've added a post-build step to publish a custom comment:
We can see that my SCM_REVISION environment variable is displayed in Stash:
I hope it helped :)

Select branch via URL param for Jenkins job build

I make builds in Jenkins many times a day and I would like to automate it a little bit, but still access it through web interface.
I'm already selecting job with URL (and building this URL with very simple Alfred workflow) but I would like to also select a branch – we have many branches in repo and 90% of time I want to select master of develop
jenkins.skypicker.com:8080/job/beta/build works great for selecting job, but is there a way to select branch, something like jenkins.skypicker.com:8080/job/beta/build?branch=origin/master?
You can, theoretically, use the Parameterized Build feature to define a parameter later to be used with GIT plugin configuration. Then you should be able to use the format of url like this:
http://jenkins.skypicker.com:8080/job/beta/buildWithParameters?BRANCH=origin%2Fmaster
Be careful with the special character in the branch name though. If you can avoid it by specifying a branch name in the form of origin/$BRANCH it would be safer...
Remember that in order to start the build process one must use POST method, not GET - just a side note...
Anser by #Łukasz-rżanek is right, just adding a few notes on which I've stumbled
Set this in Job configuration:
and run it
#!/bin/bash
JOB_NAME="Beta"
JOB_BRANCH=""
JOB_TOKEN="TOKEN"
JENKINS_URL="http://jenkins..."
crumb=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`
curl -H $crumb -X POST $JENKINS_URL/job/$JOB_NAME/buildWithParameters?token=$JOB_TOKEN

Resources