Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env groovy
- import java.util.logging.Logger
- import jenkins.model.Jenkins
- import hudson.model.*
- import hudson.markup.RawHtmlMarkupFormatter
- import javaposse.jobdsl.dsl.DslScriptLoader
- import javaposse.jobdsl.plugin.JenkinsJobManagement
- import com.cloudbees.plugins.credentials.*
- import com.cloudbees.plugins.credentials.common.*
- import com.cloudbees.plugins.credentials.domains.*
- import com.cloudbees.plugins.credentials.impl.*
- /**
- * Startup script
- */
- Logger.global.info("[Running] startup script")
- Logger.global.info("[Running] startup script for credentials")
- addCreds()
- configureJenkins()
- seedJenkinsJobs()
- seedJenkinsViews()
- Logger.global.info("[Done] startup script")
- /**
- * Add github authentication
- */
- private void addCreds(){
- githubKey = System.getenv("GITHUB_KEY")
- githubUser = System.getenv("GITHUB_USER")
- Logger.global.info("[Creds] Adding Creditals " + githubUser)
- store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
- println("Setting github user as " + githubUser)
- println("Setting github key as ********* ")
- Logger.global.info("[Creds] setting user to " + githubUser)
- usernameAndPassword = new UsernamePasswordCredentialsImpl(
- CredentialsScope.GLOBAL,
- "github-ci-key", "GitHub Access",
- githubUser,
- githubKey
- )
- domain = Domain.global()
- Logger.global.info("[Creds] Removing Old Creditals " + githubUser)
- allCreds = store.getCredentials(domain)
- allCreds.each{
- store.removeCredentials(domain, it)
- }
- Logger.global.info("[Creds] Adding new Creditals " + githubUser)
- store.addCredentials(domain, usernameAndPassword)
- }
- /**
- * Configure jenkins default settings
- */
- private void configureJenkins() {
- // disable jenkins security
- Jenkins.getInstance().disableSecurity()
- // disable security warning
- List<AdministrativeMonitor> p = jenkins.model.Jenkins.instance.getActiveAdministrativeMonitors()
- p.each { x ->
- if (x.getClass().name.contains("SecurityIsOffMonitor")) {
- x.disable(true)
- }
- }
- // disable remoting cli
- jenkins.model.Jenkins.instance.getDescriptor("jenkins.CLI").get().setEnabled(false)
- // set markupformatter
- Jenkins.instance.setMarkupFormatter(new RawHtmlMarkupFormatter(false))
- // set jenkins executors number
- Jenkins.instance.setNumExecutors(6)
- }
- /**
- * Creates jenkins default views
- */
- private void seedJenkinsViews() {
- File seeds = new File('/var/jenkins_home/seeds/views/')
- JenkinsJobManagement jobManagement = new JenkinsJobManagement(System.out, [:], new File('.'))
- new DslScriptLoader(jobManagement).with {
- seeds.eachFileRecurse (groovy.io.FileType.FILES) { file ->
- if (file.name.endsWith('.groovy')) {
- runScript(file.text)
- }
- }
- }
- }
- /**
- * Creates jenkins default jobs
- */
- private void seedJenkinsJobs() {
- File seeds = new File('/var/jenkins_home/seeds/jobs/')
- JenkinsJobManagement jobManagement = new JenkinsJobManagement(System.out, [:], new File('.'))
- new DslScriptLoader(jobManagement).with {
- seeds.eachFileRecurse (groovy.io.FileType.FILES) { file ->
- if (file.name.endsWith('.groovy')) {
- runScript(file.text)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment