• Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • None
    • Jenkins: 2.479.3
      Bitbucket Branch Source plugin: 934.3.2

      As part of JENKINS-53535 four new environment variables were added about the Bitbucket source (URL, owner, project, and repo). When I tested this three weeks ago on the master branch these environment variables were present. Since then I switched to the latest release of the plugin but now the environment variables are no longer available in my builds:


      • BITBUCKET_SERVER_URL

      • BITBUCKET_PROJECT_KEY

      • BITBUCKET_REPOSITORY

      • BITBUCKET_OWNER

          [JENKINS-75182] Bitbucket env vars missing

          Nikolas Falco added a comment -

          Please open a config.xml of a workflow job in Jenkins master, for example in $JENKINS_HOME/jobs/<organization job name>/master/config.xml

          Should be something like:

          <flow-definition plugin="workflow-job@1476.v90f02a_225559">
            <displayName>master</displayName>
            ...
            <properties>
              <org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty plugin="workflow-multibranch@795.ve0cb_1f45ca_9a_">
                <branch plugin="branch-api@2.1206.vd9f35001c95c">
                  <scm class="hudson.plugins.git.GitSCM" plugin="git@5.7.0">
                    <extensions>
                      <com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension plugin="cloudbees-bitbucket-branch-source@934.4.0-SNAPSHOT">
                        <owner>amuniz</owner>
                        <repository>test-repo</repository>
                        <projectKey>DEF</projectKey>
                        <serverURL>https://bitbucket.org</serverURL>
                      </com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension>
                    </extensions>
                  </scm>
                </branch>
              </org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty>
            </properties>
            ...
          </flow-definition>
          

          If not than run a "Scan Organization Folder Now" and check again

          Nikolas Falco added a comment - Please open a config.xml of a workflow job in Jenkins master, for example in $JENKINS_HOME/jobs/<organization job name>/master/config.xml Should be something like: <flow-definition plugin= "workflow-job@1476.v90f02a_225559" > <displayName> master </displayName> ... <properties> <org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty plugin= "workflow-multibranch@795.ve0cb_1f45ca_9a_" > <branch plugin= "branch-api@2.1206.vd9f35001c95c" > <scm class= "hudson.plugins.git.GitSCM" plugin= "git@5.7.0" > <extensions> <com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension plugin= "cloudbees-bitbucket-branch-source@934.4.0-SNAPSHOT" > <owner> amuniz </owner> <repository> test-repo </repository> <projectKey> DEF </projectKey> <serverURL> https://bitbucket.org </serverURL> </com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension> </extensions> </scm> </branch> </org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty> </properties> ... </flow-definition> If not than run a "Scan Organization Folder Now" and check again

          nfalco yes the snippet is there for me, but still missing the env vars: 

          <flow-definition plugin="workflow-job@1498.v33a_0c6f3a_4b_4">
            <properties>
              <org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty plugin="workflow-multibranch@800.v5f0a_a_660950e">
                <branch plugin="branch-api@2.1208.vf528356feca_4">
                  <scm class="hudson.plugins.git.GitSCM" plugin="git@5.7.0">
                    <extensions>
                      <com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension plugin="cloudbees-bitbucket-branch-source@934.3.2">
                        <owner>FOO</owner>
                        <repository>foobar</repository>
                        <projectKey>FOO</projectKey>
                        <serverURL>https://bitbucket.internal</serverURL>
                      </com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension>
                    </extensions>
                  </scm>
                  <properties/>
                </branch>
              </org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty>
            </properties>
          </flow-definition> 

          Marijn van Zon added a comment - nfalco yes the snippet is there for me, but still missing the env vars:  <flow-definition plugin= "workflow-job@1498.v33a_0c6f3a_4b_4" >   <properties>     <org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty plugin= "workflow-multibranch@800.v5f0a_a_660950e" >       <branch plugin= "branch-api@2.1208.vf528356feca_4" >         <scm class= "hudson.plugins.git.GitSCM" plugin= "git@5.7.0" >           <extensions>             <com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension plugin= "cloudbees-bitbucket-branch-source@934.3.2" >               <owner> FOO </owner>               <repository> foobar </repository>               <projectKey> FOO </projectKey>               <serverURL> https://bitbucket.internal </serverURL>             </com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension>           </extensions>         </scm>         <properties/>       </branch>     </org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty>   </properties> </flow-definition>

          Nikolas Falco added a comment -

          I expect you use checkout scm pipeline step. This step invoke the git plugin and contribute the ENV var through the BitbucketEnvVarExtension.

          This is my example pipeline script and build log:
          The script is also available here

          pipeline {
              agent any
          
              stages {
                  stage('env var') {
                      steps {
                          script {
                              checkout scm
                              bat "set"
                          }
                      }
                  } 
              }
          }
          

          Build Logs:

          [Pipeline] Start of Pipeline
          [Pipeline] node
          Running on Jenkins in D:\git\bitbucket-branch-source-plugin\work\workspace\multibranch_project_PR-6
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (Declarative: Checkout SCM)
          [Pipeline] checkout
          ...
          Fetching upstream changes from https://bitbucket.org/nfalco79/test-repos.git
           > git.exe --version # timeout=10
           > git --version # 'git version 2.39.2.windows.1'
           > git.exe fetch --no-tags --force --progress -- https://bitbucket.org/nfalco79/test-repos.git +refs/heads/feature/issue-819:refs/remotes/origin/feature/issue-819 +refs/heads/master:refs/remotes/origin/master # timeout=10
          ...
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] withEnv
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (env var)
          [Pipeline] script
          [Pipeline] {
          [Pipeline] checkout
          ...
          Checking out Revision a599804b7c86e31a4c62fb6028bc790ec881e0e1 (PR-6)
           > git.exe config core.sparsecheckout # timeout=10
           > git.exe checkout -f a599804b7c86e31a4c62fb6028bc790ec881e0e1 # timeout=10
          Commit message: "Merge commit 'e43fdffe2def75053da30efa8204d0317a0b932a' into HEAD"
           > git.exe rev-list --no-walk e87089f8227afa350808f3fe9a1e3db39be294eb # timeout=10
          [Pipeline] bat
          
          D:\git\bitbucket-branch-source-plugin\work\workspace\multibranch_project_PR-6>set
          ALLUSERSPROFILE=C:\ProgramData
          BITBUCKET_OWNER=nfalco79
          BITBUCKET_PROJECT_KEY=PUB
          BITBUCKET_REPOSITORY=test-repos
          BITBUCKET_SERVER_URL=https://bitbucket.org
          BRANCH_NAME=PR-6
          BUILD_DISPLAY_NAME=#2
          BUILD_ID=2
          BUILD_NUMBER=2
          BUILD_TAG=jenkins-multibranch project-PR-6-2
          CHANGE_AUTHOR=557058:270a1f96-cd27-4013-ade6-85df2ab9820c
          CHANGE_AUTHOR_DISPLAY_NAME=Nikolas Falco
          CHANGE_BRANCH=feature/issue-819
          CHANGE_ID=6
          CHANGE_TARGET=master
          CHANGE_TITLE=Feature/issue 819
          CHANGE_URL=https://bitbucket.org/nfalco79/test-repos/pull-requests/6
          ...
          [Pipeline] }
          [Pipeline] // script
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // withEnv
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          

          Please provide the pipeline you are using to checkout and the relative build logs or better execute the same pipeline in your environment and post build logs?

          Nikolas Falco added a comment - I expect you use checkout scm pipeline step. This step invoke the git plugin and contribute the ENV var through the BitbucketEnvVarExtension . This is my example pipeline script and build log: The script is also available here pipeline { agent any stages { stage( 'env var ' ) { steps { script { checkout scm bat "set" } } } } } Build Logs: [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in D:\git\bitbucket-branch-source-plugin\work\workspace\multibranch_project_PR-6 [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Checkout SCM) [Pipeline] checkout ... Fetching upstream changes from https: //bitbucket.org/nfalco79/test-repos.git > git.exe --version # timeout=10 > git --version # 'git version 2.39.2.windows.1' > git.exe fetch --no-tags --force --progress -- https: //bitbucket.org/nfalco79/test-repos.git +refs/heads/feature/issue-819:refs/remotes/origin/feature/issue-819 +refs/heads/master:refs/remotes/origin/master # timeout=10 ... [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (env var ) [Pipeline] script [Pipeline] { [Pipeline] checkout ... Checking out Revision a599804b7c86e31a4c62fb6028bc790ec881e0e1 (PR-6) > git.exe config core.sparsecheckout # timeout=10 > git.exe checkout -f a599804b7c86e31a4c62fb6028bc790ec881e0e1 # timeout=10 Commit message: "Merge commit 'e43fdffe2def75053da30efa8204d0317a0b932a' into HEAD" > git.exe rev-list --no-walk e87089f8227afa350808f3fe9a1e3db39be294eb # timeout=10 [Pipeline] bat D:\git\bitbucket-branch-source-plugin\work\workspace\multibranch_project_PR-6>set ALLUSERSPROFILE=C:\ProgramData BITBUCKET_OWNER=nfalco79 BITBUCKET_PROJECT_KEY=PUB BITBUCKET_REPOSITORY=test-repos BITBUCKET_SERVER_URL=https: //bitbucket.org BRANCH_NAME=PR-6 BUILD_DISPLAY_NAME=#2 BUILD_ID=2 BUILD_NUMBER=2 BUILD_TAG=jenkins-multibranch project-PR-6-2 CHANGE_AUTHOR=557058:270a1f96-cd27-4013-ade6-85df2ab9820c CHANGE_AUTHOR_DISPLAY_NAME=Nikolas Falco CHANGE_BRANCH=feature/issue-819 CHANGE_ID=6 CHANGE_TARGET=master CHANGE_TITLE=Feature/issue 819 CHANGE_URL=https: //bitbucket.org/nfalco79/test-repos/pull-requests/6 ... [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Please provide the pipeline you are using to checkout and the relative build logs or better execute the same pipeline in your environment and post build logs?

          Just created a new project with that sample pipeline and an empty repository, and there the env vars do show up.

          I'll try this week to dig a little deeper and see if I can find out why it stopped working in my other pipelines.

          Thank you for the support!

          Marijn van Zon added a comment - Just created a new project with that sample pipeline and an empty repository, and there the env vars do show up. I'll try this week to dig a little deeper and see if I can find out why it stopped working in my other pipelines. Thank you for the support!

          Nikolas Falco added a comment - - edited

          It not an issue, because the env var are contributed by a git extension and it is called only after checkout phase. To update other pipeline you have to perform a Scan Organization Folder now to ensure each config.xml in the org folder will update own definition with the correct git extension and populated with values. This is needed also when for example you move a repository from a project to other project (I'm speaking about bb cloud). Unless you perform a scan action the git extension will remain with old stored information because the trait informations are not updated at runtime. Maybe I have to enhance user documentation

          Nikolas Falco added a comment - - edited It not an issue, because the env var are contributed by a git extension and it is called only after checkout phase. To update other pipeline you have to perform a Scan Organization Folder now to ensure each config.xml in the org folder will update own definition with the correct git extension and populated with values. This is needed also when for example you move a repository from a project to other project (I'm speaking about bb cloud). Unless you perform a scan action the git extension will remain with old stored information because the trait informations are not updated at runtime. Maybe I have to enhance user documentation

          Marijn van Zon added a comment - - edited

          nfalco All pipelines are scanned, my Jenkins is actually a clean install and it doesn't persist any state. It runs in a container without any volumes, so when Jenkins is restarted, the entire JENKINS_HOME is destroyed (which happens constantly because I'm testing this in my local environment). When it starts all pipelines are created automatically through a groovy script, and a scan is triggered for each pipeline. Note that I don't use Organization Folders, only individual Multibranch Pipeline jobs, which get scanned when they are created and through Bitbucket (Data Center) webhooks.

          I did notice that if I dump() the return of "checkout scm" the variables are there as entrySets, they are just not in the environment when I do an sh("printenv") or when I later use Script Console to dump EnvActionImpl of the build. The pipeline that worked with your sample was one that I created manually, maybe there is a difference between a manually created pipeline and one created through groovy, but I haven't had a chance yet to do a deep dive. Still intend to do that sometime soon, but not sure when I will have time. For now I'm just grabbing the information from the BitbucketSCMSource like I did before these envs were added.

          Only created this ticket to log what I came across, I'm happy with my alternative solution. Thank you for the effort on this plugin and maintaining it!

          Marijn van Zon added a comment - - edited nfalco All pipelines are scanned, my Jenkins is actually a clean install and it doesn't persist any state. It runs in a container without any volumes, so when Jenkins is restarted, the entire JENKINS_HOME is destroyed (which happens constantly because I'm testing this in my local environment). When it starts all pipelines are created automatically through a groovy script, and a scan is triggered for each pipeline. Note that I don't use Organization Folders, only individual Multibranch Pipeline jobs, which get scanned when they are created and through Bitbucket (Data Center) webhooks. I did notice that if I dump() the return of "checkout scm" the variables are there as entrySets, they are just not in the environment when I do an sh("printenv") or when I later use Script Console to dump EnvActionImpl of the build. The pipeline that worked with your sample was one that I created manually, maybe there is a difference between a manually created pipeline and one created through groovy, but I haven't had a chance yet to do a deep dive. Still intend to do that sometime soon, but not sure when I will have time. For now I'm just grabbing the information from the BitbucketSCMSource like I did before these envs were added. Only created this ticket to log what I came across, I'm happy with my alternative solution. Thank you for the effort on this plugin and maintaining it!

            nfalco Nikolas Falco
            sunmar Marijn van Zon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: