Skip to content

Instantly share code, notes, and snippets.

@atomlab
Created January 31, 2020 11:44
Show Gist options
  • Save atomlab/de9a5055ccd3695a14f301c79bf92830 to your computer and use it in GitHub Desktop.
Save atomlab/de9a5055ccd3695a14f301c79bf92830 to your computer and use it in GitHub Desktop.
Jenkins job DSL scripts examples
import jenkins.model.*
import com.cloudbees.plugins.credentials.*
def credStore(credentials_id) {
def cred_list = [:]
credentials_store = jenkins.model.Jenkins.instance.getExtensionList(
'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)
credentials_store[0].credentials.each { it ->
if (it.id == credentials_id) {
cred_list['username'] = it.username
cred_list['password'] = it.password
}
}
return cred_list
}
// Jenkins Job DSL script
job_path = "${new File(__FILE__).parent}"
GroovyShell shell = new GroovyShell()
creds_nexus = shell.parse(new File (job_path + '/cred.groovy'))
releases = shell.parse(new File (job_path + '/nexus_releases.groovy'))
r = releases.get_releases(creds_nexus.credStore('nexus_user'))
println(r)
def get_releases (cred) {
// cred[username:'user', password: 'passwrod']
String userCredentials = "${cred.username}:${cred.password}"
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()))
serviceURL = 'http://hub:8081/service/rest/v1/assets?repository=release'
http_client = new URL(serviceURL).openConnection()
http_client.setRequestProperty ("Authorization", basicAuth);
http_client.setRequestMethod('GET')
if (http_client.responseCode == 200) {
response = new groovy.json.JsonSlurperClassic().parseText(http_client.inputStream.getText('UTF-8'))
} else {
println("HTTP response error")
System.exit(0)
}
nexusPkgV = []
response.items.each{ k ->
nexusPkgV.add(k.downloadUrl)
}
return nexusPkgV
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment