我如何在 Jenkins 全局库中使用 sh

How do I use sh in Jenkins Global library

我正在为 Jenkins 创建我自己的全局库,我有 hosted on github,为了简化一些 运行-of-the-mill 任务,我想添加一个函数 returns GIT 标签。

因此我创建了这样的东西:

    class Myclass{
      static String getGitTag() {
        return "${sh(returnStdout: true, script: 'git tag --sort version:refname | tail -1').trim()}"
      }
    }

... 导致此错误:

No signature of method: static com.stevnsvig.jenkins.release.ReleaseUtil.sh()

所以我还有两个问题:

  1. Jenkins 的groovy 风格显然已经导入了sh() 库的解决方案是导入吗? (如果是的话如何)
  2. 此处的最佳做法是什么?我想知道为什么在使用声明性管道时没有 GIT_TAG 全局变量,而像这样的事情(在我看来)应该很容易。

编辑#1:

    static String getGitTag() {
        stdout = script.sh(script: "git tag --sort version:refname | tail -1", returnStdout: true)
        return stdout.trim()
    }

产生类似的错误:

No signature of method: static com.stevnsvig.jenkins.release.ReleaseUtil.sh() is applicable for argument types: (java.util.LinkedHashMap) values: [[returnStdout:true, script:git tag --sort version:refname | tail -1]]

编辑#2:

   static String getGitTag() {
        def stdout = "git tag --sort version:refname | tail -1".execute()
        return stdout.in.text
    }

完成,但输出为空白。 运行 与pwd returns / 相同的命令表示未设置环境,这是有道理的,因为Jenkins 下的所有命令运行ning 都是旨在运行在管道

编辑#3:

我去寻找进口。在 github 上偶然发现了 Jenkins CI 项目,并开始搜索许多存储库。 Found a promising one... 并将名为 pwd.groovy 的文件放入 /vars 中,内容如下:

import org.jenkinsci.plugins.workflow.steps.durable_task.ShellStep


    static String getPWD() {
        def ret = ShellStep.sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()
        echo "currently in ${ret}"
    }

我得到的错误是同一个错误的变体。我想自从itsa插件以来,定义就不同了...

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static org.jenkinsci.plugins.workflow.steps.durable_task.ShellStep.sh() is applicable ...

选项 1) 使用 Groovy execute 到 运行 cmd 并得到如下输出

tag = "git tag --sort version:refname | tail -1".execute().text

选项 2) 使用 Jenkins 管道步骤 sh

需要弄清楚一个概念:sh is global function 的上下文是在 Jenkinsfile 中 sh 直接 使用 的上下文。

在你的例子中,sh 在 Jenkinsfile 之外使用。为了更好地理解,我举了一个 Jenkinsfile 的例子。

pipeline {
 
  stages('foo') {
    steps {
       sh 'pwd' 
       // In above sh step, there is an implicit `this` which represents the 
       // global object for Jenkinsfile, you can image  sh 'pwd'  to this.sh 'pwd'
       // 
       // Thus if you want to use `sh` outside Jenkinsfile, you must pass down the
       // implicit `this` into the file where you used `sh`
    }
  }
}

解决您的问题

// ReleaseUtil.groovy

static String getGitTag(steps) {
   // here `steps` is the global object for Jenkinsfile
   // you can use other pipeline step here by `steps`

   steps.echo 'test use pipeline echo outside Jenkinsfile'

   steps.withCredentials([steps.string(credentialsId: 'git_hub_auth', variable: 'GIT_AUTH_TOKEN')]) {
      steps.echo '....'
      steps.sh '....'
   }

   
   return  steps.sh(returnStdout: true, script:"git tag --sort version:refname | tail -1").trim()
}
// Jenkinsfile

import com.stevnsvig.jenkins.release.ReleaseUtil

pipeline {
 
  stages('foo') {
    steps {
       ReleaseUtil.getGitTag(this)
    }
  }
}