Jenkins Groovy Hook

자동 admin 생성

import jenkins.model.*
import hudson.security.*
 
def instance = Jenkins.getInstance()
// System.getenv("JENKINS_HOME") - 존재안함
File adminCreatedMarker = new File("/var/lib/jenkins", ".adminCreatedmark")
println "mark file path : $adminCreatedMarker"
 
if (adminCreatedMarker.exists()) {
    println "admin user already exists. skip creating user."
} else {
    def hudsonRealm = new HudsonPrivateSecurityRealm(false)
    println "admin does not exists. creating admin...."
    hudsonRealm.createAccount("admin","admin") // username, password
    instance.setSecurityRealm(hudsonRealm)
 
    def strategy = new hudson.security.FullControlOnceLoggedInAuthorizationStrategy()
    strategy.setAllowAnonymousRead(false)
    instance.setAuthorizationStrategy(strategy)
 
    instance.save()
    adminCreatedMarker.createNewFile()
    println "$adminCreatedMarker file created."
}