Skip to content

Commit

Permalink
feat: replacing connector webhook with MS workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardikl committed Sep 30, 2024
1 parent b43656e commit 15d7405
Showing 1 changed file with 183 additions and 4 deletions.
187 changes: 183 additions & 4 deletions jenkins/artifacts/jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ pipeline {
stage("Initialization") {
steps {
buildName "${BUILD_NUMBER}_$BRANCH"
script {
currentStage = 'Initialization'
}
}
}

stage('clean system') {
steps {
stopAndRemoveDockers()
script {
currentStage = 'clean system'
}
}
}

Expand All @@ -67,6 +73,9 @@ pipeline {
pip install mkdocs-material==9.5.6
pip install mkdocs-macros-plugin==0.7.0
'''
script {
currentStage = 'Download Prerequisites'
}
}
}

Expand All @@ -79,6 +88,7 @@ pipeline {

script {
def file = readFile('.harvest.env')
currentStage = 'Git Clone Harvest'
file.split('\n').each { envLine ->
def (key, value) = envLine.tokenize('=')
env."${key}" = "${value}"
Expand All @@ -89,11 +99,13 @@ pipeline {

stage('Setup GO') {
steps {

sh '''
wget -q -O go.tar.gz "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz"
rm -rf /usr/local/go && tar -C /usr/local -xzf go.tar.gz
'''
script {
currentStage = 'Setup GO'
}
}
}

Expand All @@ -111,6 +123,9 @@ pipeline {
./package.sh rpm x86_64 $VERSION $RELEASE $ASUP_MAKE_TARGET $GIT_TOKEN
cp -RT ./dist/$VERSION-$RELEASE/x86_64/ $targetLocation
'''
script {
currentStage = 'Build Harvest RPM/DEB'
}
}
}

Expand All @@ -125,6 +140,9 @@ pipeline {
cp -RT ./dist/ $targetLocation
cp ./autosupport/asup $targetLocation/asup_linux_amd64
'''
script {
currentStage = 'Build Harvest Tar gz'
}
}
}

Expand All @@ -137,6 +155,9 @@ pipeline {
docker run --rm -v "$PWD":/$harvestPath -w /$harvestPath golang:${GO_VERSION} sh -c "export PATH=$PATH:/usr/local/go/bin && make asup VERSION=$VERSION RELEASE=$RELEASE ASUP_MAKE_TARGET=$ASUP_MAKE_TARGET GIT_TOKEN=$GIT_TOKEN"
cp ./autosupport/asup $targetLocation/asup_linux_amd64
'''
script {
currentStage = 'Build Autosupport Binary'
}
}
}

