Skip to content

Commit

Permalink
Added enhancement to slack publish with build failure cause (opensear…
Browse files Browse the repository at this point in the history
…ch-project#1819)

* Added enhancement to slack publish with build failure cause

Signed-off-by: pgodithi <[email protected]>

* Added Unit tests

Signed-off-by: pgodithi <[email protected]>

* Added Unit tests

Signed-off-by: pgodithi <[email protected]>

* Added Unit tests

Signed-off-by: pgodithi <[email protected]>

* Added Unit tests

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Added data types

Signed-off-by: pgodithi <[email protected]>

* Buffer Reader to store the jenkins log and read from this stream

Signed-off-by: pgodithi <[email protected]>

* Removed delimiter

Signed-off-by: pgodithi <[email protected]>

* Removed delimiter

Signed-off-by: pgodithi <[email protected]>

* Added test files

Signed-off-by: pgodithi <[email protected]>

* Added jenkins BuildFailureMessage shared lib test cases

Signed-off-by: pgodithi <[email protected]>

* Fixing all failed test cases

Signed-off-by: pgodithi <[email protected]>

* Fixing all failed test cases: testing

Signed-off-by: pgodithi <[email protected]>

* Fixing all failed test cases: testing

Signed-off-by: pgodithi <[email protected]>

* Fixing double-quotes bug in buildFailureMessage.groovy file

Signed-off-by: pgodithi <[email protected]>

* Fixing double-quotes bug in buildFailureMessage.groovy file

Signed-off-by: pgodithi <[email protected]>

* Added buildFailureMessage() pipelines

Signed-off-by: pgodithi <[email protected]>

* Added buildFailureMessage() pipelines

Signed-off-by: pgodithi <[email protected]>

* removing buildFailureMessage() for dashboard jenkinsfile

Signed-off-by: pgodithi <[email protected]>
  • Loading branch information
prudhvigodithi authored Mar 29, 2022
1 parent 7f58698 commit aa6b829
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 27 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ sharedLibrary {
}

test {
testLogging {
events "failed"
exceptionFormat "full"
}
if (project.hasProperty('pipeline.stack.write')) {
systemProperty 'pipeline.stack.write', project.getProperty('pipeline.stack.write')
}
Expand Down
2 changes: 1 addition & 1 deletion jenkins/opensearch/distribution-build.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pipeline {
if (params.PUBLISH_NOTIFICATION) {
publishNotification(
icon: ':warning:',
message: 'Failed Build',
message: buildFailureMessage(),
credentialsId: 'BUILD_NOTICE_WEBHOOK',
manifest: "${INPUT_MANIFEST}"
)
Expand Down
28 changes: 28 additions & 0 deletions tests/jenkins/TestBuildFailureMessage.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package jenkins.tests
import jenkins.tests.BuildPipelineTest
import org.junit.*

class TestBuildFailureMessage extends BuildPipelineTest {


@Before
void setUp() {
this.registerLibTester(new BuildFailureMessageLibTester())
super.setUp()
def currentBuild = binding.getVariable('currentBuild')
binding.setVariable("currentBuild", currentBuild)
}

@Test
void testBuildFailureMsg() {
super.testPipeline("tests/jenkins/jobs/BuildFailureMessage_Jenkinsfile")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@
bwc-test.publishNotification({icon=:white_check_mark:, message=BWC Tests Successful, extra=, credentialsId=INTEG_TEST_WEBHOOK, manifest=tests/jenkins/data/opensearch-dashboards-1.2.0-test.yml})
publishNotification.string({credentialsId=INTEG_TEST_WEBHOOK, variable=WEBHOOK_URL})
publishNotification.withCredentials([WEBHOOK_URL], groovy.lang.Closure)
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark: dummy_job [123] BWC Tests Successful
Build: htth://BUILD_URL_dummy.com
tests/jenkins/data/opensearch-dashboards-1.2.0-test.yml
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark:
JOB_NAME=dummy_job
BUILD_NUMBER=[123]
MESSAGE=BWC Tests Successful
BUILD_URL: htth://BUILD_URL_dummy.com
MANIFEST: tests/jenkins/data/opensearch-dashboards-1.2.0-test.yml
"}' "WEBHOOK_URL")
bwc-test.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@
integ-test.publishNotification({icon=:white_check_mark:, message=Integration Tests Successful, extra=, credentialsId=INTEG_TEST_WEBHOOK, manifest=tests/jenkins/data/opensearch-dashboards-1.2.0-test.yml})
publishNotification.string({credentialsId=INTEG_TEST_WEBHOOK, variable=WEBHOOK_URL})
publishNotification.withCredentials([WEBHOOK_URL], groovy.lang.Closure)
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark: dummy_job [123] Integration Tests Successful
Build: htth://BUILD_URL_dummy.com
tests/jenkins/data/opensearch-dashboards-1.2.0-test.yml
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark:
JOB_NAME=dummy_job
BUILD_NUMBER=[123]
MESSAGE=Integration Tests Successful
BUILD_URL: htth://BUILD_URL_dummy.com
MANIFEST: tests/jenkins/data/opensearch-dashboards-1.2.0-test.yml
"}' "WEBHOOK_URL")
integ-test.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@
bwc-test.publishNotification({icon=:white_check_mark:, message=BWC Tests Successful, extra=, credentialsId=INTEG_TEST_WEBHOOK, manifest=tests/jenkins/data/opensearch-1.3.0-test.yml})
publishNotification.string({credentialsId=INTEG_TEST_WEBHOOK, variable=WEBHOOK_URL})
publishNotification.withCredentials([WEBHOOK_URL], groovy.lang.Closure)
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark: dummy_job [123] BWC Tests Successful
Build: htth://BUILD_URL_dummy.com
tests/jenkins/data/opensearch-1.3.0-test.yml
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark:
JOB_NAME=dummy_job
BUILD_NUMBER=[123]
MESSAGE=BWC Tests Successful
BUILD_URL: htth://BUILD_URL_dummy.com
MANIFEST: tests/jenkins/data/opensearch-1.3.0-test.yml
"}' "WEBHOOK_URL")
bwc-test.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@
integ-test.publishNotification({icon=:white_check_mark:, message=Integration Tests Successful, extra=, credentialsId=INTEG_TEST_WEBHOOK, manifest=tests/jenkins/data/opensearch-1.3.0-test.yml})
publishNotification.string({credentialsId=INTEG_TEST_WEBHOOK, variable=WEBHOOK_URL})
publishNotification.withCredentials([WEBHOOK_URL], groovy.lang.Closure)
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark: dummy_job [123] Integration Tests Successful
Build: htth://BUILD_URL_dummy.com
tests/jenkins/data/opensearch-1.3.0-test.yml
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark:
JOB_NAME=dummy_job
BUILD_NUMBER=[123]
MESSAGE=Integration Tests Successful
BUILD_URL: htth://BUILD_URL_dummy.com
MANIFEST: tests/jenkins/data/opensearch-1.3.0-test.yml
"}' "WEBHOOK_URL")
integ-test.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
19 changes: 19 additions & 0 deletions tests/jenkins/jobs/BuildFailureMessage_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pipeline {
agent none
stages {
stage('notify') {
steps {
script {
try {
buildFailureMessage()
currentBuild.result = 'SUCCESS'
} catch (Exception err) {
//https://github.com/jenkinsci/JenkinsPipelineUnit/issues/509
currentBuild.result = 'SUCCESS'
}
}
}
}
}
}

6 changes: 6 additions & 0 deletions tests/jenkins/jobs/BuildFailureMessage_Jenkinsfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BuildFailureMessage_Jenkinsfile.run()
BuildFailureMessage_Jenkinsfile.pipeline(groovy.lang.Closure)
BuildFailureMessage_Jenkinsfile.echo(Executing on agent [label:none])
BuildFailureMessage_Jenkinsfile.stage(notify, groovy.lang.Closure)
BuildFailureMessage_Jenkinsfile.script(groovy.lang.Closure)
BuildFailureMessage_Jenkinsfile.buildFailureMessage()
18 changes: 11 additions & 7 deletions tests/jenkins/jobs/PublishNotification_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ pipeline {
stage('notify') {
steps {
script {
publishNotification(
icon: ':white_check_mark:',
message: 'Successful Build',
extra: 'extra',
manifest: '1.2.0/opensearch-1.2.0.yml',
credentialsId: 'BUILD_NOTICE_WEBHOOK'
)
try {
publishNotification(
icon: ':white_check_mark:',
message: 'Successful Build',
extra: 'extra',
manifest: '1.2.0/opensearch-1.2.0.yml',
credentialsId: 'BUILD_NOTICE_WEBHOOK'
)
}catch (Exception e) {
echo 'Exception occurred: ' + e.toString()
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions tests/jenkins/jobs/PublishNotification_Jenkinsfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
PublishNotification_Jenkinsfile.publishNotification({icon=:white_check_mark:, message=Successful Build, extra=extra, manifest=1.2.0/opensearch-1.2.0.yml, credentialsId=BUILD_NOTICE_WEBHOOK})
publishNotification.string({credentialsId=BUILD_NOTICE_WEBHOOK, variable=WEBHOOK_URL})
publishNotification.withCredentials([WEBHOOK_URL], groovy.lang.Closure)
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark: dummy_job [123] Successful Build
Build: htth://BUILD_URL_dummy.com
1.2.0/opensearch-1.2.0.yml
publishNotification.sh(curl -XPOST --header "Content-Type: application/json" --data '{"result_text":":white_check_mark:
JOB_NAME=dummy_job
BUILD_NUMBER=[123]
MESSAGE=Successful Build
BUILD_URL: htth://BUILD_URL_dummy.com
MANIFEST: 1.2.0/opensearch-1.2.0.yml
extra"}' "WEBHOOK_URL")
25 changes: 25 additions & 0 deletions tests/jenkins/lib-testers/BuildFailureMessageLibTester.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import static org.hamcrest.CoreMatchers.notNullValue
import static org.hamcrest.MatcherAssert.assertThat

class BuildFailureMessageLibTester extends LibFunctionTester {


public BuildFailureMessageLibTester() {}


void parameterInvariantsAssertions(call) {
//N/A
}

boolean expectedParametersMatcher(call) {
return true
}


String libFunctionName() {
return 'buildFailureMessage'
}

void configure(helper, binding){
}
}
21 changes: 21 additions & 0 deletions vars/buildFailureMessage.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import com.cloudbees.groovy.cps.NonCPS
import org.apache.commons.io.IOUtils
@NonCPS
def call(){
String ERROR_STRING = "ERROR"
List<String> message = []
Reader performance_log = currentBuild.getRawBuild().getLogReader()
String logContent = IOUtils.toString(performance_log)
performance_log.close()
performance_log = null
logContent.eachLine() { line ->
if (line.matches(".*?$ERROR_STRING(.*?)")) {
line=line.replace("\"", "")
message.add(line)
}
}
if(message.isEmpty()){
message=["Build failed"]
}
return message
}
10 changes: 6 additions & 4 deletions vars/publishNotification.groovy
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
void call(Map args = [:]) {
text = ([
"${args.icon} ${JOB_NAME} [${BUILD_NUMBER}] ${args.message}",
"Build: ${BUILD_URL}",
args.manifest,
"${args.icon}",
"JOB_NAME=${JOB_NAME}",
"BUILD_NUMBER=[${BUILD_NUMBER}]",
"MESSAGE=${args.message}",
"BUILD_URL: ${BUILD_URL}",
"MANIFEST: ${args.manifest}",
args.extra
] - null).join("\n")

withCredentials([string(credentialsId: args.credentialsId, variable: 'WEBHOOK_URL')]) {
sh ([
'curl',
Expand Down

0 comments on commit aa6b829

Please sign in to comment.