<p>Some bad news and some good news :)</p>

<h2>Bad news:</h2>

<p>not a bug. this is a feature (default behaviour)</p>

<p>I don't understand why this bug is still open, either regression, or miscommunication.</p>

<h2>Good news:</h2>

<p>During setup of Global Shared Libraries jenkins allows to mark whether you should trigger user jobs upon changes in the library.
And, you can also mark this during library usage in the job's groovy.</p>

<p>Please refer to:</p>

<ul>
<li><a href="https://issues.jenkins-ci.org/browse/JENKINS-41497" rel="nofollow noreferrer">https://issues.jenkins-ci.org/browse/JENKINS-41497</a></li>
<li><a href="https://stackoverflow.com/questions/46819344/jenkins-shared-groovy-library-git-merge-triggering-jobs">https://stackoverflow.com/questions/46819344/jenkins-shared-groovy-library-git-merge-triggering-jobs</a></li>
</ul>

CC BY-SA 4.0
2018-08-10T08:27:54.167Z
2018-08-10T08:27:54.167Z
mvk_il
<p>Recently, I used this code snippet (that's part of a regular groovy script located inside the 'vars/' directory) to get the project URL as set in the pipeline project config. Hope this helps.</p>

<pre><code>scm.getUserRemoteConfigs()[0].getUrl()
</code></pre>

<p>FYI, it does not expect to have any SCM operation to have taken place.</p>

<p>For more info, refer to this <a href="https://stackoverflow.com/questions/38254968/how-do-i-get-the-scm-url-inside-a-jenkins-pipeline-or-multibranch-pipeline">SO</a> thread. </p>

<p>Hopefully, am not violating the devops stackexchange ground rules with this reference to stack overflow.</p>

CC BY-SA 4.0
2021-06-13T07:51:19.873Z
After all these years I came back to upvote this. I have been parsing the URL from sh step using git remote this entire time =/. The global variable docs says scm is only avaliable in a multibranch pipeline but I'm guess it will work anytime you have configured your giturl and branch in the job config page but I need to test this.
Levi
CC BY-SA 4.0
2018-05-24T04:26:06.753Z
No worries, making references to SO posts is fine.
Dan Cornilescu
CC BY-SA 4.0
2018-05-23T15:40:05.030Z
2018-05-23T15:40:05.030Z
Ramu
<p>Unfortunately I had to <a href="https://wiki.jenkins.io/display/JENKINS/Permissive+Script+Security+Plugin" rel="nofollow noreferrer">disable the Groovy sandbox</a> because I ran into so many situations where methods I wanted to use in my jobs did not appear available for whitelist on the script security page.  Instead of adding a bunch of individual method calls to a whitelist or disabling the sandbox, you can also use a <a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">global shared library</a>, since global shared libraries are automatically whitelisted in the sandbox.  (The shared library approach worked well for me at first, but eventually I ran into situations where I did not feel that the code I was writing was appropriate for a shared library, so I just disabled the sandbox as it had never provided any benefit to me anyway.  Just as a warning, disabling the sandbox is usually fine in single-tenancy situations, but not in multi-tenancy Jenkins instances.)</p>

<p>As for your code, unfortunately <a href="http://javadoc.jenkins-ci.org/hudson/triggers/SCMTrigger.SCMTriggerCause.html" rel="nofollow noreferrer"><code>SCMTriggerCause</code></a> doesn't appear to have any properties pointing to the actual URL of the SCM source (I can't confirm this as I don't use SCM polling on my Jenkins instance).  Instead you could try something like this:</p>

<pre><code>currentBuild.rawBuild.changeSets.collect { it.browser.repoUrl }
</code></pre>

CC BY-SA 3.0
2018-04-04T18:22:56.720Z
I looked at changesets when the job was running using the API, did not see url. I will relook.

EDIT: I'm not seeing it in the rest API but I will try it anyways. When I get a chance.
Levi
CC BY-SA 3.0
2018-04-04T21:50:50.510Z
That's because the URL isn't attached to the changeset directly - it's attached to a browser object, which you will have if you are using GitHub or Bitbucket plugins.
jayhendren
CC BY-SA 3.0
2018-04-04T22:53:19.343Z
All that printed was an empty list. I think it's a good answer, but not for my situation. Something I'm def going to save in my toolbox.
Levi
CC BY-SA 3.0
2018-04-04T18:16:34.703Z
2018-04-04T18:16:34.703Z
jayhendren
<p>My Jenkins is affected by a known Jenkins <a href="https://issues.jenkins-ci.org/browse/JENKINS-39615%20bug" rel="nofollow noreferrer">bug</a> where it will trigger jobs that have an SCM poll set if a change to the shared library happens. I can't have this, so I thought I would get the trigger info from inside the job and if the URL was from the library repo I would abort the build. This has proven to be extremely difficult to do.</p>
<p>This doesn't work...</p>
<pre><code>println (&quot;Cause: &quot; + currentBuild.causes[0])
</code></pre>
<p>or</p>
<pre><code>print currentBuild.rawBuild.getCause(hudson.triggers.SCMTrigger$SCMTriggerCause)
</code></pre>
<p>or</p>
<pre><code>def causes = currentBuild.rawBuild.getCauses()
for (cause in causes) {
    print cause
}
</code></pre>
<p>The first example barfs</p>
<pre><code>org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper causes
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:397)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:381)
</code></pre>
<p>but doesn't allow me to whitelist it on the script security page.</p>
<p>The last 2 print thing <code>hudson.triggers.SCMTrigger$SCMTriggerCause@3</code> but it seems the only thing I can do is call <code>getShortDescription()</code> and it prints <code>Started by an SCM change</code>.</p>
<p>If you know how to print more information about an SCM trigger such as the URL please share =).</p>

