Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-63439

CHANGE_TARGET is not set to the target branch

      CHANGE_TARGET is intended to represent the "target branch" of a change request – i.e., the target branch that the change will be merged into if approved and submitted. Currently this gets set to the change ref. These variables were printed using sh "env".

       

      // git push gerrit HEAD:refs/for/master
      CHANGE_TITLE=53/106353/19
      CHANGE_TARGET=53/106353/19
      CHANGE_ID=C-106353/19
      CHANGE_URL=https://gerrit.server/106353
      GERRIT_BRANCH=master
      

      In this case, GERRIT_BRANCH represents the target branch, but because it's not set in CHANGE_TARGET, we cannot use the changeRequest conditional in a declarative pipeline:

      pipeline {
        stage("for master only") {
          when {
            changeRequest target: 'master'  // will not work
          }
        }
      }
      

          [JENKINS-63439] CHANGE_TARGET is not set to the target branch

          Joe Hansche added a comment -

          There are other useful conditions that could be checked with the when.changeRequest conditions, as documented here: https://www.jenkins.io/doc/book/pipeline/syntax/#when. Some of those work correctly, but it seems like branch and target might be swapped, perhaps?

          Joe Hansche added a comment - There are other useful conditions that could be checked with the when.changeRequest conditions, as documented here: https://www.jenkins.io/doc/book/pipeline/syntax/#when . Some of those work correctly, but it seems like branch and target might be swapped, perhaps?

          I cannot find any reference to CHANGE_TARGET environment variable in the code-base.

          Are you sure that you referring to the gerrit-code-review plugin and not to the gerrit-trigger plugin?

          Luca Domenico Milanesio added a comment - I cannot find any reference to CHANGE_TARGET environment variable in the code-base. Are you sure that you referring to the gerrit-code-review plugin and not to the gerrit-trigger plugin?

          Joe Hansche added a comment - - edited

          The CHANGE_TARGET environment variable is contributed by the Multibranch Pipeline plugin, not this plugin. But this plugin does not appear to set the upstream "change" info correctly, so the target doesn't get set to the actual target, and can't be used with the built-in when conditions.

          Following the link in my comment above, the built-in when block's changeRequest condition allows specifying optional attributes to further filter the change requests that will trigger a stage:

          By adding a filter attribute with parameter to the change request, the stage can be made to run only on matching change requests. Possible attributes are id, target, branch, fork, url, title, author, authorDisplayName, and authorEmail. Each of these corresponds to a CHANGE_* environment variable, for example: when { changeRequest target: 'master' }.

          Multibranch pipeline would normally allow something like:

          stage {
            when {
              changeRequest(target: "master")
            }
            steps { . . }
          }
          

          which would allow the stage to run only if it is a change request (i.e., an unmerged open code review in Gerrit), AND it is targeting the master branch.

          From the issue description, you can see that the two fields are set, but appear to be swapped. So for a change targeting the master branch, it seems like this would work:

            when {
              changeRequest(branch: "master")
            }
          

          But that isn't the right definition of "branch" and "master" in terms of Multibranch Pipeline change requests.

          In Multibranch Pipeline terms, "branch" would be the source of the change – for example, in a Github PR, it would be the forked PR branch name; and "target" would be the branch into which the change WILL merge, if it is approved and submitted. Usually that target would be "master" or "dev", for example, while branch would be some other fork branch ("fork:pr-fix-a-thing").

          In Gerrit terms, the equivalent definitions would be: "branch" is the refs/changes/* ref or the ##/#####/# pseudo-branch-name we are currently using in this plugin. While "target" should be the change's target branch (where it will merge when it's approved and submitted).

          Joe Hansche added a comment - - edited The CHANGE_TARGET  environment variable is contributed by the Multibranch Pipeline plugin, not this plugin. But this plugin does not appear to set the upstream "change" info correctly, so the target doesn't get set to the actual target, and can't be used with the built-in when conditions. Following the link in my comment above, the built-in when block's changeRequest condition allows specifying optional attributes to further filter the change requests that will trigger a stage: By adding a filter attribute with parameter to the change request, the stage can be made to run only on matching change requests. Possible attributes are id, target, branch, fork, url, title, author, authorDisplayName, and authorEmail. Each of these corresponds to a CHANGE_* environment variable, for example: when { changeRequest target: 'master' }. Multibranch pipeline would normally allow something like: stage { when { changeRequest(target: "master" ) } steps { . . } } which would allow the stage to run only if it is a change request (i.e., an unmerged open code review in Gerrit), AND it is targeting the master branch. From the issue description, you can see that the two fields are set, but appear to be swapped. So for a change targeting the master branch, it seems like this would work: when { changeRequest(branch: "master" ) } But that isn't the right definition of "branch" and "master" in terms of Multibranch Pipeline change requests. In Multibranch Pipeline terms, "branch" would be the source of the change – for example, in a Github PR, it would be the forked PR branch name; and "target" would be the branch into which the change WILL merge, if it is approved and submitted. Usually that target would be "master" or "dev", for example, while branch would be some other fork branch ("fork:pr-fix-a-thing"). In Gerrit terms, the equivalent definitions would be: "branch" is the refs/changes/* ref or the ##/#####/# pseudo-branch-name we are currently using in this plugin. While "target" should be the change's target branch (where it will merge when it's approved and submitted).

          Hi Joe, thanks for the comprehensive explanation.

          Yes, this plugin does not yet fully implement all the change info requested. It is definitely an excellent idea to invest more in that direction.

          Luca Domenico Milanesio added a comment - Hi Joe, thanks for the comprehensive explanation. Yes, this plugin does not yet fully implement all the change info requested. It is definitely an excellent idea to invest more in that direction.

          Joe Hansche added a comment - - edited

          I confused myself: I read GERRIT_BRANCH as CHANGE_BRANCH, but that's not what I wrote in the original description. So because of that, even changeRequest(branch: "master") won't work right. As I can see, there is no way to properly filter the stage by a when.changeRequest condition based on the target branch it will merge into.

          We could still handle it separately though:

          when {
            changeRequest() // is an open gerrit code review
            environment name: 'GERRIT_BRANCH', value: 'master'
          }
          

          It's just not using the built-in changeRequest() syntax

          Joe Hansche added a comment - - edited I confused myself: I read GERRIT_BRANCH as CHANGE_BRANCH, but that's not what I wrote in the original description. So because of that, even changeRequest(branch: "master") won't work right. As I can see, there is no way to properly filter the stage by a when.changeRequest condition based on the target branch it will merge into. We could still handle it separately though: when { changeRequest() // is an open gerrit code review environment name: 'GERRIT_BRANCH' , value: 'master' } It's just not using the built-in changeRequest() syntax

          The problem seems to be in ChangeSCMHead.getTarget(); compare to the ChangeRequestSCMHead.getTarget() documentation. I have no idea how easy it would be to retrieve that info from Gerrit when constructing the ChangeSCMHead.

          Kalle Niemitalo added a comment - The problem seems to be in ChangeSCMHead.getTarget() ; compare to the ChangeRequestSCMHead.getTarget() documentation . I have no idea how easy it would be to retrieve that info from Gerrit when constructing the ChangeSCMHead.

            lucamilanesio Luca Domenico Milanesio
            jhansche Joe Hansche
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: