-
-
Save hemanth22/2dceac75e2ea6f04383dea5f84f9b9ff to your computer and use it in GitHub Desktop.
This is a collection of groovy scripts I gathered and use for bootstrapping Jenkins. Most of this can also be achieved with the CasC Plugin https://github.com/jenkinsci/configuration-as-code-plugin
This file contains hidden or 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
| /*********************************************** | |
| * This file is desiganted for the init.groovy.d | |
| * directory to be executed at startup time of the | |
| * Jenkins instance. | |
| ***********************************************/ | |
| import javaposse.jobdsl.dsl.DslScriptLoader | |
| import javaposse.jobdsl.plugin.JenkinsJobManagement | |
| import jenkins.model.Jenkins | |
| import com.cloudbees.plugins.credentials.domains.Domain | |
| import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey | |
| import com.cloudbees.plugins.credentials.CredentialsScope | |
| // Add deploy key for the centrally shared pipeline and configuration repository | |
| def domain = Domain.global() | |
| def store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore() | |
| def keyFileContents = new File("/var/jenkins_home/.ssh/shared-libraries-deploy-key").text | |
| def privateKey = new BasicSSHUserPrivateKey( | |
| CredentialsScope.GLOBAL, | |
| "shared-libraries-deploy-key", | |
| "root", | |
| new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(keyFileContents), | |
| "", | |
| "SSH key for shared-libraries" | |
| ) | |
| store.addCredentials(domain, privateKey) | |
| // Create the configuration pipeline from a jobDSL script | |
| def jobDslScript = new File('/var/jenkins_home/init-dsl/admin.groovy') | |
| def workspace = new File('.') | |
| def jobManagement = new JenkinsJobManagement(System.out, [:], workspace) | |
| new DslScriptLoader(jobManagement).runScript(jobDslScript.text) |
This file contains hidden or 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
| #!groovy | |
| 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 hudson.plugins.sshslaves.*; | |
| // Get master credential store | |
| domain = Domain.global() | |
| store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore() | |
| // Add user/password credentials to global scope | |
| Credentials registryCreds = (Credentials) new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "my-credential-id", "My credential description", "my_user", "my_pass") | |
| store.addCredentials(domain, registryCreds) | |
| // Add private ssh keys from master fs to global scope | |
| privateRepoKeys = ["my-key-1", "my-key-2"] | |
| privateRepoKeys.each { | |
| privateKey = new BasicSSHUserPrivateKey( | |
| CredentialsScope.GLOBAL, | |
| "jenkins-${it}", | |
| "jenkins", | |
| new BasicSSHUserPrivateKey.FileOnMasterPrivateKeySource("/var/jenkins_home/.ssh/${it}"), | |
| "", | |
| "Jenkins ${it} Key" | |
| ) | |
| store.addCredentials(domain, privateKey) | |
| } |
This file contains hidden or 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
| #!groovy | |
| import jenkins.* | |
| import jenkins.model.* | |
| import hudson.* | |
| import hudson.model.* | |
| instance = Jenkins.getInstance() | |
| globalNodeProperties = instance.getGlobalNodeProperties() | |
| envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class) | |
| newEnvVarsNodeProperty = null | |
| envVars = null | |
| if (envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0) { | |
| newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty(); | |
| globalNodeProperties.add(newEnvVarsNodeProperty) | |
| envVars = newEnvVarsNodeProperty.getEnvVars() | |
| } else { | |
| envVars = envVarsNodePropertyList.get(0).getEnvVars() | |
| } | |
| envVars.put("MY_ENV_VAR", "MyEnvVar") | |
| envVars.put("MY_ENV_VAR_FROM_HOST_SYSTEM", System.getenv()['HOST_SYSTEM_ENV_VAR']) | |
| instance.save() |
This file contains hidden or 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
| #!groovy | |
| import jenkins.model.* | |
| import java.util.logging.Logger | |
| /* | |
| FIXME: This script still triggers a lot of restarts until everything is | |
| properly installed... | |
| */ | |
| def logger = Logger.getLogger("") | |
| def installed = false | |
| def initialized = false | |
| def pluginParameter="plugin-id-1 plugin-id-2 plugin-id-3" | |
| def plugins = pluginParameter.split() | |
| logger.info("" + plugins) | |
| def instance = Jenkins.getInstance() | |
| def pm = instance.getPluginManager() | |
| def uc = instance.getUpdateCenter() | |
| uc.updateAllSites() | |
| plugins.each { | |
| logger.info("Checking " + it) | |
| if (!pm.getPlugin(it)) { | |
| logger.info("Looking UpdateCenter for " + it) | |
| if (!initialized) { | |
| uc.updateAllSites() | |
| initialized = true | |
| } | |
| def plugin = uc.getPlugin(it) | |
| if (plugin) { | |
| logger.info("Installing " + it) | |
| plugin.deploy() | |
| installed = true | |
| } | |
| } | |
| } | |
| if (installed) { | |
| logger.info("Plugins installed, initializing a restart!") | |
| instance.save() | |
| instance.doSafeRestart() | |
| } |
This file contains hidden or 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
| #!groovy | |
| import jenkins.model.* | |
| import hudson.security.* | |
| def instance = Jenkins.getInstance() | |
| // Create user account | |
| def hudsonRealm = new HudsonPrivateSecurityRealm(false) | |
| hudsonRealm.createAccount("my_user", "my_pass") | |
| instance.setSecurityRealm(hudsonRealm) | |
| // Enable matrix auth strategy and set my_user as admin | |
| def strategy = new GlobalMatrixAuthorizationStrategy() | |
| strategy.add(Jenkins.ADMINISTER, "my_user") | |
| instance.setAuthorizationStrategy(strategy) | |
| instance.save() |
This file contains hidden or 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
| #!groovy | |
| import jenkins.model.* | |
| import hudson.model.* | |
| import hudson.slaves.* | |
| import hudson.plugins.sshslaves.* | |
| import java.util.ArrayList; | |
| import hudson.slaves.EnvironmentVariablesNodeProperty.Entry; | |
| // Prepare env vars for slave node | |
| List<Entry> env = new ArrayList<Entry>(); | |
| env.add(new Entry("key1","value1")) | |
| env.add(new Entry("key2","value2")) | |
| EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env); | |
| // Define slave to be bootstrapped by master | |
| Slave slave = new DumbSlave( | |
| "my-slave-id", | |
| "My slave description", | |
| "/path/to/slave/workdir", | |
| "4", // # of executors | |
| Node.Mode.EXCLUSIVE, | |
| "slave-label", | |
| new SSHLauncher(System.getenv()['MY_SLAVE_ADDRESS'], 22, SSHLauncher.lookupSystemCredentials("my-slave-ssh-key-credential-id"), "", null, null, "", "", 60, 3, 15), | |
| new RetentionStrategy.Always(), | |
| new LinkedList() | |
| ) | |
| // Add env vars to slave | |
| slave.getNodeProperties().add(envPro) | |
| // Save slave | |
| Jenkins.instance.addNode(slave) |
This file contains hidden or 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
| #!groovy | |
| import hudson.tasks.Maven.MavenInstallation; | |
| import hudson.tools.InstallSourceProperty; | |
| import hudson.tools.ToolProperty; | |
| import hudson.tools.ToolPropertyDescriptor; | |
| import hudson.util.DescribableList; | |
| // Install maven tool | |
| def mavenDesc = jenkins.model.Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0] | |
| def isp = new InstallSourceProperty() | |
| def autoInstaller = new hudson.tasks.Maven.MavenInstaller("3.3.9") | |
| isp.installers.add(autoInstaller) | |
| def proplist = new DescribableList<ToolProperty<?>, ToolPropertyDescriptor>() | |
| proplist.add(isp) | |
| def installation = new MavenInstallation("M3", "", proplist) | |
| mavenDesc.setInstallations(installation) | |
| mavenDesc.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment