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

beside GIT_BRANCH also expose it as BRANCH

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      jenkins declarative pipeline has a nice  condition to check for branch names:

      stage('build Snapshot') {
          when { not { branch 'master' } }      
          steps {
               sh 'mvn clean install -Dmaven.test.failure.ignore'
          }
      }

      unfortunate, it only checks for the branch name in `env.BRANCH`. Sure, I can use something like this:

      when {
        expression { return env.GIT_BRANCH == "master" }
      }

      but that's not as nice as the former...

      The other solution for this would be, the pipeline-model-definition-plugin would check for multiple variables (BRANCH_NAME, BRANCH, GIT_BRANCH) and maybe throw an error in case they define different values (except for null).

      This does not sound like a big issue, but for sure it will make a lot of things easier to understand, e.g. why would the condition behave different in a multibranch job then in a normal job?

      please also see JENKINS-50622 

        Attachments

          Issue Links

            Activity

            Hide
            markewaite Mark Waite added a comment -

            Dominik Bartholdi the multi-branch Pipeline already works as you describe, at least according to JENKINS-50622. If Pipeline (without multi-branch) needs to work that way, it is a change to another plugin, not the git plugin. I assigned this to the model definition plugin in hopes that is the correct location.

            I'm unwilling to add the "BRANCH" environment variable to the git plugin because that risks confusion for other plugins (like Pipeline) which operate with that environment variable. I don't want to manage precedence rules (when does the git plugin assignment of BRANCH override the Pipeline assignment of branch). The git plugin records the branch name in GIT_BRANCH. Other plugins may use different environment variable names for branches.

            Show
            markewaite Mark Waite added a comment - Dominik Bartholdi the multi-branch Pipeline already works as you describe, at least according to JENKINS-50622 . If Pipeline (without multi-branch) needs to work that way, it is a change to another plugin, not the git plugin. I assigned this to the model definition plugin in hopes that is the correct location. I'm unwilling to add the "BRANCH" environment variable to the git plugin because that risks confusion for other plugins (like Pipeline) which operate with that environment variable. I don't want to manage precedence rules (when does the git plugin assignment of BRANCH override the Pipeline assignment of branch). The git plugin records the branch name in GIT_BRANCH. Other plugins may use different environment variable names for branches.
            Hide
            imod Dominik Bartholdi added a comment -

            Mark Waite the fact that I can not run the pipeline as shown in JENKINS-50622 without multi-branch shows that it does not the way I described it. I would expect to be able to run the same pipeline in a normal pipeline job and in multi-branch, but thats not working.

            Show
            imod Dominik Bartholdi added a comment - Mark Waite the fact that I can not run the pipeline as shown in  JENKINS-50622  without multi-branch shows that it does not the way I described it. I would expect to be able to run the same pipeline in a normal pipeline job and in multi-branch, but thats not working.
            Hide
            abayer Andrew Bayer added a comment -

            It's worth considering adding some backup heuristics in Declarative - i.e., if env.BRANCH is undefined, check env.GIT_BRANCH, etc... Will need to think about this.

            Show
            abayer Andrew Bayer added a comment - It's worth considering adding some backup heuristics in Declarative - i.e., if env.BRANCH is undefined, check env.GIT_BRANCH , etc... Will need to think about this.
            Hide
            abayer Andrew Bayer added a comment -

            PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/265 adding fallback to GIT_LOCAL_BRANCH (which doesn't have the remote on it, unlike GIT_BRANCH) if BRANCH_NAME is null.

            Show
            abayer Andrew Bayer added a comment - PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/265 adding fallback to GIT_LOCAL_BRANCH (which doesn't have the remote on it, unlike GIT_BRANCH ) if BRANCH_NAME is null.
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditionalScript.groovy
            pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java
            pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java
            pipeline-model-definition/src/test/resources/whenBranchNotMultibranch.groovy
            pom.xml
            http://jenkins-ci.org/commit/pipeline-model-definition-plugin/7fb4f69e3cf4fb9bfcd5addd10f96febd1d0e4fc
            Log:
            [FIXED JENKINS-50815] Fallback to GIT_LOCAL_BRANCH for when branch

            If we're not on multibranch (meaning BRANCH_NAME is null), fallback to
            GIT_LOCAL_BRANCH for when branch condition. We can theoretically add
            other SCMs here too if needed. We're using GIT_LOCAL_BRANCH because it
            most closely matches BRANCH_NAME - i.e., GIT_BRANCH includes the
            remote, GIT_LOCAL_BRANCH does not.

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditionalScript.groovy pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/WhenStageTest.java pipeline-model-definition/src/test/resources/whenBranchNotMultibranch.groovy pom.xml http://jenkins-ci.org/commit/pipeline-model-definition-plugin/7fb4f69e3cf4fb9bfcd5addd10f96febd1d0e4fc Log: [FIXED JENKINS-50815] Fallback to GIT_LOCAL_BRANCH for when branch If we're not on multibranch (meaning BRANCH_NAME is null), fallback to GIT_LOCAL_BRANCH for when branch condition. We can theoretically add other SCMs here too if needed. We're using GIT_LOCAL_BRANCH because it most closely matches BRANCH_NAME - i.e., GIT_BRANCH includes the remote, GIT_LOCAL_BRANCH does not.
            Hide
            abayer Andrew Bayer added a comment -

            Merged, will be in 1.2.10.

            Show
            abayer Andrew Bayer added a comment - Merged, will be in 1.2.10.

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                imod Dominik Bartholdi
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: