Skip to content

Instantly share code, notes, and snippets.

@ansig
Created December 22, 2018 12:18
Show Gist options
  • Save ansig/d7edafe38fbfc13c5b3cd15351849804 to your computer and use it in GitHub Desktop.
Save ansig/d7edafe38fbfc13c5b3cd15351849804 to your computer and use it in GitHub Desktop.
Jenkins init script to add LockableResources specified in a config file (specified in INIT_JENKINS_CONFIG var)
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
def lockableResourcesConfigFile = new File("${System.getenv('INIT_JENKINS_CONFIG')}/lockableResources.yml")
if (lockableResourcesConfigFile.exists()) {
println "INFO: Adding LockableResources from configuration: ${lockableResourcesConfigFile.absolutePath}"
Yaml parser = new Yaml()
def lockableResourcesConfig = parser.load(lockableResourcesConfigFile.text)
def manager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()
lockableResourcesConfig.resources.each {
println "INFO: Adding: ${it}"
manager.createResource(it.name)
def resource = manager.fromName(it.name)
if (it.description) {
resource.setDescription("${it.description}")
}
if (it.labels) {
resource.setLabels("${it.labels}")
}
if (it.reservedBy) {
resource.setReservedBy("${it.reservedBy}")
}
}
manager.save()
println "INFO: Done configuring LockableResources"
} else {
println "INFO: Did not find ${lockableResourcesConfigFile.absolutePath}, ignoring LockableResources init script"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment