How to get the BUILD_USER in Jenkins when job triggered by timer?

后端 未结 14 1580
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 03:01

I wanted to show the user who triggered a Jenkins job in the post job email. This is possible by using the plugin Build User Vars Plugin and the env variable

14条回答
  •  一向
    一向 (楼主)
    2020-12-09 03:06

    SIMPLE SOLUTIONS (NO PLUGINS) !!

    METHOD 1: Via Shell

    BUILD_TRIGGER_BY=$(curl -k --silent ${BUILD_URL}/api/xml | tr '<' '\n' | egrep '^userId>|^userName>' | sed 's/.*>//g' | sed -e '1s/$/ \//g' | tr '\n' ' ')
    echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
    

    METHOD 2: Via Groovy

    node('master') {
    BUILD_TRIGGER_BY = sh ( script: "BUILD_BY=\$(curl -k --silent ${BUILD_URL}/api/xml | tr '<' '\n' | egrep '^userId>|^userName>' | sed 's/.*>//g' | sed -e '1s/\$/ \\/ /g'); if [[ -z \${BUILD_BY} ]]; then BUILD_BY=\$(curl -k --silent ${BUILD_URL}/api/xml | tr '<' '\n' | grep '^shortDescription>' | sed 's/.*user //g;s/.*by //g'); fi; echo \${BUILD_BY}", returnStdout: true ).trim()
    echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
    }
    

    METHOD 3: Via Groovy

    BUILD_TRIGGER_BY = "${currentBuild.getBuildCauses()[0].shortDescription} / ${currentBuild.getBuildCauses()[0].userId}"
    echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
    

    OUTPUT:

    Started by user Admin / user@example.com
    

    Note: Output will be both User ID and User Name

提交回复
热议问题