27
votes

I am getting the following error when i try triggering a build using the following command:

curl http://jenkins_server:port/jenkins/job/job_name/build?token=token_name

Output:

Authentication required

<-- You are authenticated as: anonymous
Groups that you are in:

Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
->

I have admin rights and have also enabled 'Authentication Token'. I also have Build, Discover and Read rights on Job. I am using Jenkins 1.614.

I did check several posts online but could not find anything that works for me.

Tried few options such as
1) curl -X POST http://jenkins_server:port/jenkins/job/job_name/build?token=token_name
2) curl -u user:API (Prints a long HTML page)

Any suggestions.

9

9 Answers

26
votes

I install Build Token Root Plugin to solve this issue before

https://wiki.jenkins-ci.org/display/JENKINS/Build+Token+Root+Plugin

Then as the same, setup Authentication Token

Finally, either use curl to trigger remote build (Be careful the escape character "\")

curl http://JENKINS_URL/buildByToken/build?job=JOB_NAME\&token=TOKEN_NAME

or paste the URL to your browser (No needs escape character "\")

http://JENKINS_URL/buildByToken/build?job=JOB_NAME&token=TOKEN_NAME

If you see Succeed, it means that trigger remote Jenkins successfully.

Note that, you don't have to setup build, discover, and read rights on Job

For more information, you could reference to https://cloudbees.zendesk.com/hc/en-us/articles/204338790-Why-are-builds-not-being-triggered-with-Build-Token-Root-Plugin-

14
votes

I think there's no need to set up any kind of plugin to make it works, simply do this BATCH CMD:

curl -X POST http://USER_ID_JENKINS_RECEIVER:TOKEN_OF_USER_ID_JENKINS_RECEIVER@URL_JENKINS_RECEIVER:PORT/job/NAME_OF_JOB/buildWithParameters?token=TOKEN_JOB_JENKINS_RECEIVER
  • To see you USER_ID_TOKEN, go to your username at the top-right of jenkins > go to Configure > Click on API Token and it will be displayed.

  • You must configure a token on the JOB_OF_JENKINS_RECEIVER

I have to say that it works perfectly on Jenkins without any kind of permissions to anonymous users.

7
votes

I had the same problem after setting up a test Jenkins server. The solution was to authenticate using my Jenkins admin password:

curl -u admin:<mypassword> http://localhost:8080/job/Test/build?token=<mytoken>

Use the same username and password you use to login to Jenkins. There was no need to install additional plugins.

3
votes

I suggest you try Jenkins Python API. It provides an build_job action that is super easy to trigger a job.

build_job(name, parameters=None, token=None)
   Trigger build job.

   Parameters:  
   name – name of job
   parameters – parameters for job, or None, dict
   token – Jenkins API token
2
votes

In my case i had the URL and token correct, but had forgotten one of the required permissions for the API user that triggers the URL/build.

The permissions have to minimally include the list here:

  • Overall - Read
  • Job - Build
  • Job - Read
  • Job - Workspace
0
votes

Install Parameterized Build plugin

https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build

If you are using an authorization token to trigger the builds (Job -> Configure -> 'Build Triggers' -> 'Trigger builds remotely (e.g., from scripts)'), you can access:

curl -X POST http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value

0
votes

I also have faced the same issue and resolved it.

Consider, you have 2 Jenkins A and B. If you want to execute a build job present in Jenkins B, then you have to give the API token of Jenkins B. If you provide the API token of Jenkins A, it will show the authentication issue only.

This is the mistake I have did and it got resolved once I have changed the API token properly according to the required Jenkins.

-1
votes

On 1.625.3, giving Anonymous read access to almost everything worked for me.

UPDATE: I guess I should clarify since this got downvoted. I was able to reproduce that error message, and when I turned on read access for the Anonymous user on all of the access types, it resolved the problem. Just saying...

-1
votes

This works for me:

tokenuser = You need login with the user and check token config jenkins -> admin users

user = username

curl -XPOST --silent --show-error --user user:tokenuser "http://ipserver/jenkins/buildByToken/build?job=JOBNAME&token=7RJWiIwD5cW3S6QjwF7a"

Example:

curl -XPOST --silent --show-error --user desarrollador:b9611288e4159ea9fb7857faeadd06fe "http://ipserver/jenkins/buildByToken/build?job=DeployGF&token=7RJWiIwD5cW3S6QjwF7a"