Jenkins Java: Get user who started the build

后端 未结 2 1834
生来不讨喜
生来不讨喜 2021-01-13 18:52

Under http://[JENKINS_NAME]/job/[JOB_NAME]/[BUILD_NUMBER]/

I can see Started by user [USER_NAME].

I want to get that username f

2条回答
  •  心在旅途
    2021-01-13 19:23

    You could get the build user from Jenkins environment (i.e as an env var). If you use Jenkins 2 pipeline, For example: pipeline {

     //rest of the pipeline
     stages {
       stage('Build Info') {
         steps {
           wrap([$class: 'BuildUser']) {
            sh 'java -jar .jar'
           }
         } 
       }
     }
    

    In your java app you should be able to get the environment variable using System.getenv("BUILD_USER") or else you could pass it as a JVM arg. Ex: sh 'java -jar -DbuildUser=$BUILD_USER .jar' and get the buildUser system property in the application.

    On older version of Jenkins, you may use Build User Vars Plugin or Env Inject plugin. As in the answers on this question. how to get the BUILD_USER in Jenkins when job triggered by timer

提交回复
热议问题