简体   繁体   中英

Jenkinsfile, return specific line from console output

I have a multi-branch pipeline in Jenkins, using a Jenkinsfile. I have it running a Maven job which then sends the artifacts to S3. I would like to send an email notification after a successful build with a link that dynamically points to the artifacts in S3. I would rather point to the artifacts via S3 rather than Jenkins because 1) this ensures that the artifacts were properly stored in S3 and 2) S3 has longer storage. Is there an easy way to do that? I was thinking maybe scanning the build's console output for the line where the artifact is being uploaded to S3 (see non-working code below, I get "Error when executing success post condition" and don't receive an email), but I'm new to coding/Jenkins and don't know how to filter through the console output for the artifacts. Am I overcomplicating this, is there is an easier way to do this? And/or can you help me fix this code snippet to get this to work? Thank you!

post {
    success {
       script {
          def file = currentBuild.rawBuild.log
          def data = file.filterLine { line ->
             line.contains('Uploaded to repo.XXX: s3://XXX-artifacts')
          }
          emailext (
             subject: '$JOB_NAME build',
             body: "$JOB_NAME, job $BUILD_ID.\n$BUILD_URL\nhttps://XXX-artifacts.s3.us-west-2.amazonaws.com" + data,
             to: env.EmailList)
    }
  }
}

I was able to figure this out using manager.getLogMatcher

post {
    success {
        script {
            def S3_url = manager.getLogMatcher ("(.*)Uploaded(.*)+s3(.*)+zip(.*)")
            if (S3_url.matches()) {
              S3_url = S3_url.group(0)
            ....
            }
        }
    }
}

The manager.getLogMatcher searches the console output, in this case it's looking for a particular line that contains "Uploaded," "s3" and ".zip". I am then able to manipulate that result (not all of it is shown here) to create the URL as the variable S3_url, which I then just include in the emailext in place of the ""https....com" + data" in the original post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM