Search code examples
svnjenkinsjenkins-workflow

How to get SVN revision number in Jenkins Workflow Plugin?


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?


Solution

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