Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jenkins: Add Slack notifications for job status changes #306

Merged
merged 1 commit into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Jenkinsfile.aws-test
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ node(NODE) {
return
}

try {
utils.inside_assembler_container("-v /srv:/srv") {
stage("Sync In") {
withCredentials([
Expand Down Expand Up @@ -79,4 +80,10 @@ node(NODE) {
}
}
}
} catch (Throwable e) {
currentBuild.result = 'FAILURE'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just do try-finally and move this statement as part of the finally block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you differentiate between success and failure then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what I was thinking when I made that suggestion. :)

throw e
} finally {
utils.notify_status_change currentBuild
}
}
7 changes: 7 additions & 0 deletions Jenkinsfile.cloud
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ node(NODE) {
utils = load("pipeline-utils.groovy")
utils.define_properties(TIMER)

try {
def manifest = "host-maipo.yaml"
def manifest_data = readYaml file: "${manifest}";
def ref = manifest_data.ref;
Expand Down Expand Up @@ -257,4 +258,10 @@ node(NODE) {
string(name: 'aws_type', value: "t2.small")
]
}
} catch (Throwable e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
utils.notify_status_change currentBuild
}
}
5 changes: 4 additions & 1 deletion Jenkinsfile.rdgo
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ node(NODE) {
build job: 'coreos-rhcos-treecompose', wait: false
}

} catch (Throwable e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
archiveArtifacts artifacts: "log/**", allowEmptyArchive: true
utils.notify_status_change currentBuild
}
}

9 changes: 8 additions & 1 deletion Jenkinsfile.treecompose
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ node(NODE) {
utils = load("pipeline-utils.groovy")
utils.define_properties(TIMER)

try {
def manifest = "host-${OS_NAME}.yaml"
def manifest_data = readYaml file: "${manifest}";
// TODO - stop using the ref in favor of oscontainer:// pivot
Expand Down Expand Up @@ -127,5 +128,11 @@ node(NODE) {

// Trigger downstream jobs
build job: 'coreos-rhcos-cloud', wait: false
}
}
} catch (Throwable e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
utils.notify_status_change currentBuild
}
}
9 changes: 8 additions & 1 deletion Jenkinsfile.treecompose-ootpa
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ node(NODE) {
utils = load("pipeline-utils.groovy")
utils.define_properties(TIMER)

try {
def manifest = "host-${OS_NAME}.yaml"
def manifest_data = readYaml file: "${manifest}";
// TODO - stop using the ref in favor of oscontainer:// pivot
Expand Down Expand Up @@ -105,5 +106,11 @@ node(NODE) {
stage("Cleanup") { sh """
rm ${treecompose_workdir} -rf
""" }
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an extra } to me, but I'm not 100% sure. The indentation of groovy throws me off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put the new try blocks at the same indentation level as the rest of the node closure to be consistent with the ones that already existed (which I assume did that to not indent everything and have a huge diff). I indented that one } to four spaces because nothing else is indented to two spaces in any of the files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thanks!

} catch (Throwable e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
utils.notify_status_change currentBuild
}
}
32 changes: 32 additions & 0 deletions pipeline-utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,36 @@ def inside_assembler_container(args, fn) {
}
}

// Send a notification when a job's status changes.
// Treat non-SUCCESS states the same, so it does not send notifications when
// changes go between UNSTABLE and FAILURE.
def notify_status_change(build) {
def color = ''
def message = "<${env.BUILD_URL}|Build ${env.BUILD_NUMBER} of ${env.JOB_NAME}>"

if (build.currentResult == build.previousBuild?.result)
return

if (build.previousBuild?.result == null) {
echo 'The previous build is still running; ignoring its build state.'
return
} else if (build.currentResult == 'SUCCESS') {
message = ":partyparrot: ${message} is working again."
color = 'good'
} else if (build.previousBuild.result == 'SUCCESS') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth filtering out ABORT cases here where someone manually aborts a job.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could filter previously aborted job states, but I'm not sure what to do about when the current job is aborted. You apparently can't set the state to ABORTED yourself (JENKINS-43339), so it could only be treated as either success or failure. The sandbox gets angry about adding new fields to currentBuild, but maybe it could pass the ABORTED state through a different variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's unfortunate. That's fine then, we can live with it.

message = ":trashfire: ${message} has started failing."
color = 'danger'
} else {
echo 'This and the previous build have different non-success states.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, let's just notify in that case, too. Maybe something like message = ":boom: ${message} is failing, but in a different way." ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still want this additional notification while ABORTED states are considered a failure? I believe the only possible non-success states are FAILURE, ABORTED, and UNSTABLE.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's fine. Thinking more on this, if this case happens, it means that the previous build was not successful, and we already notified about the SUCCESS --> not-success transition in the past.

return
}

try {
slackSend channel: '#jenkins-coreos', color: color, message: message
} catch (NoSuchMethodError err) {
// Log the message in the console if the Slack plugin is not installed.
echo message
}
}

return this