<jenkins-pipeline>
Determine the URL for a SCM trigger from inside the build
Levi
# Determine the URL for a SCM trigger from inside the build

(Asked by: username_0 on 2018-04-03)

My Jenkins is affected by a known Jenkins bug where it will trigger jobs that have an SCM poll set if a change to the shared library happens. I can't have this, so I thought I would get the trigger info from inside the job and if the URL was from the library repo I would abort the build. This has proven to be extremely difficult to do.
This doesn't work...
println ("Cause: " + currentBuild.causes[0])

or
print currentBuild.rawBuild.getCause(hudson.triggers.SCMTrigger$SCMTriggerCause)

or
def causes = currentBuild.rawBuild.getCauses()
for (cause in causes) {
    print cause
}

The first example barfs
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper causes
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:397)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:381)

but doesn't allow me to whitelist it on the script security page.
The last 2 print thing hudson.triggers.SCMTrigger$SCMTriggerCause@3 but it seems the only thing I can do is call getShortDescription() and it prints Started by an SCM change.
If you know how to print more information about an SCM trigger such as the URL please share =).

**Tags:** jenkins-pipeline


---

## 3 answers

### Answer by username_1 on 2018-08-10. Score: 1 | Accepted answer

Some bad news and some good news :)

## Bad news:

not a bug. this is a feature (default behaviour)

I don't understand why this bug is still open, either regression, or miscommunication.

## Good news:

During setup of Global Shared Libraries jenkins allows to mark whether you should trigger user jobs upon changes in the library.
And, you can also mark this during library usage in the job's groovy.

Please refer to:

* <https://issues.jenkins-ci.org/browse/JENKINS-41497>
* <https://stackoverflow.com/questions/46819344/jenkins-shared-groovy-library-git-merge-triggering-jobs>


---

### Answer by username_2 on 2018-05-23. Score: 1

Recently, I used this code snippet (that's part of a regular groovy script located inside the 'vars/' directory) to get the project URL as set in the pipeline project config. Hope this helps.
scm.getUserRemoteConfigs()[0].getUrl()

FYI, it does not expect to have any SCM operation to have taken place.
For more info, refer to this SO thread. 
Hopefully, am not violating the devops stackexchange ground rules with this reference to stack overflow.

> **Comments:**
> **username_3**: No worries, making references to SO posts is fine.  
> **username_0**: After all these years I came back to upvote this. I have been parsing the URL from sh step using git remote this entire time =/. The global variable docs says scm is only avaliable in a multibranch pipeline but I'm guess it will work anytime you have configured your giturl and branch in the job config page but I need to test this.  

---

### Answer by username_4 on 2018-04-04. Score: 0

Unfortunately I had to disable the Groovy sandbox because I ran into so many situations where methods I wanted to use in my jobs did not appear available for whitelist on the script security page.  Instead of adding a bunch of individual method calls to a whitelist or disabling the sandbox, you can also use a global shared library, since global shared libraries are automatically whitelisted in the sandbox.  (The shared library approach worked well for me at first, but eventually I ran into situations where I did not feel that the code I was writing was appropriate for a shared library, so I just disabled the sandbox as it had never provided any benefit to me anyway.  Just as a warning, disabling the sandbox is usually fine in single-tenancy situations, but not in multi-tenancy Jenkins instances.)
As for your code, unfortunately SCMTriggerCause doesn't appear to have any properties pointing to the actual URL of the SCM source (I can't confirm this as I don't use SCM polling on my Jenkins instance).  Instead you could try something like this:
currentBuild.rawBuild.changeSets.collect { it.browser.repoUrl }

> **Comments:**
> **username_0**: I looked at changesets when the job was running using the API, did not see url. I will relook.  EDIT: I'm not seeing it in the rest API but I will try it anyways. When I get a chance.  
> **username_4**: That's because the URL isn't attached to the changeset directly - it's attached to a browser object, which you will have if you are using GitHub or Bitbucket plugins.  
> **username_0**: All that printed was an empty list. I think it's a good answer, but not for my situation. Something I'm def going to save in my toolbox.  

---

