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..
If you want to write a script to automate creation of jobs using the Jenkins API, you can use one of the API clients to do that. A ruby client for Jenkins is available at https://github.com/arangamani/jenkins_api_client
The API client can be used to perform a lot of operations.
With Jenkins CLI you do not have to reload everything - you just can load the job (update-job command). You can't use tokens with CLI, AFAIK - you have to use password or password file.
Token name for user can be obtained via
http://<jenkins-server>/user/<username>/configure
- push on 'Show API token' button.Here's a link on how to use API tokens (it uses
wget
, butcurl
is very similar).API token is the same as password from API point of view, see source code uses token in place of passwords for the API.
See related answer from @coffeebreaks in my question python-jenkins or jenkinsapi for jenkins remote access API in python
Others is described in doc to use http basic authentication model
In order to use API tokens, users will have to obtain their own tokens, each from
https://<jenkins-server>/me/configure
orhttps://<jenkins-server>/user/<user-name>/configure
. It is up to you, as the author of the script, to determine how users supply the token to the script. For example, in a Bourne Shell script running interactively inside a Git repository, where.gitignore
contains/.jenkins_api_token
, you might do something like:This worked for me:
API token can be obtained from Jenkins user configuration.
I needed to explicitly add POST in the CURL command:
I also have the SafeRestart Plugin installed, in case that makes a difference.