[JIRA] (JENKINS-54285) Changeset not reflective of Pull Request

2 views
Skip to first unread message

mike@silverorange.com (JIRA)

unread,
Oct 26, 2018, 6:54:02 PM10/26/18
to jenkinsc...@googlegroups.com
Michael Gauthier created an issue
 
Jenkins / Bug JENKINS-54285
Changeset not reflective of Pull Request
Issue Type: Bug Bug
Assignee: Unassigned
Components: blueocean-plugin
Created: 2018-10-26 22:53
Labels: github changeset declarative pipeline when
Priority: Minor Minor
Reporter: Michael Gauthier

I'm using Blue Ocean's default multi-branch setup for GitHub. I am using Jenkins to build and test Pull Requests from GitHub.

I have a repository with multiple subproject directories that I would like to selectively build and test depending on changes in the Pull Request. Looking at the documentation it seems like I should use `when { changeset 'my-dir/**' }` to do this; however the changeset does not contain the changes from the Pull Request, but instead contains the changes since last build + the changes merged to master since the last build.

 

I think the behavior here should be to consider the changes in the Pull Request to make it possible to use this conditional.

Add Comment Add Comment
 
This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:42:04 PM11/14/18
to jenkinsc...@googlegroups.com
Michael Gauthier commented on Bug JENKINS-54285
 
Re: Changeset not reflective of Pull Request

