Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a Jenkins user authentication details be "passed" to a script which uses Jenkins API to create jobs?

I have a script that delete and re-create jobs through curl HTTP-calls and I want to get rid of any hard-coded "username:password". E.g. curl -X POST $url --user username:password

Considerations:

  • Jenkins CLI (probably not an option). One should be able to achieve the same with the CLI as with Jenkins API (creating jobs etc) but as far as I understand Jenkins CLI is not a good alternative for me since jobs created with will only appear in Jenkins after restarting or a "Reload Configuration from Disk", and that would cancel any other running jobs.

  • API token. Can't find out how to get the user token and then pass it as a parameter to the script, but that may be a solution..

like image 454
HenrikSN Avatar asked May 22 '12 08:05

HenrikSN


People also ask

How do I authenticate Jenkins API?

If your Jenkins server requires authentication (and it SHOULD), you'll see a message saying "Authentication Required". The Jenkins API uses HTTP BASIC authentication and requires a username as well as an API token to connect. Then click the box named "Show API Token", and copy the token to your clipboard.

How do I add user credentials in Jenkins?

From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click Manage Jenkins > Manage Credentials. Under Stores scoped to Jenkins on the right, click on Jenkins. Under System, click the Global credentials (unrestricted) link to access this default domain. Click Add Credentials on the left.


2 Answers

Try this way: (for example delete the job)

curl --silent --show-error http://<username>:<api-token>@<jenkins-server>/job/<job-name>/doDelete 

The api-token can be obtained from http://<jenkins-server>/user/<username>/configure.

like image 86
PiotrO Avatar answered Sep 22 '22 04:09

PiotrO


This worked for me:

curl -u $username:$api_token -FSubmit=Build 'http://<jenkins-server>/job/<job-name>/buildWithParameters?environment=' 

API token can be obtained from Jenkins user configuration.

like image 35
Deepti Avatar answered Sep 23 '22 04:09

Deepti