Expand All @@ -146,6 +167,7 @@ pipeline {
script {
// Write the GIT_TOKEN to a temporary file
def gitTokenFile = "${env.WORKSPACE}/git_token"
currentStage = 'Build Docker Images'
writeFile file: gitTokenFile, text: env.GIT_TOKEN

withDockerRegistry([credentialsId: "DockerHub", url: ""]) {
Expand All @@ -165,6 +187,9 @@ pipeline {

stage('Publish builds locally'){
steps {
script {
currentStage = 'Publish builds locally'
}
dir("$targetParentLocation$VERSION-$RELEASE-$BRANCH") {
archiveArtifacts artifacts: '**', fingerprint: true
}
Expand All @@ -179,6 +204,7 @@ pipeline {
}
steps {
script {
currentStage = 'Run Tests'
dockerBuild = "${BUILD_URL}/artifact/docker_harvest.tar"
dockerBuildFips = "${BUILD_URL}/artifact/docker_harvest_fips.tar"
nativeBuild = "${BUILD_URL}/artifact/harvest-${VERSION}-${RELEASE}_linux_amd64.tar.gz"
Expand All @@ -191,6 +217,7 @@ pipeline {
stage('Test and Generate Harvest Metrics') {
steps {
script {
currentStage = 'Test and Generate Harvest Metrics'
def result = sh(script: 'bin/harvest generate metrics --poller dc1 --config /u/mpeg/harvest/harvest.yml', returnStatus: true)
if (result != 0) {
error("Generate Harvest metrics command failed.")
Expand All @@ -213,6 +240,7 @@ pipeline {
docker push ${imageName}:$VERSION-$RELEASE
'''
script {
currentStage = 'Docker Image Publish'
if (OVERWRITE_DOCKER_LATEST_TAG == 'true') {
sh '''
docker push ${imageName}:latest-fips
Expand All @@ -227,6 +255,7 @@ pipeline {
docker push ${ghcrImage}:$VERSION-$RELEASE
'''
script {
currentStage = 'Docker Image Publish'
if (OVERWRITE_DOCKER_LATEST_TAG == 'true') {
sh '''
docker push ${ghcrImage}:latest-fips
Expand All @@ -241,6 +270,7 @@ pipeline {
docker push ${jfrogImagePrefix}:$VERSION-$RELEASE
'''
script {
currentStage = 'Docker Image Publish'
if (OVERWRITE_DOCKER_LATEST_TAG == 'true') {
sh '''
docker push ${jfrogImagePrefix}:latest-fips
Expand All @@ -263,6 +293,7 @@ pipeline {
script {
// Write the GIT_TOKEN to a temporary file
def gitTokenFile = "${env.WORKSPACE}/git_token"
currentStage = 'Publish Nightly Build to GitHub'
writeFile file: gitTokenFile, text: env.GIT_TOKEN

sh '''
Expand Down Expand Up @@ -346,9 +377,157 @@ def getBranchName(gitBranchName, paramBranchName) {
}

def void sendNotification(def status) {
office365ConnectorSend (
status: status,
webhookUrl: "${TEAM_HOOK}"
def harvestProjectName = 'Harvest'
def msWorkflowUrl = "${TEAM_HOOK}"
def prName = env.CHANGE_TITLE ?: "N/A"
def prNo = env.CHANGE_ID ?: "N/A"
def buildState = status
def PrCommitter = env.CHANGE_AUTHOR_DISPLAY_NAME ?: env.CHANGE_AUTHOR ?: "N/A"
def startTime = new Date(currentBuild.startTimeInMillis + currentBuild.duration).format("yyyy-MM-dd HH:mm:ss")
def jenkinsImageUrl = "https://www.jenkins.io/images/logos/jenkins/jenkins.png"
def statusMessage = "Jenkins Build SUCCESS"
def statusColor = "Good"
def stage = "All Stages Passed"

if (buildState == 'FAILURE') {
jenkinsImageUrl = "https://www.jenkins.io/images/logos/fire/fire.png"
statusMessage = "Jenkins Build FAIL"
statusColor = "warning"
stage = "${currentStage}"
}

if (buildState == 'Aborted') {
jenkinsImageUrl = "https://www.jenkins.io/images/logos/fire/fire.png"
statusMessage = "Jenkins Build Aborted"
statusColor = "warning"
stage = "${currentStage}"
}

def payloadData = """
{
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"\$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.4",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Build Notification"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "Image",
"style": "Person",
"url": "${jenkinsImageUrl}",
"altText": "Jenkins Logo",
"size": "Medium",
"height": "50px",
"width": "48px"
}
],
"width": "auto"
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"weight": "Bolder",
"text": "${statusMessage}",
"color": "${statusColor}",
"wrap": true,
"size": "ExtraLarge",
"isSubtle": true
},
{
"type": "TextBlock",
"spacing": "None",
"text": "Build time: ${startTime}",
"wrap": true,
"isSubtle": true
}
],
"width": "stretch"
}
]
},
{
"type": "FactSet",
"facts": [
{
"title": "Application Name:",
"value": "${harvestProjectName}"
},
{
"title": "Stage:",
"value": "${stage}"
},
{
"title": "Job Name:",
"value": "${env.JOB_BASE_NAME}"
},
{
"title": "Build Number:",
"value": "${env.BUILD_NUMBER}"
},
{
"title": "PR Number:",
"value": "${prNo}"
},
{
"title": "Commit Author:",
"value": "${PrCommitter}"
}
],
"spacing": "Medium",
"separator": true
},
{
"type": "TextBlock",
"text": "${prName}",
"wrap": true,
"spacing": "Medium",
"separator": true,
"maxLines": 2,
"size": "Small",
"fontType": "Monospace",
"weight": "Bolder",
"color": "Accent"
}
],
"actions": [
{
"type": "Action.OpenUrl",
"title": "View ${harvestProjectName} Build",
"url": "${env.BUILD_URL}",
"iconUrl": "https://i.ibb.co/Ks2JKfG/cloudbees-logo-icon-168396.png"
}
],
"rtl": false
}
}
]
}
"""

// This POST call would trigger notification to MS Teams channel
httpRequest(
httpMode: 'POST',
acceptType: 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
url: msWorkflowUrl,
requestBody: payloadData
)
}

Expand Down

0 comments on commit 15d7405

Please sign in to comment.