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.
There is a way to do it using the variables for each SVN module. See the answers below
https://stackoverflow.com/a/24956967/5842403
Basically you use the $SVN_REVISION_x variable where "x" is a counter of the svn URL module. You can echo that variable and put in a file as an artifact.
It's a known bug: https://issues.jenkins-ci.org/browse/JENKINS-32744.
I had the same issue (environment variable isn't loaded).
You can get the revision number from a file named "revision.txt" in the path: "C:\Program Files (x86)\Jenkins\jobs\${pipelineTaskName}\builds\${buildNumber}".
Since I found a nice script in the internet (I do not remember where) and we use groovy scripting, I wanted to share this solution which worked for me very well:
def url = "ENTER HERE YOUR SVN URL"
def SVN_REVISION = "svn info --show-item last-changed-revision $url".execute().text
echo "$SVN_REVISION" /*should work now*/
Sorry for exhuming this thread, but I hope this will work for some people.
Do not forget to use double quotation marks when using these variables.
This was kinda difficult to find out for me. :-/