Skip to content

Instantly share code, notes, and snippets.

@johnbuhay
Last active September 27, 2021 10:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save johnbuhay/3e3c10b3f20cea7ead2b321ac8792463 to your computer and use it in GitHub Desktop.
Save johnbuhay/3e3c10b3f20cea7ead2b321ac8792463 to your computer and use it in GitHub Desktop.
Jenkins | Configurations done via init.groovy.d

Jenkins

Configurations done via init.groovy.d

setup.yml

Configurations defined by YAML

seed.groovy

Create ci/master job

This file used to name this gist.
#!/usr/bin/env groovy
// Sample jenkins build log
//Added items:
// GeneratedJob{name='master-build'}
//Existing items:
// GeneratedJob{name='develop-build'}
//Unreferenced items:
// GeneratedJob{name='feature-zo1MjozMy-build'}
// GeneratedJob{name='feature-zoyNToxMy-build'}
//Removed items:
// GeneratedJob{name='feature-zoyNToxMy-build'}
// GeneratedJob{name='feature-zo1MjozMy-build'}
//
import java.util.regex.Pattern
import java.util.regex.Matcher
String text = ''
manager.build.logFile.eachLine { line -> text += line }
String patternString = "Added items:( +GeneratedJob\\{name='((\\w*-)+\\w*)'\\})+"
String patternString2 = "(name='((\\w*-)+\\w*)')+"
Pattern pattern = Pattern.compile(patternString)
Pattern pattern2 = Pattern.compile(patternString2)
Matcher matcher = pattern.matcher(text)
def added_jobs = [] as LinkedList
while(matcher.find()) {
Matcher matcher2 = pattern2.matcher(matcher.group(0))
while(matcher2.find()) { added_jobs << matcher2.group(2) }
}
def JENKINS_URL = manager.getEnvVariable('JENKINS_URL')
def JOB_URL = manager.getEnvVariable('JOB_URL')
// this is a base64 encoded username:apikey string
// ensure the user needs access to 'read' and 'build' jobs
def JENKINS_AUTH = manager.getEnvVariable('JENKINS_AUTH') ?: null
// trigger newly created jobs
added_jobs.each {
_temp = JOB_URL.split('/job')
_temp[-1] = "/${it}/".toString() // not GString from interpolation
_branchJobUrl = _temp.join('/job')
_triggerUrl = "${_branchJobUrl}/build?delay=0sec"
manager.listener.logger.println(_triggerUrl)
url = new URL(_triggerUrl)
def connection = url.openConnection()
if(JENKINS_AUTH) { connection.setRequestProperty('Authorization', "Basic ${JENKINS_AUTH}") }
connection.setRequestMethod("POST")
connection.doOutput = true
connection.connect()
manager.listener.logger.println( connection.inputStream.text )
manager.listener.logger.println( connection.getResponseCode() )
connection.disconnect()
}
//class.methods.each { manager.listener.logger.println(it) }
@Grapes([
@Grab(group='org.yaml', module='snakeyaml', version='1.17')
])
import org.yaml.snakeyaml.Yaml
import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.plugin.JenkinsJobManagement
import java.util.logging.Logger
env = System.getenv()
JENKINS_SETUP_YAML = env['JENKINS_SETUP_YAML'] ?: "${env['JENKINS_CONFIG_HOME']}/setup.yml"
config = new Yaml().load(new File(JENKINS_SETUP_YAML).text)
Logger logger = Logger.getLogger('seed.groovy')
Thread.start {
WORKSPACE_BASE = "${env['JENKINS_HOME']}/workspace"
def workspace = new File("${WORKSPACE_BASE}")
// workspace.mkdirs()
// def seedJobDsl = new File("${WORKSPACE_BASE}/seed.groovy")
def seedJobDsl = config.seed_jobdsl
logger.info(initJobDsl)
def jobManagement = new JenkinsJobManagement(System.out, [:], workspace)
new DslScriptLoader(jobManagement).runScript(seedJobDsl)
logger.info('Created first job')
}
---
admin:
username: sirjenkins
password: password
email: &admin_email admin@sirjenkins.io
credentials:
- global:
-
username: <username>
password: <password>
id: <credentials-id>
description:
git:
config:
name: sirjenkins
email: *admin_email
scopes: read:org,user:email
github:
oauth:
client_id:
client_secret:
mailer:
smtp_email: *admin_email
smtp_password: password
smtp_host:
seed_jobdsl: |
def creds = <credentials-id>
def full_name = 'jnbnyc/ci'
def branch_name = 'master'
def folder_name = 'ci'
def job_name = 'master'
folder(folder_name)
job("${folder_name}/${job_name}") {
description('This job is automagically generated.')
disabled(false)
blockOnUpstreamProjects()
logRotator(daysToKeep = 14, numToKeep = 9)
steps {
scm {
git {
remote {
url("https://github.com/${full_name}")
credentials(creds)
}
branch(branch_name)
}
}
dsl {
external('master.groovy')
ignoreExisting(false)
removeAction('DELETE')
removeViewAction('DELETE')
}
}
}
set_master_kill_switch: true
web_port: 8080
web_proto: http
// setup Time Zone
Thread.start {
TZ = env['JENKINS_TZ'] ?: config.time_zone ?: 'America/New_York'
System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', TZ)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment