Skip to content

Commit

Permalink
Reorganise jenkinsfile twig templates and extract slack notification …
Browse files Browse the repository at this point in the history
…script
  • Loading branch information
andytson-inviqa committed Nov 2, 2023
1 parent 52fa261 commit 76768df
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 51 deletions.
44 changes: 1 addition & 43 deletions application/overlay/Jenkinsfile.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,7 @@ pipeline {
post {
{% if @('jenkins.notifications.slack.channel') and @('jenkins.notifications.slack.token_credential_id') %}
failure {
script {
def message = "{% if @('jenkins.notifications.global.full_build_name') %}${env.JOB_NAME}{% else %}${env.JOB_BASE_NAME}{% endif %} #${env.BUILD_NUMBER} - Failure after ${currentBuild.durationString.minus(' and counting')} (<${env.RUN_DISPLAY_URL}|View Build>)"
def fallbackMessages = [ message ]
def fields = []

{% if @('jenkins.notifications.global.branch_link_template') and @('jenkins.notifications.global.commit_link_template') %}
def shortCommitHash = "${GIT_COMMIT}".substring(0, 7)
def commitLink = "commit <{{ @('jenkins.notifications.global.commit_link_template') }}".replace('GIT_COMMIT', GIT_COMMIT) + "|${shortCommitHash}>"
def gitMessage = "Branch <{{ @('jenkins.notifications.global.branch_link_template') }}".replace('GIT_BRANCH', GIT_BRANCH) + "|${GIT_BRANCH}> at ${commitLink}"

if (env.CHANGE_URL) {
// Jenkins builds pull requests by merging the pull request branch into the pull request's target branch,
// so we build on commits that do not technically exist and can't link to them.
gitMessage = "<${env.CHANGE_URL}|{{ @('jenkins.notifications.global.change_request_name') }} #${env.CHANGE_ID}> ${env.CHANGE_TITLE}{% if @('jenkins.notifications.global.change_request_build_on_merge') %} - merged into target branch " + "<{{ @('jenkins.notifications.global.branch_link_template') }}".replace('GIT_BRANCH', CHANGE_TARGET) + "|${CHANGE_TARGET}>{% endif %}"
}
fields << [
title: 'Source',
value: gitMessage,
short: false
]
fallbackMessages << gitMessage
{% endif %}

def failureMessage = failureMessages.join("\n")
if (failureMessage) {
fields << [
title: 'Reason(s)',
value: failureMessage,
short: false
]
fallbackMessages << failureMessage
}
def attachments = [
[
text: message,
fallback: fallbackMessages.join("\n"),
color: 'danger',
fields: fields
]
]

slackSend (channel: '{{ @('jenkins.notifications.slack.channel') }}', color: 'danger', attachments: attachments, tokenCredentialId: '{{ @('jenkins.notifications.slack.token_credential_id') }}')
}
{% include blocks ~ 'slack-notification.twig' %}
}
{% endif %}
always {
Expand Down
43 changes: 43 additions & 0 deletions application/overlay/_twig/Jenkinsfile/slack-notification.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
script {
def message = "{% if @('jenkins.notifications.global.full_build_name') %}${env.JOB_NAME}{% else %}${env.JOB_BASE_NAME}{% endif %} #${env.BUILD_NUMBER} - Failure after ${currentBuild.durationString.minus(' and counting')} (<${env.RUN_DISPLAY_URL}|View Build>)"
def fallbackMessages = [ message ]
def fields = []

{% if @('jenkins.notifications.global.branch_link_template') and @('jenkins.notifications.global.commit_link_template') %}
def shortCommitHash = "${GIT_COMMIT}".substring(0, 7)
def commitLink = "commit <{{ @('jenkins.notifications.global.commit_link_template') }}".replace('GIT_COMMIT', GIT_COMMIT) + "|${shortCommitHash}>"
def gitMessage = "Branch <{{ @('jenkins.notifications.global.branch_link_template') }}".replace('GIT_BRANCH', GIT_BRANCH) + "|${GIT_BRANCH}> at ${commitLink}"

if (env.CHANGE_URL) {
// Jenkins builds pull requests by merging the pull request branch into the pull request's target branch,
// so we build on commits that do not technically exist and can't link to them.
gitMessage = "<${env.CHANGE_URL}|{{ @('jenkins.notifications.global.change_request_name') }} #${env.CHANGE_ID}> ${env.CHANGE_TITLE}{% if @('jenkins.notifications.global.change_request_build_on_merge') %} - merged into target branch " + "<{{ @('jenkins.notifications.global.branch_link_template') }}".replace('GIT_BRANCH', CHANGE_TARGET) + "|${CHANGE_TARGET}>{% endif %}"
}
fields << [
title: 'Source',
value: gitMessage,
short: false
]
fallbackMessages << gitMessage
{% endif %}

def failureMessage = failureMessages.join("\n")
if (failureMessage) {
fields << [
title: 'Reason(s)',
value: failureMessage,
short: false
]
fallbackMessages << failureMessage
}
def attachments = [
[
text: message,
fallback: fallbackMessages.join("\n"),
color: 'danger',
fields: fields
]
]

slackSend (channel: '{{ @('jenkins.notifications.slack.channel') }}', color: 'danger', attachments: attachments, tokenCredentialId: '{{ @('jenkins.notifications.slack.token_credential_id') }}')
}
16 changes: 8 additions & 8 deletions application/overlay/_twig/Jenkinsfile/stages.twig
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{% set blocks = 'application/overlay/_twig/Jenkinsfile/' %}

{% if @('jenkins.tests.isolated') %}
{% include blocks ~ 'setup.twig' %}
{% include blocks ~ 'test.twig' %}
{% include blocks ~ 'build.twig' %}
{% include blocks ~ 'test-end-to-end.twig' %}
{% include blocks ~ 'stages/setup.twig' %}
{% include blocks ~ 'stages/test.twig' %}
{% include blocks ~ 'stages/build.twig' %}
{% include blocks ~ 'stages/test-end-to-end.twig' %}
{% else %}
{% include blocks ~ 'build.twig' %}
{% include blocks ~ 'test.twig' %}
{% include blocks ~ 'stages/build.twig' %}
{% include blocks ~ 'stages/test.twig' %}
{% endif %}

{% include blocks ~ 'publish.twig' %}
{% include blocks ~ 'deploy.twig' %}
{% include blocks ~ 'stages/publish.twig' %}
{% include blocks ~ 'stages/deploy.twig' %}

0 comments on commit 76768df

Please sign in to comment.