Skip to content

Instantly share code, notes, and snippets.

@caryyu
Created February 7, 2021 10:20
Show Gist options
  • Save caryyu/6437b602d292b65327ab7387f2979ef9 to your computer and use it in GitHub Desktop.
Save caryyu/6437b602d292b65327ab7387f2979ef9 to your computer and use it in GitHub Desktop.
Lark notification in Jenkinsfile

Script

Please use the script below as a shell command and save to /usr/local/bin/lark in the execution environment

#!/usr/bin/env bash

action=$1
url=$2

# This method won't work
#"duration": $(echo $(($(date +%s%N)/1000000 - ${TAG_TIMESTAMP:-0}))),

JSON_BODY=$(cat <<EOF
{
  "action": "${action}",
  "type": "job",
  "name": "${BUILD_TAG}",
  "order": "${GIT_BRANCH}-${GIT_COMMIT}",
  "buildurl": "${BUILD_URL}",
  "joburl": "${JOB_URL}",
  "duration": 0,
  "causeby": "点击【查看】按钮"
}
EOF
)

curl -si \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-X POST --data "$JSON_BODY" \
${url}

Jenkinsfile

pipeline {
  agent any
  stages {
    stage('Hello World') {
      ....
    }
  }
  post {
    aborted {
      sh 'lark "abort" "<webhook url>"'
    }
    failure {
      sh 'lark "failed" "<webhook url>"'
    }
    success {
      sh 'lark "success" "<webhook url>"'
    }
  }
}

the url of webhook will look like this: https://open.feishu.cn/officialapp/notify/xxxxx

@caryyu
Copy link
Author

caryyu commented Feb 8, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment