-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Installation of credentials via API #5
Comments
I'm not a native java/groovy person... so I cobbled this together after looking thru lots and LOTS of other groovy examples and pouring thru the source code: import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import org.apache.commons.fileupload.FileItem
import com.cloudbees.plugins.credentials.domains.*
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials
import java.io.FileInputStream
domain = Domain.global()
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
def filey = new FileInputStream('/tmp/google-creds-on-jenkins-master.json')
fileItem = [ getSize: { return 1L }, getInputStream: { return filey } ] as FileItem
def ServiceAccount = new JsonServiceAccountConfig(fileItem, null)
def GoogleAccount = new GoogleRobotPrivateKeyCredentials("project-id", ServiceAccount, null)
store.addCredentials(domain, GoogleAccount) |
@msheiny This is exactly what I am looking for! How are you currently running that? As part of a set of startup scripts? |
@chris-vest yay drops it into groovy.init.d .. see https://github.com/freedomofpress/ansible-role-jenkins-config/blob/b0edb027f474a004d91622a48269b9328b74a0f0/templates/creds.groovy#L73 |
@msheiny Very much appreciated, thank you! |
Hi @msheiny, Your code didn't work for me, so I did some improvements now it's working. (Jun/2019). import org.apache.commons.io.FileUtils
import org.apache.commons.fileupload.FileItem
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials
def jenkinsInstance = Jenkins.getInstance()
// Google Credential
def googleCredentialId = "my-credential-id"
def googleCredentialFile = new File("/path/to/my/json/file.json")
def googleCredentialFileInputStream = new FileInputStream(googleCredentialFile)
def googleCredentialFileItem = [ getName: { return googleCredentialFile.getName() }, getSize: { return 1L }, getInputStream: { return googleCredentialFileInputStream }, get: { return FileUtils.readFileToByteArray(googleCredentialFile) } ] as FileItem
def googleJsonServiceAccountConfig = new JsonServiceAccountConfig(googleCredentialFileItem, null)
def googleCredential = new GoogleRobotPrivateKeyCredentials(googleCredentialId, googleJsonServiceAccountConfig, null)
// Saving
def domain = Domain.global()
def store = jenkinsInstance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
store.addCredentials(domain, googleCredential) Hope that it helps you guys too. |
Resolved in #48 |
As per this example:
arangamani/jenkins_api_client#162
Is it possible to install Google Service account credentials (Google Service Account from JSON or P12) via an API request such as cURL ?
Reason for asking is this would then allow full scripting of the Jenkins job creation (config.xml) and credentials associated with a GCP Git Repository.
The text was updated successfully, but these errors were encountered: