webhook-template/pipeline.groovy

114 lines
4.7 KiB
Groovy
Raw Normal View History

2023-09-18 14:26:47 -04:00
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"
2023-09-19 12:39:46 -04:00
// WEBHOOK PAYLOAD AND COMMIT DETAILS
2023-09-18 17:08:02 -04:00
WEBHOOK_SENDER_EMAIL = ""
WEBHOOK_SENDER_NAME = ""
WEBHOOK_SENDER_LOGIN = ""
COMMIT_HASH = ""
COMPARE_URL = ""
2023-09-19 12:39:46 -04:00
COMMIT_MSG = ""
BRAND_REF = ""
2023-09-18 14:26:47 -04:00
}
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 {
2023-09-19 12:39:46 -04:00
echo '------- START RAW WEBHOOK PAYLOAD -------'
echo payload
echo '------- END RAW WEBHOOK PAYLOAD -------'
}
script {
2023-09-18 14:26:47 -04:00
// Parse the JSON content using jsonSlurper
def jsonSlurper = new groovy.json.JsonSlurper()
2023-09-19 12:42:58 -04:00
def jsonPayload = payload ? jsonSlurper.parseText(payload) : null
payload ? {
2023-09-19 12:44:15 -04:00
WEBHOOK_SENDER_EMAIL= jsonPayload.sender.email
WEBHOOK_SENDER_NAME=jsonPayload.sender.full_name
WEBHOOK_SENDER_LOGIN=jsonPayload.sender.login
COMMIT_HASH=jsonPayload.after
COMPARE_URL=jsonPayload.compare_url
COMMIT_MSG=jsonPayload.head_commit.message
BRANCH_REF=jsonPayload.ref
2023-09-19 12:42:58 -04:00
} : {
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"
}
2023-09-18 14:26:47 -04:00
}
}
}
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}
2023-09-19 12:39:46 -04:00
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}
2023-09-18 14:26:47 -04:00
2023-09-19 12:39:46 -04:00
--- Pipeline stage status ---
2023-09-18 14:26:47 -04:00
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()
}
}
}
}