Forked from iocanel/jenkins-setup-credentials.groovy
Last active
November 28, 2021 16:40
-
-
Save chrisvire/383a2c7b7cfb3f55df6a to your computer and use it in GitHub Desktop.
Setup Jenkins Credentials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jenkins.model.* | |
import com.cloudbees.plugins.credentials.* | |
import com.cloudbees.plugins.credentials.common.* | |
import com.cloudbees.plugins.credentials.domains.* | |
import com.cloudbees.plugins.credentials.impl.* | |
import com.cloudbees.jenkins.plugins.sshcredentials.impl.* | |
import org.jenkinsci.plugins.plaincredentials.* | |
import org.jenkinsci.plugins.plaincredentials.impl.* | |
import hudson.util.Secret | |
import hudson.plugins.sshslaves.* | |
import org.apache.commons.fileupload.* | |
import org.apache.commons.fileupload.disk.* | |
import java.nio.file.Files | |
domain = Domain.global() | |
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore() | |
priveteKey = new BasicSSHUserPrivateKey( | |
CredentialsScope.GLOBAL, | |
"jenkins-slave-key", | |
"root", | |
new BasicSSHUserPrivateKey.UsersPrivateKeySource(), | |
"", | |
"" | |
) | |
usernameAndPassword = new UsernamePasswordCredentialsImpl( | |
CredentialsScope.GLOBAL, | |
"jenkins-slave-password", "Jenkis Slave with Password Configuration", | |
"root", | |
"jenkins" | |
) | |
secretText = new StringCredentialsImpl( | |
CredentialsScope.GLOBAL, | |
"secret-text", | |
"Secret Text Description", | |
Secret.fromString("some secret text goes here")) | |
factory = new DiskFileItemFactory() | |
dfi = factory.createItem("", "application/octet-stream", false, "filename") | |
out = dfi.getOutputStream() | |
file = new File("/path/to/some/file") | |
Files.copy(file.toPath(), out) | |
FileCredentailsImpl can take a file from a do | |
secretFile = new FileCredentialsImpl( | |
CredentialsScope.GLOBAL, | |
"secret-file", | |
"Secret File Description" | |
dfi, // Don't use FileItem | |
"", | |
"") | |
store.addCredentials(domain, priveteKey) | |
store.addCredentials(domain, usernameAndPassword) | |
store.addCredentials(domain, secretText) | |
store.addCredentials(domain, secretFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To enable slave -> master access control, you drop a file named "slave-to-master-security-kill-switch" in $JENKINS_HOME/secrets. It has to be a text file with the one line "false" in it. (Yes false, not true -- I don't get it either.)
You could write a groovy script to do that.