Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload secret file credentials to Jenkins with REST / CLI

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.

like image 746
Amir Hadi Avatar asked Mar 30 '17 19:03

Amir Hadi


2 Answers

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.

like image 132
giorgiosironi Avatar answered Sep 22 '22 18:09

giorgiosironi


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

like image 32
lumaks Avatar answered Sep 20 '22 18:09

lumaks