Skip to content

Instantly share code, notes, and snippets.

@vadikgo
Created November 12, 2018 10:48
Show Gist options
  • Save vadikgo/128728c227356ae977d298dadd298e90 to your computer and use it in GitHub Desktop.
Save vadikgo/128728c227356ae977d298dadd298e90 to your computer and use it in GitHub Desktop.
Nexus artifacts Jenkins dropdown demo
node('linux') {
def ver
stage ('Stage 1') {
withCredentials([usernamePassword(credentialsId: 'nexus_user_cred', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
def versions = getVersionId('http://nexus.example.com:8081/nexus/service/local', "${USERNAME}", "${PASSWORD}",'distrib', 'test1')
ver = input message: 'Get version',
parameters: [choice(choices: versions, description: '', name: 'versions')]
file_name = ver + '.zip'
getNexusFile(file_name, "${USERNAME}", "${PASSWORD}", 'http://nexus.example.com:8081/nexus/service/local', 'loans.car', ver, 'zip', 'test_elease', 'distrib', '')
}
}
stage ('send mail') {
echo "pass"
}
}
def getNexusFile(fileName, username, password, nexusRestApiUrl, nexusArtifactId, nexusVersionId, nexusExtensionId, nexusRepositoryId, nexusGroupId, classifier) {
def nexus_url = "${nexusRestApiUrl}/artifact/maven/redirect?g=${nexusGroupId}&a=${nexusArtifactId}&v=${nexusVersionId}&e=${nexusExtensionId}&r=${nexusRepositoryId}&c=${classifier}"
def authString = "${username}:${password}".getBytes().encodeBase64().toString()
httpRequest customHeaders: [[maskValue: true, name: "Authorization", value: "Basic ${authString}"]], outputFile: fileName, ignoreSslErrors: true, responseHandle: 'NONE', url: nexus_url
wk = pwd()
if (nexusExtensionId == 'rar') {
sh "unrar x -y -idp -ai -u ${fileName} ${wk}/"
}
if (nexusExtensionId == 'zip') {
sh "unzip -o -d ${wk} ${fileName}"
}
}
def getVersionId(nexusRestApiUrl, username, password, nexusGroupId, nexusArtifactId) {
def addr = "${nexusRestApiUrl}/lucene/search?g=${nexusGroupId}&a=${nexusArtifactId}"
def authString = "${username}:${password}".getBytes().encodeBase64().toString()
def response = httpRequest customHeaders: [[maskValue: true, name: "Authorization", value: "Basic ${authString}"]], ignoreSslErrors: true, url: addr, validResponseCodes: '200'
def xml = new XmlParser().parseText(response.content);
xml['data']['artifact']['version'].collect { it ->
it.text()
}.unique().join("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment