How to get scm url in build script for jenkins multibranch workflow project

I am trying to get a prebuild merge to work inside a multibranch pipeline and I would like to avoid having to hardcode the git url in my pipeline script.

It seems like scm step must store the url somehow, but I cannot figure out how to access it.

  • Android Studio - unable to merge from SVN branch
  • Git subtree merge strategy or subtree command?
  • Proper git workflow scheme with multiple developers working on same task
  • commit count calculation in git-describe
  • Git “leaky” branches?
  • How to resolve conflict of renamed folder of file in git?
  • CSS not updating after Heroku deployments
  • Git pull - error: The following untracked working tree files would be overwritten by merge:
  • Using ssh-agent with jenkins while polling SCM
  • Maintain hundred of customized branches over master branch
  • How can I push a specific branch of Git to my server?
  • How can I download a previous git commit in Netbeans?
  • 3 Solutions collect form web for “How to get scm url in build script for jenkins multibranch workflow project”

    You are correct, the scm object does have the information you need.

    You’re using a git multibranch pipeline, so the scm global variable will be an instance of GitSCM. That means that `scm.getUserRemoteConfigs()’ will return a list of UserRemoteConfig instances. Those instances have the git remote’s name, url, and refspec. You can iterate over that list to find a matching remote, or just take the first one if your sure you only have one url.

    def scmUrl = scm.getUserRemoteConfigs()[0].getUrl()
    

    NOTES

    • RejectedAccessException – The getUserRemoteConfigs and getUrl methods will both throw org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException until you manually approve them, under “Manage Jenkins -> In-process Script Approval”. The only way I’ve found to do this is to try running the script, have it throw an access exception, approve the one method that caused the exception, and repeat for each method until no more access exceptions are thrown. Happily the setting is server-wide, so you only have to do this once per jenkins controller, not for each pipeline job.

    • GitHub – While testing with a GitHub-sourced multibranch pipeline, getUserRemoteConfigs returned two UserRemoteConfig instances, one for regular branches and another for pull requests. These had the same url so no big deal, but something to keep in mind:

      echo scm.getUserRemoteConfigs()
      
      "[
          +refs/heads/*:refs/remotes/origin/* => https://github.com/bitwiseman/project.git (origin),
          +refs/pull/*/head:refs/remotes/origin/pr/* => https://github.com/bitwiseman/project.git (origin)
      ]"
      

    Inspired by a comment in answer by @BitwiseMan, I have found a (hacky) way to get the URL without RejectedAccessException:

    checkout scm
    def url = sh(returnStdout: true, script: 'git config remote.origin.url').trim()
    

    Please note that it must be done after checkout scm. Basically, you must be in a checked out git repository (i.e. has .git/config file in it)

    You have to store the git url inside your parent job and pass the parameter to the child phase

    or you can simply clone the project on your root job and work form there.

    Use the https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin its the Jenkins git plugin

    Git Baby is a git and github fan, let's start git clone.