在Groovy中检索Jenkins Job Builder凭证

时间:2018-10-26 02:21:42

标签: jenkins groovy jenkins-plugins jenkins-pipeline jira

我正在尝试从启动构建的jenkins groovy脚本中提取用户名和密码。我需要这些详细信息,以便使用我的名字在jira上发表评论。 例如,我登录jenkins并开始工作,然后使用我的登录凭据在jira上发布评论。 我尝试了很多帖子,但没有发现与我的要求有关的任何内容。 任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

在谷歌搜索几秒钟后,我发现了this script officially published by cloudbees

因此,如下:

Jenkins.instance.getAllItems(Job).each{

  def jobBuilds=it.getBuilds()

    //for each of such jobs we can get all the builds (or you can limit the number at your convenience)
    jobBuilds.each { build ->
      def runningSince = groovy.time.TimeCategory.minus( new Date(), build.getTime() )
      def currentStatus = build.buildStatusSummary.message
      def cause = build.getCauses()[0] //we keep the first cause
      //This is a simple case where we want to get information on the cause if the build was 
      //triggered by an user
      def user = cause instanceof Cause.UserIdCause? cause.getUserId():""
      //This is an easy way to show the information on screen but can be changed at convenience
      println "Build: ${build} | Since: ${runningSince} | Status: ${currentStatus} | Cause: ${cause} | User: ${user}" 

      // You can get all the information available for build parameters.
      def parameters = build.getAction(ParametersAction)?.parameters
      parameters.each {
        println "Type: ${it.class} Name: ${it.name}, Value: ${it.dump()}" 

        }
    }
}

您将获得开始工作的用户的用户ID,以确保您将无法获取他的凭据,至少不能以纯文本格式。

很少解释

//to get all jobs
Jenkins.instance.getAllItems(Job)
{...}
//get builds per job
def jobBuilds=it.getBuilds()
//get build cause
def cause = build.getCauses()[0] //we keep the first cause
//if triggered by an user get id, otherwise empty string
def user = cause instanceof Cause.UserIdCause? cause.getUserId():""