2

Is there a variable in Jenkins, which I can use, to get which SCM is configured for the job?

i.e Git, Subversion, etc...

I am hoping to have this information at the time of build like with msbuild

I am using SVN_URL and GIT_URL at the moment. Want to know if there is a better way.

Idothisallday
  • 311
  • 1
  • 3
  • 14

1 Answers1

1

Your msbuild script can access environment variables set by Jenkins, as you mention.

If you see a non-empty environment variable named:

  • GIT_COMMIT: that means there is a Git repository associated to your job
  • SVN_REVISION: that means there is a SVN repository associated to your job
  • CVS_BRANCH: that means there is a CVS repository associated to your job

More complex approach:

As in this groovy script, you can check:

  • if there is any scm defined for a job

    scm = project.scm;
    
  • if that scm is Git or another SCM (like the Mercurial one)

    if (scm instanceof hudson.plugins.git.GitSCM) {
    

From there, your groovy script can inject in your job a new environment variable (with the JENKINS EnvInject Plugin)

import hudson.model.*
def build = Thread.currentThread().executable
def pa = new ParametersAction([
  new StringParameterValue("SCM", "Git")
])
build.addAction(pa)

The OP Idothisallday asks in the comments:

How is above var better than using SVN_URL and GIT_URL?

This is the same general idea. The second approach let you define your own value), but to the general question "Want to know if there is a better way.": no, not really (that I know of).

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi @Vonc, i have added more context, need it in msbuild and not groovy script thanks – Idothisallday Apr 19 '17 at 06:44
  • @Idothisallday you can still use groovy as a pre-build step. The rest can use msbuild command, since groovy set an environment variable that can then ben accessed/read by the msbuild process. – VonC Apr 19 '17 at 06:45
  • Ahh, I will give it a try.Is project.scm not available just like other jenkins variables? – Idothisallday Apr 19 '17 at 06:56
  • I am still not sure, how is above var better than using SVN_URL and GIT_URL. – Idothisallday Apr 20 '17 at 18:15
  • @Idothisallday it is the same general idea (the second approach let you define your own value), but to your general question "Want to know if there is a better way.": no, not really (that I know of) – VonC Apr 20 '17 at 18:30
  • Thanks VonC, if you can add this context to your answer (for completeness and others who may search for this), i will accept it. As i have not seen any other answer. – Idothisallday Apr 20 '17 at 19:41