如何在Jenkins工作流程插件中获取SVN版本号? [英] How to get SVN revision number in Jenkins Workflow Plugin?

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

问题描述

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

我的工作流脚本部分为:

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('\n').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:无法运行程序"svn":error = 2,没有这样的文件或目录",第二个显示找不到名为Svn的工具"(我还尝试了"Subversion").尝试def revision = System.getenv('SVN_REVISION')会显示"null".

知道我该怎么做吗?

解决方案

No such file or directory错误表示错误的含义:Subversion未安装在您的构建从站上.

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

(通过Mercurial插件始终运行hg可执行文件的方式,Git插件可以使用git可执行文件或嵌入式JGit库.两者都可以定义工具安装,但可以不能定义特殊的(自动)安装程序,因此对于这种情况,它们不会有太大帮助.您也可以运行sh 'sudo apt-get install subversion'.)

假设您安装Subversion以便$PATH位于$PATH中,那么下一个问题是,从GDK中使用String.execute()通常也不能在工作流中使用.这是因为流脚本是在Jenkins主进程内部运行的,而不是在从属进程上运行的.您必须使用sh步骤(或Windows从属服务器上的bat)来运行外部命令.关于从他们那里获取输出, JENKINS-26133 描述了当前的习惯用法. /p> JDK中的

String.find当前将无法使用: JENKINS-26481 .请改用Java Platform方法,或使用任何不需使用闭包的方法.

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

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

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('\n').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"
}

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".

Any idea how I might do this?

解决方案

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

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.

(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'.)

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 from the JDK will not currently work: JENKINS-26481. Use Java Platform methods instead, or just anything not taking a closure.

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.

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工作流程插件中获取SVN版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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