Groovy build script: What triggered the build

4,272 views
Skip to first unread message

Sverre Moe

unread,
Feb 16, 2015, 7:06:07 AM2/16/15
to jenkins...@googlegroups.com
Is it possible to find out who or what triggered the build from a Groovy script? Either an SCM change, another project or user. I have just begun reading a little about Groovy and the Jenkins API.

I want to check for the following conditions and build accordingly.

if (trigger == scm) {
  build_with_automake
  new_version = git tag exist for version
  if (new_version) {
    tag new release candidate in Git
    publish rc
  }
}
else if (trigger == "Build other projects") {
  build_with_automake
}

The project should build on every SCM change, but only tag and publish if version has been increased. It should also build when a build has been triggered by another project.

Ginga, Dick

unread,
Feb 17, 2015, 8:21:59 AM2/17/15
to jenkins...@googlegroups.com

There is a BUILD_CAUSE environment variable that can have values like: SCMTRIGGER, MANUALTRIGGER, and others

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/a4a10f09-9846-4c6e-b572-ed5123f0977d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sverre Moe

unread,
Feb 19, 2015, 6:53:57 AM2/19/15
to jenkins...@googlegroups.com
Excellent. I could not find any documentation about this variable. It is not listed among the environment variables of Jenkins. Makes me wonder which other variables are available that could be usefull.

When I trigger the build manually in Jenkins I got BUILD_CASE = UPSTREAMTRIGGER
When triggered by SCM change BUILD_CAUSE = SCMTRIGGER.
When triggered by an downstream project UPSTREAMTRIGGER (a library triggered a build on the app that uses it.

So it looks like the BUILD_CAUSE is the same for both manually trigger and triggered by an upstream/downstream project.
However I think I could work this that since I would have the same build procedure for both manual and automatic UPSTREAMTRIGGER build.
Still would be nice to also have BUILD_CAUSE_BY, user or project.

Where do I find a complete list of variables I could use?

Ginga, Dick

unread,
Feb 19, 2015, 8:39:54 AM2/19/15
to jenkins...@googlegroups.com

Basically, many plugins contribute tokens. I think these come from the Environment inject plugin:

 

Build Causes

This plugin also exposes the cause of the current build as an environment variable. A build can be triggered by multiple causes at the same time e.g. an SCM Change could have occurred at the same time as a user triggers the build manually.

The build cause is exposed as a coma separated list:

BUILD_CAUSE=USERIDCAUSE, SCMTRIGGER, UPSTREAMTRIGGER

In addition, each cause is exposed as a single envvariable too:

BUILD_CAUSE_USERIDCAUSE=true

BUILD_CAUSE_SCMTRIGGER=true

BUILD_CAUSE_UPSTREAMTRIGGER=true

 

It looks like you can get the user id

Sverre Moe

unread,
Feb 23, 2015, 8:17:41 AM2/23/15
to jenkins...@googlegroups.com
It seems also like there is access to the build cause in the Jenkins API.
http://javadoc.jenkins-ci.org/hudson/model/Cause.html

So instead of accessing an environment variable, I could instead access it programmatically in my Groovy build script.
Trying to find any groovy script examples using Cause from Jenkins API, no such luck.

Baptiste Mathus

unread,
Feb 23, 2015, 11:11:16 AM2/23/15
to jenkins...@googlegroups.com

Basically groovy script are just like java code with syntactical sugar. I mean if you find the Jenkins api you want to use, doing it in groovy is generally very very easy.
In your case the "hardest" part is getting hold onto the current build reference in a system groovy script in a build step. Then the rest is east as you already found where to begin (if not have a look at the build trigger badge plugin code [shameless plug]).

And if this if something you want to offer as a simple build feature, then writing a plugin might even be a good and quite  simple way too.

Cheers

Sverre Moe

unread,
Mar 11, 2015, 3:55:45 AM3/11/15
to jenkins...@googlegroups.com, m...@batmat.net
Running a script in Groovy Postbuild.

All my projects are Multi-configuration project, Matrix build. When I trigger the build manually, the cause is UpstreamCause, but toString() contains UserIdCause. When one of my projects gets triggered from a upstream project, then the cause is still UpstreamCause, with content UserIdCause.
I have a script I want to run only if it is UpstreamCause, i.e. triggered by an upstream project.

ProjectA:
manager.build.getCauses().each { cause ->
    cause
.toString() == "job/projectA/148[job/projectA/14[hudson.model.Cause$UserIdCause@caebc9aa]]"
    cause
.getClass() == "class hudson.model.Cause$UpstreamCause"
}


ProjectB triggered by ProjectA:
manager.build.getCauses().each { cause ->
    cause
.toString() == "job/projectB/14[hudson.model.Cause$UserIdCause@caebc9aa]"
    cause
.getClass() == "class hudson.model.Cause$UpstreamCause"
}


I need to check if a project has been triggered because of an upstream project, but if everything is upstream then what is the point of the Cause.
Last resort: parse cause..getShortDescription()
Started by upstream project "projectA" build number 148
if (cause.getShortDescription().contains("Started by upstream project")


Tried accessing through variable, 
def userCause = manager.envVars["BUILD_CAUSE"]
but in both these cases the cause is UPSTREAMTRIGGER. It seems like the downstream project inherits the cause from the upstream project.

Björn Pedersen

unread,
Mar 11, 2015, 4:12:44 AM3/11/15
to jenkins...@googlegroups.com, m...@batmat.net
Hi,

maybe it is issue #24785 what is causing this behaviuor?

Björn

Sverre Moe

unread,
Mar 11, 2015, 5:06:31 AM3/11/15
to jenkins...@googlegroups.com, m...@batmat.net
Seems Jenkins issue #24785 was fixed by introducing ROOT_BUILD_CAUSE. It is a little bit helpful, but does not help with the downstream triggered projects. 
ProjectA is triggered and ROOT_BUILD_CAUSE == MANUALTRIGGER
ProjectB is triggered from ProjectA and ROOT_BULD_CAUSE == MANUALTRIGGER.

ProjectB was never triggered manually, but as a downstream trigger from ProjectA.
So I need to find out if a Project is triggered by User, SCM or if it was triggered from an upstream project.

Wonder if this is yet another bug in Jenkins. Downstream project should not get the same (ROOT_)BUILD_CAUSE as its upstream project.

Sverre Moe

unread,
Mar 11, 2015, 6:32:46 AM3/11/15
to jenkins...@googlegroups.com, m...@batmat.net
It seems like another bug in Jenkins.
Using freestyle the build cause for the downstream project is upstream, while for multi-config the build cause is the same as for the upstream project.

I created a new Jenkins issue for this:

Hiteswar

unread,
Mar 11, 2015, 6:43:24 AM3/11/15
to jenkins...@googlegroups.com, jenkin...@googlegroups.com, hit...@gmail.com
Hi Gang ,
Please suggest how to allow and authenticate multiple users in 2 or more different active directories ?

Example .
User a in group aGroup active directory A.net
And use b in group bGroup of another active directoy B.net

Then how to configure both active directory A.net and B.net in active directly plugin so both user a and b can be authenticated and login successfully ?

If this is not avail in existing latest active directly plugin then is there any future scope to add in plugin ?

Please suggest and comment .

Regards
Hiteswar
Reply all
Reply to author
Forward
0 new messages