HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Determine the URL for a SCM trigger from inside the build

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
thetriggerfordeterminescmfrombuildurlinside

Problem

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 =).

Solution

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.

Code Snippets

scm.getUserRemoteConfigs()[0].getUrl()

Context

StackExchange DevOps Q#3798, answer score: 1

Revisions (0)

No revisions yet.