Skip to content

Commit

Permalink
Add twig extends syntax to override Jenkinsfile
Browse files Browse the repository at this point in the history
Base blocks are able to be optionally overriden

Just an idea atm to get feedback. Perhaps the base file is small enough with notification template change without having to introduce it
  • Loading branch information
andytson-inviqa committed Oct 30, 2023
1 parent f8c4bb3 commit ec56f8e
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 77 deletions.
99 changes: 36 additions & 63 deletions application/overlay/Jenkinsfile.twig
Original file line number Diff line number Diff line change
@@ -1,70 +1,43 @@
{% set blocks = 'application/overlay/_twig/Jenkinsfile/' %}
def failureMessages = []
pipeline {
agent { label 'linux-amd64' }
environment {
COMPOSE_DOCKER_CLI_BUILD = {{ @('jenkins.docker.buildkit.enabled') ? '1' : '0' }}
DOCKER_BUILDKIT = {{ @('jenkins.docker.buildkit.enabled') ? '1' : '0' }}
{% if @('jenkins.credentials.my127ws_key') %}
MY127WS_KEY = credentials('{{ @('jenkins.credentials.my127ws_key') }}')
{% endif %}
MY127WS_ENV = "pipeline"
}
triggers { cron(env.BRANCH_NAME == '{{ @('git.default_branch') }}' ? 'H H(2-6) * * 1' : '') }
stages {
{% include blocks ~ 'stages.twig' %}
}
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}"
{% extends blocks ~ 'base.twig' %}

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 %}
{#
{% block agent %}
agent {
docker {
label 'linux-amd64'
alwaysPull true
image 'quay.io/inviqa_images/workspace:latest'
args '--group-add docker --entrypoint "" --volume /var/run/docker.sock:/var/run/docker.sock --volume "$HOME/.my127:/root/.my127"'
}
}
{% endblock %}
def failureMessage = failureMessages.join("\n")
if (failureMessage) {
fields << [
title: 'Reason(s)',
value: failureMessage,
short: false
]
fallbackMessages << failureMessage
{% block stages %}
stage('Build, Test') {
when {
anyOf {
triggeredBy 'TimerTrigger'
allOf {
not { branch 'main' }
}
}
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') }}')
}
stages {
{% include blocks ~ 'stages-build-test.twig' %}
}
}
{% endif %}
always {
sh 'ws destroy'
cleanWs()
stage('Publish, Release') {
when {
not { triggeredBy 'TimerTrigger' }
anyOf {
branch 'develop'
branch 'main'
}
}
stages {
{% include blocks ~ 'stages-publish-release.twig' %}
}
}
}
}
{% endblock %}
#}
33 changes: 33 additions & 0 deletions application/overlay/_twig/Jenkinsfile/base.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% set blocks = 'application/overlay/_twig/Jenkinsfile/' %}
def failureMessages = []
pipeline {
{% block agent %}
agent { label 'linux-amd64' }
{% endblock %}
environment {
{% block environment %}
COMPOSE_DOCKER_CLI_BUILD = {{ @('jenkins.docker.buildkit.enabled') ? '1' : '0' }}
DOCKER_BUILDKIT = {{ @('jenkins.docker.buildkit.enabled') ? '1' : '0' }}
{% if @('jenkins.credentials.my127ws_key') %}
MY127WS_KEY = credentials('{{ @('jenkins.credentials.my127ws_key') }}')
{% endif %}
MY127WS_ENV = "pipeline"
{% endblock %}
}
triggers { cron(env.BRANCH_NAME == '{{ @('git.default_branch') }}' ? 'H H(2-6) * * 1' : '') }
stages {
{% block stages %}
{% include blocks ~ 'stages-build-test.twig' %}
{% include blocks ~ 'stages-publish-release.twig' %}
{% endblock %}
}
post {
{% block post %}
{% include blocks ~ 'notifications.twig' %}
always {
sh 'ws destroy'
cleanWs()
}
{% endblock %}
}
}
45 changes: 45 additions & 0 deletions application/overlay/_twig/Jenkinsfile/notfications.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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') }}')
}
}
11 changes: 11 additions & 0 deletions application/overlay/_twig/Jenkinsfile/stages-build-test.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% set blocks = 'application/overlay/_twig/Jenkinsfile/' %}

{% if @('jenkins.tests.isolated') %}
{% 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 ~ 'stages/build.twig' %}
{% include blocks ~ 'stages/test.twig' %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% include blocks ~ 'stages/publish.twig' %}
{% include blocks ~ 'stages/deploy.twig' %}
14 changes: 0 additions & 14 deletions application/overlay/_twig/Jenkinsfile/stages.twig

This file was deleted.

0 comments on commit ec56f8e

Please sign in to comment.