webhook-template/pipeline.groovy

114 lines
5.0 KiB
Groovy

pipeline {
agent any
environment {
CLONE_STAGE_STATUS = "FAIL"
PYLINT_STAGE_STATUS = "FAIL"
GENERATE_BUILD_INFORMATION_STAGE_STATUS = "FAIL"
BUILD_STAGE_STATUS = "FAIL"
UNIT_TEST_STAGE_STATUS = "FAIL"
PYPI_STAGE_STATUS = "FAIL"
AUTO_DOC_STAGE_STATUS = "FAIL"
// WEBHOOK PAYLOAD AND COMMIT DETAILS
WEBHOOK_SENDER_EMAIL = ""
WEBHOOK_SENDER_NAME = ""
WEBHOOK_SENDER_LOGIN = ""
COMMIT_HASH = ""
COMPARE_URL = ""
COMMIT_MSG = ""
BRAND_REF = ""
}
stages {
stage('Clone') {
steps {
script {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
echo 'Start branch development pipeline'
dir('webhook-test') {
git branch: 'main', changelog: false, credentialsId: 'Terence', poll: false, url: 'https://nextgenerations-cities.encs.concordia.ca/gitea/a_nguyen/webhook-test.git'
}
CLONE_STAGE_STATUS = "SUCCESS"
}
}
}
}
stage('Webhook Handler') {
steps {
script {
echo '------- START RAW WEBHOOK PAYLOAD -------'
echo payload
echo '------- END RAW WEBHOOK PAYLOAD -------'
}
script {
// Parse the JSON content using jsonSlurper
def jsonSlurper = new groovy.json.JsonSlurper()
def jsonPayload = payload ? jsonSlurper.parseText(payload) : null
payload ? {
WEBHOOK_SENDER_EMAIL= jsonPayload.sender.email : || currentBuild.getBuildCauses()[0].userId
WEBHOOK_SENDER_NAME=jsonPayload.sender.full_name || "Manually " + currentBuild.getBuildCauses()[0].shortDescription
WEBHOOK_SENDER_LOGIN=jsonPayload.sender.login || "N/A: Manual trigger"
COMMIT_HASH=jsonPayload.after || sh (script: "cd webhook-test && git log -n 1 --pretty=format:'%H'", returnStdout: true)
COMPARE_URL=jsonPayload.compare_url || "N/A: Manual trigger"
COMMIT_MSG=jsonPayload.head_commit.message || sh (script: "cd webhook-test && git log -n 1 --pretty=format:'%B'", returnStdout: true)
BRANCH_REF=jsonPayload.ref || "N/A: Manual trigger"
} : {
WEBHOOK_SENDER_EMAIL=currentBuild.getBuildCauses()[0].userId
WEBHOOK_SENDER_NAME="Manually " + currentBuild.getBuildCauses()[0].shortDescription
WEBHOOK_SENDER_LOGIN="N/A: Manual trigger"
COMMIT_HASH=sh (script: "cd webhook-test && git log -n 1 --pretty=format:'%H'", returnStdout: true)
COMPARE_URL="N/A: Manual trigger"
COMMIT_MSG=sh (script: "cd webhook-test && git log -n 1 --pretty=format:'%B'", returnStdout: true)
BRANCH_REF="N/A: Manual trigger"
}
}
}
}
stage('Get/Set Build Information') {
steps {
sh 'rm -r webhook-test/.jenkins || echo "Creating runtime .jenkins folder. Will be removed in cleanup stage." && mkdir -p webhook-test/.jenkins'
sh 'cd webhook-test && git log -1 --pretty=format:\'%an\' > .jenkins/git_author && git log -1 --pretty=format:\'%ae\' > .jenkins/git_author_email && git log -1 --pretty=format:\'%h\' > .jenkins/git_hash'
script {
GENERATE_BUILD_INFORMATION_STAGE_STATUS="SUCCESS"
}
}
}
stage('Cleanup') {
steps {
script {
git_author = readFile('webhook-test/.jenkins/git_author').trim()
git_author_email = readFile('webhook-test/.jenkins/git_author_email').trim()
git_hash = readFile('webhook-test/.jenkins/git_hash').trim()
}
mail to: "13dd15fb.liveconcordia.onmicrosoft.com@ca.teams.ms",
subject: "${env.BUILD_TAG} Pipeline Results",
body: """
Results of Hub/release Pipeline ${env.BUILD_TAG}
Build triggered by: ${WEBHOOK_SENDER_NAME} - ${WEBHOOK_SENDER_LOGIN}
Email: ${WEBHOOK_SENDER_EMAIL}
--- Commit details ---
HEAD Commit: ${COMMIT_HASH}
Commit message: ${COMMIT_MSG}
Branch: ${BRANCH_REF}
See changes: ${COMPARE_URL}
--- Pipeline stage status ---
Clone Stage: ${CLONE_STAGE_STATUS}
Generate Build Information Stage: ${GENERATE_BUILD_INFORMATION_STAGE_STATUS}
Build Stage : ${BUILD_STAGE_STATUS}
Unit Test Stage: ${UNIT_TEST_STAGE_STATUS}
To access the pipeline run logs, please go here: http://nextgenerations-cities.encs.concordia.ca:8080/job/Hub/job/release/
"""
sh 'rm -rf webhook-test/.jenkins'
cleanWs()
}
}
}
}