15

How can I create a Jenkins Credential via REST API or Jenkins CLI? The credential should be of type "secret file", instead of a username / password combination.

The question is similar to this question, but not the same or a duplicate.

Community
  • 1
  • 1
Amir Hadi
  • 308
  • 2
  • 9

3 Answers3

10

You can do it as follows:

curl -X POST \
 https://jenkins.local/job/TEAM-FOLDER/credentials/store/folder/domain/_/createCredentials \
 -F secret=@/Users/maksym/secret \
 -F 'json={"": "4", "credentials": {"file": "secret", "id": "test", 
"description": "HELLO-curl", "stapler-class": 
"org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl", 
"$class": 
"org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl"}}'

just finished with it today https://www.linkedin.com/pulse/upload-jenkins-secret-file-credential-via-api-maksym-lushpenko/?trackingId=RDcgSk0KyvW5RxrBD2t1RA%3D%3D

Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
lumaks
  • 308
  • 3
  • 9
  • Did you find a way to update an existing creds file? Repeating this request with a different file gets ignored. – doughgle Dec 06 '18 at 15:12
  • I had to do a check if secret is present via API, then delete it and upload new one, did it via Ansible, had the same problem – lumaks Dec 08 '18 at 11:16
  • I'm getting `HTTP ERROR 403 No valid crumb was included in the request` – Maoz Zadok May 12 '20 at 15:30
  • You can check a number of options here about that error https://github.com/spinnaker/spinnaker/issues/2067 – lumaks May 14 '20 at 20:04
8

To create Jenkins credentials via the CLI you can use the create-credentials-by-xml command:

java -jar jenkins-cli.jar -s <JENKINS_URL> create-credentials-by-xml  system::system::jenkins _ < credential-name.xml

The best way to know the syntax of this is to create a credential manually, and then dump it:

java -jar jenkins-cli.jar -s <JENKINS_URL> get-credentials-as-xml system::system::jenkins _ credential-name > credential-name.xml

Then you can use this XML example as a template, it should be self-explanatory.

Zioalex
  • 3,441
  • 2
  • 33
  • 30
giorgiosironi
  • 1,077
  • 1
  • 11
  • 18
2

If you want to update an existing secret file, the simplest way I found was to delete and re-create.

A delete request, to extend @lumaks answer (i.e. with the same hostname, folder name and credentials id), looks like:

curl -v -X POST \
-u "user:password" \
https://jenkins.local/job/TEAM-FOLDER/credentials/store/folder/domain/_/credential/test/doDelete

This will return either HTTP status code 302 Found or 404 Not Found for existing and non-existing creds file respectively.

doughgle
  • 827
  • 1
  • 9
  • 18
  • Is there some full Credentials Plugin API reference? It is kind of struggle to search a bits of information about it all over the internet. – kirill.login Jul 23 '19 at 05:45