I was able to work around this issue by defining a function for my pipeline:

 
def boolean hasChangesIn(String module) {
return !env.CHANGE_TARGET || sh(
returnStatus: true,
script: "git diff --name-only origin/${env.CHANGE_TARGET}...${env.GIT_COMMIT} | grep ^${module}/"
) == 0
{color:#24292e}}
 
and then using:

 
when { expression { return hasChangesIn('my-dir') } }
 
in my pipeline stages
 

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:43:01 PM11/14/18
to jenkinsc...@googlegroups.com
Michael Gauthier edited a comment on Bug JENKINS-54285
I was able to work around this issue by defining a function for my pipeline:

{{   }}
{
{ { color:#d73a49}def{color} {color:#d73a49}boolean{color} {color:#6f42c1}hasChangesIn{color}{color:#24292e}({color}{color:#d73a49}String{color}{color:#24292e} module) {{color} }}

{ {{ color:#d73a49}   return{color} {color:#d73a49}!{color}{color:#24292e}env{color}{color:#d73a49}.{color}{color:#24292e}CHANGE_TARGET {color}{color:#d73a49}||{color}{color:#24292e} sh({color} }}

{ {{ color:#24292e}     returnStatus: {color}{color:#005cc5}true{color}{color:#24292e},{color} }}

{ {{ color:#24292e}     script: {color}{color:#22863a}"git diff --name-only origin/${env.CHANGE_TARGET}...${env.GIT_COMMIT} | grep ^${module}/"{color} }}

{ {{ color:#24292e}   ) {color}{color:#d73a49}=={color} {color:#005cc5}0{color} }}

{ color:#24292e { }} {color }
{{   }}
{
color:#24292e} { and then using: {color } }
{{   }}
{{{
{{ color:#24292e}when \{ expression { return hasChangesIn('my-dir') } }{color}}} }}
{{   }}
{
{ { color:#24292e}in my pipeline stages{color} }}
{{   }}

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:46:02 PM11/14/18
to jenkinsc...@googlegroups.com
Michael Gauthier edited a comment on Bug JENKINS-54285
I was able to work around this issue by defining a function for my pipeline:

{{
 }}
{{ {color:#d73a49} def {color} {color:#d73a49} boolean {color} {color:#6f42c1} hasChangesIn {color}{color:#24292e} ( {color}{color:#d73a49} String {color}{color:#24292e} module) \ { {color}}}

{{{color:#d73a49}
  return {color} {color:#d73a49} ! {color}{color:#24292e} env {color}{color:#d73a49} . {color}{color:#24292e} CHANGE_TARGET {color}{color:#d73a49} || {color}{color:#24292e} sh( {color}}}

{{{color:#24292e}
    returnStatus: {color}{color:#005cc5} true {color}{color:#24292e} , {color}}}

{{{color:#24292e}
    script: {color}{color:#22863a} "git diff --name-only origin/$ \ {env.CHANGE_TARGET \ }...$ \ {env.GIT_COMMIT \ } | grep ^$ \ {module \ }/" {color}}}

{{{color:#24292e}
  ) {color}{color:#d73a49} == {color} {color:#005cc5} 0 {color}}}

{{
} }}
{{   }}
{{
and then using: }}
{{  }}
{{
{{{color:#24292e} when \{ expression \ { return hasChangesIn('my-dir') \ } \ } {color }} }}}
{{  }}
{{ {color:#24292e} in my pipeline stages {color}}}
{{  }}

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:47:01 PM11/14/18
to jenkinsc...@googlegroups.com
Michael Gauthier edited a comment on Bug JENKINS-54285
I was able to work around this issue by defining a function for my pipeline:

{{

def boolean hasChangesIn(String module) \{ }}
{{   return !env.CHANGE_TARGET || sh( }}
{{     returnStatus: true, }}
{{     script: "git diff --name-only origin/$\{env.CHANGE_TARGET\}...$\{env.GIT_COMMIT\} | grep ^$\{module\}/" }}
{{   ) == 0
} }
{{\ }} }

and then using:

{{ when \{ expression \{ return hasChangesIn('my-dir') \} \} }}

in my pipeline stages

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:48:02 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:49:01 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:49:02 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:50:01 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:51:40 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:51:40 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:51:40 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:51:41 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:51:41 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:51:41 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:52:00 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:52:01 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:52:01 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:53:02 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:53:02 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:54:01 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:54:02 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:54:02 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:56:01 PM11/14/18
to jenkinsc...@googlegroups.com
Michael Gauthier edited a comment on Bug JENKINS-54285
I was able to work around this issue by defining a function for my pipeline:

{{def boolean hasChangesIn(String module) {}}
{{  return !env.CHANGE_TARGET || sh(}}
{{    returnStatus: true,}}
{{    script: "git diff --name-only origin/${env.CHANGE_TARGET}...${env.GIT_COMMIT} | grep ^${module}/"}}
{{  ) == 0}}
{{}}}

and then using:

 

{ {when { code } }
when {
expression { }}
{{    return hasChangesIn('my-dir')
} }
 
  }
{
{ code } }}

 

in my pipeline stages

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:57:01 PM11/14/18
to jenkinsc...@googlegroups.com
Michael Gauthier edited a comment on Bug JENKINS-54285
I was able to work around this issue by defining a function for my pipeline:

{ { code}
def boolean hasChangesIn(String module) { }}
{{  return !env.CHANGE_TARGET || sh( }}
{{    returnStatus: true, }}
{{    script: "git diff --name-only origin/${env.CHANGE_TARGET}...${env.GIT_COMMIT} | grep ^${module}/" }}
{{  ) == 0
} }

{
{ code } }}

and then using:

 

{code}

when {
  expression {
    return hasChangesIn('my-dir')
  }
}
{code}

 

in my pipeline stages

mike@silverorange.com (JIRA)

unread,
Nov 14, 2018, 5:57:02 PM11/14/18
to jenkinsc...@googlegroups.com

mike@silverorange.com (JIRA)

unread,
Nov 27, 2018, 1:32:01 PM11/27/18
to jenkinsc...@googlegroups.com

After a bit more testing, there's an improved version of the hasChangesIn that works for our monorepo:

def boolean hasChangesIn(String module) {
    if (env.CHANGE_TARGET == null) {
        return true;
    }

    def MASTER = sh(
        returnStdout: true,
        script: "git rev-parse origin/${env.CHANGE_TARGET}"
    ).trim()

    // Gets commit hash of HEAD commit. Jenkins will try to merge master into
    // HEAD before running checks. If this is a fast-forward merge, HEAD does
    // not change. If it is not a fast-forward merge, a new commit becomes HEAD
    // so we check for the non-master parent commit hash to get the original
    // HEAD. Jenkins does not save this hash in an environment variable.
    def HEAD = sh(
        returnStdout: true,
        script: "git show -s --no-abbrev-commit --pretty=format:%P%n%H%n HEAD | tr ' ' '\n' | grep -v ${MASTER} | head -n 1"
    ).trim()

    return sh(
        returnStatus: true,
        script: "git diff --name-only ${MASTER}...${HEAD} | grep ^${module}/"
    ) == 0
}

anna.m.tikhonova@gmail.com (JIRA)

unread,
Dec 3, 2019, 8:41:03 AM12/3/19
to jenkinsc...@googlegroups.com

I wanted to use 'when { changeset ... }' to test changes in my monorepo as well (I'm using Bitbucket branch source, Bitbucket Server) and then came to find this... Why don't we have pull request changes? Webhook payload has the destination branch, so why not have the behaviour the author suggested in the description?

This message was sent by Atlassian Jira (v7.13.6#713006-sha1:cc4451f)
Atlassian logo
Reply all
Reply to author
Forward
0 new messages