如何在 Jenkins Workflow 插件中获取 SVN 修订号? [英] How to get SVN revision number in Jenkins Workflow Plugin?

查看:19
本文介绍了如何在 Jenkins Workflow 插件中获取 SVN 修订号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jenkins 1.596、Workflow 1.3 和 Svn 插件 2.5.我正在尝试在我的工作流脚本中获取 svn 修订号.

I'm using Jenkins 1.596, Workflow 1.3, and Svn plugin 2.5. I'm trying to get the svn revision number in my workflow script.

我的工作流脚本部分是:

The section of my workflow script is:

node {
   checkout scm: [ $class: "SubversionSCM", locations: [[ remote:'https://secure3.svnrepository.com/somerepo/trunk', credentialsId: cid]] ]
   stage 'build'
   dir('trunk') {
      def revision = 'svn info'.execute().in.text.split('
').find { it.startsWith('Revision') }.split(':')[1].trim()
      println revision
      def svnHome = tool 'Svn'
      sh "$svnHome/bin/svn info"
      def mvnHome = tool 'Maven'
      sh "export JAVA_HOME=/var/jenkins_home/java; $mvnHome/bin/mvn --version"
      sh "export JAVA_HOME=/var/jenkins_home/java; $mvnHome/bin/mvn clean deploy"
}

在这里你看到了两次尝试:第一次打印java.io.IOException: Cannot run program "svn": error=2, No such file or directory",第二次打印No tool named Svn found"(我还尝试了颠覆").尝试 def revision = System.getenv('SVN_REVISION') 打印null".

Here you see two attempts: the first prints "java.io.IOException: Cannot run program "svn": error=2, No such file or directory", and the second says "No tool named Svn found" (I also tried "Subversion"). Trying def revision = System.getenv('SVN_REVISION') prints "null".

知道我该怎么做吗?

推荐答案

No such file or directory 错误的意思就是:Subversion is not installed on your build slave.

The No such file or directory error means just what it says: Subversion is not installed on your build slave.

您似乎已经明白了,并尝试通过使用 tool 安装 Subversion 来解决它.但是 Jenkins Subversion 插件没有 Subversion 的工具定义;它总是使用 SVNKit,一个进程内 (Java) 库.所以这行不通.

You seem to have gotten that, and tried to work around it by using tool to install Subversion. But the Jenkins Subversion plugin has no tool definition for Subversion; it always uses SVNKit, an in-process (Java) library. So this cannot work.

(顺便说一下,Mercurial 插件总是运行 hg 可执行文件,Git 插件可以使用 git 可执行文件或嵌入的 JGit 库.两者都让你定义工具安装,但定义特殊(自动)安装程序,所以它们对这种情况没有多大帮助.你也可以运行 sh 'sudo apt-get install颠覆'.)

(By the way the Mercurial plugin always runs the hg executable, and the Git plugin can use either a git executable or the embedded JGit library. Both let you define tool installations, but do not define special (automatic) installers, so they would not be of much help for this kind of situation. You would do as well running sh 'sudo apt-get install subversion'.)

假设您安装了 Subversion,以便 svn 在您的 $PATH 中,下一个问题是使用 String.execute() 从GDK 通常也不会在工作流中工作.那是因为流程脚本是在 Jenkins 主进程内部运行的,而不是在从属进程上运行的.您必须使用 sh 步骤(或 Windows 从站上的 bat)来运行外部命令.至于从他们那里获取输出,JENKINS-26133 描述了当前的成语.

Assuming you install Subversion so that svn is in your $PATH, the next issue is that using String.execute() from the GDK will not generally work in a Workflow either. That is because the flow script is run inside the Jenkins master process, not on the slave. You must use the sh step (or bat on a Windows slave) to run external commands. As to getting output back from them, JENKINS-26133 describes the current idiom.

String.find 目前无法工作:JENKINS-26481.改用 Java 平台方法,或者使用任何不使用闭包的方法.

String.find from the JDK will not currently work: JENKINS-26481. Use Java Platform methods instead, or just anything not taking a closure.

出于类似于为什么 String.execute() 不合适的原因,System.getenv 无法为工作流构建定义环境变量":这将只加载 Jenkins 主进程上设置的环境变量,在 Jenkins 启动时固定.您正在考虑的变量仅在分叉进程(sh/bat)上设置;或者您可以使用 env.VARIABLE 语法从 Groovy 访问它们.

For reasons similar to why String.execute() is inappropriate, System.getenv will not work to get "environment variables" defined for the workflow build: this will only load environment variables set on the Jenkins master process, fixed at Jenkins startup time. The variables you are thinking of are set only on forked processes (sh/bat); or you can access them from Groovy using env.VARIABLE syntax.

您真正想要开始的是直接访问 SVN_REVISION 而不必自己运行 svn info.这被跟踪为 JENKINS-26100.

What you really wanted to begin with was direct access to SVN_REVISION without having to run svn info yourself. This is tracked as JENKINS-26100.

这篇关于如何在 Jenkins Workflow 插件中获取 SVN 修订号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