Skip to content

Instantly share code, notes, and snippets.

@jianghaolu
Last active March 2, 2017 19:41
Show Gist options
  • Save jianghaolu/7fe18f401933848c18e51276303cd33b to your computer and use it in GitHub Desktop.
Save jianghaolu/7fe18f401933848c18e51276303cd33b to your computer and use it in GitHub Desktop.
Jenkins pipeline script to publish multiple artifacts to maven (on Windows node)
node("adxsdkbuilder") {
deleteDir()
stage('Retrive files') {
print "Retriving from ${location}..."
env.location = location
// Copy to ToSign folder
bat "copy ${env.location}\\*.pom ."
bat "if exist *.jar copy ${env.location}\\*.jar \\\\adxsdkbuilder\\ToSign\\"
def files = findFiles(glob: '*.pom')
// Put all artifact names in an environment variable, comma separated
env.proj = files[0].name.replace(".pom", "")
for (int i=1; i < files.size(); i++) {
def pom = files[i].name.replace(".pom", "")
env.proj = "${env.proj},${pom}"
}
print "Project files: ${env.proj}"
}
stage('Sign Jars') {
if (findFiles(glob: "*.jar").size() > 0) {
// Run sign-jar job
build job: 'sign-jar', parameters: [[$class: 'StringParameterValue', name: 'Description', value:
"${env.location} release"], [$class: 'StringParameterValue', name:'Keywords', value: "${env.location},Java"]]
}
// Retrieve all artifacts from comma separated environment variable
def poms = env.proj.split(",")
for (int i=0; i < poms.size(); i++) {
def pom = poms[i]
bat "if exist \\\\adxsdkbuilder\\Signed\\${pom}*.jar (move \\\\adxsdkbuilder\\Signed\\${pom}*.jar .)"
}
}
stage('GPG Sign') {
// Run the maven build
bat '''forfiles /s /m *.pom /c "cmd /c gpg --batch --passphrase AzureRocks!12 -ab @path"
if exist *.jar forfiles /s /m *.jar /c "cmd /c gpg --batch --passphrase AzureRocks!12 -ab @path"'''
}
stage('Calculate checksums') {
bat '''
forfiles /s /m *.* /c "cmd /c fciv -md5 @file > @file.md5"
forfiles /s /m *.* /c "cmd /c fciv -sha1 @file > @file.sha1"
del *.md5.sha1
'''
def md5s = findFiles(glob: '*.md5')
for (int i=0; i < md5s.size(); i++) {
def md5 = md5s[i]
def content = readFile(md5.name).trim()
writeFile file: md5.name, text: extractMd5(content)
}
def sha1s = findFiles(glob: '*.sha1')
for (int i=0; i < sha1s.size(); i++) {
def sha1 = sha1s[i]
def content = readFile(sha1.name).trim()
writeFile file: sha1.name, text: extractSha1(content)
}
}
stage('Create staging repo') {
withCredentials([usernamePassword(credentialsId: '60c55b3e-37d0-4e55-a96d-f3de3a9ed6f1', passwordVariable: 'ossrhpass', usernameVariable: 'ossrhuser')]) {
bat '''
curl -X POST -d "<promoteRequest><data><description>%proj%</description></data></promoteRequest>" -H "Content-Type:application/xml" -u %ossrhuser%:%ossrhpass% -k https://oss.sonatype.org/service/local/staging/profiles/534d15ee3800f4/start > create_repo.log
'''
def output=readFile('create_repo.log').trim()
def group = (output =~ /<stagedRepositoryId>([a-zA-Z0-9-]+)<\/stagedRepositoryId>/)
print group
print group[0]
env.repository = group[0][1]
print "Staging repo created: ${env.repository}"
group = null
}
}
stage('Upload') {
withCredentials([usernamePassword(credentialsId: '60c55b3e-37d0-4e55-a96d-f3de3a9ed6f1', passwordVariable: 'ossrhpass', usernameVariable: 'ossrhuser')]) {
// Retrieve all artifacts from comma separated environment variable
def poms = env.proj.split(",")
for (int i=0; i < poms.size(); i++) {
env.pom = poms[i]
env.artifact = extractArtifact(env.pom)
env.version = extractVersion(env.pom)
bat '''
forfiles /s /m %pom%*.* /c "cmd /c curl --upload-file @relpath -u %ossrhuser%:%ossrhpass% -k https://oss.sonatype.org/service/local/staging/deployByRepositoryId/%repository%/com/microsoft/azure/%artifact%/%version%/@file"
'''
}
}
}
stage('Close staging repo') {
withCredentials([usernamePassword(credentialsId: '60c55b3e-37d0-4e55-a96d-f3de3a9ed6f1', passwordVariable: 'ossrhpass', usernameVariable: 'ossrhuser')]) {
bat '''
curl -X POST -d "<promoteRequest><data><stagedRepositoryId>%repository%</stagedRepositoryId></data></promoteRequest>" -H "Content-Type:application/xml" -u %ossrhuser%:%ossrhpass% -k https://oss.sonatype.org/service/local/staging/profiles/534d15ee3800f4/finish
'''
}
print "\n\nYour job is completed. The staging repository id is ${env.repository}."
print "You can browse it at https://oss.sonatype.org/content/repositories/${env.repository}"
}
}
@NonCPS
def extractMd5(content) {
def hash = (content =~ /([a-z0-9]{32})/)
return hash[0][0]
}
@NonCPS
def extractSha1(content) {
def hash = (content =~ /([a-z0-9]{40})/)
return hash[0][0]
}
@NonCPS
def extractArtifact(identifier) {
def group = (identifier =~ /([a-zA-Z0-9-]+)-([0-9][0-9a-zA-Z\.-]+)/)
return group[0][1]
}
@NonCPS
def extractVersion(identifier) {
def group = (identifier =~ /([a-zA-Z0-9-]+)-([0-9][0-9a-zA-Z\.-]+)/)
return group[0][2]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment