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

JOB_BASE_NAME variable showing Branch value instead of Project name

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Minor
    • Resolution: Not A Defect
    • Component/s: branch-api-plugin
    • Labels:
      None
    • Environment:
      Jenkins 2.57, workflow-multibranch 2.14
    • Similar Issues:

      Description

      As mentioned by Sverre Moe , JOB_BASE_NAME variable is showing Branch value instead of Project name, when using Multibranch pipeline.

      Same behaviour is seen being inside or outside a Folder.

      Copying the same example mentioned here:

      "BRANCH_NAME=master
      JOB_NAME=projectA/master
      JOB_BASE_NAME=master

      The value for JOB_BASE_NAME should be in this case projectA."

        Attachments

          Activity

          Hide
          ncosta Nuno Costa added a comment -

          Jesse Glick, thanks for the explanation.

          Seems that there is no variable to only capture the project name and I believe that would be usefull to have available.

          Is this correct?

          The way I found to capture this was:

          def alljob = JOB_NAME.tokenize('/') as String[]
          def proj_name = alljob[1] //to capture a simple pipeline project name inside a manually created folder
          
          Show
          ncosta Nuno Costa added a comment - Jesse Glick , thanks for the explanation. Seems that there is no variable to only capture the project name and I believe that would be usefull to have available. Is this correct? The way I found to capture this was: def alljob = JOB_NAME.tokenize( '/' ) as String [] def proj_name = alljob[1] //to capture a simple pipeline project name inside a manually created folder
          Hide
          jglick Jesse Glick added a comment -

          If by “project name” you mean “name of multibranch folder containing this branch project” then you would look for the penultimate token of JOB_NAME (it might two or more tokens).

          Show
          jglick Jesse Glick added a comment - If by “project name” you mean “name of multibranch folder containing this branch project” then you would look for the penultimate token of JOB_NAME (it might two or more tokens).
          Hide
          darwinjs Darwin Sanoy added a comment - - edited

          I have the same challenge with not having a variable that shows in the "web console" name available at all - just because I choose a multi-branch pipeline (and wish to use declarative pipeline code.)

          I am using the same Jenkinsfile for multiple defined projects where it uses this console name as the only variable to know what to do per job type.  (parallel pipelines would make things over complex).

          What makes this worse is that in a declaratively coded pipeline, I cannot recapture this variable for the entire pipeline in an "Enviornment" block because as far as I know, it does not have any parsing capabilities.

          So it forces me to resort to a regular imperative pipeline so I can use the script solution posted in this thread by Nuno Costa.

          I can understand it may be impossible to use the JOB_NAME variable to keep the web console name when doing multibranch - but could you establish another variable that holds just that name?  Then at least I could covert existing pipeline scripts to declarative.

          Show
          darwinjs Darwin Sanoy added a comment - - edited I have the same challenge with not having a variable that shows in the "web console" name available at all - just because I choose a multi-branch pipeline (and wish to use declarative pipeline code.) I am using the same Jenkinsfile for multiple defined projects where it uses this console name as the only variable to know what to do per job type.  (parallel pipelines would make things over complex). What makes this worse is that in a declaratively coded pipeline, I cannot recapture this variable for the entire pipeline in an "Enviornment" block because as far as I know, it does not have any parsing capabilities. So it forces me to resort to a regular imperative pipeline so I can use the script solution posted in this thread by Nuno Costa . I can understand it may be impossible to use the JOB_NAME variable to keep the web console name when doing multibranch - but could you establish another variable that holds just that name?  Then at least I could covert existing pipeline scripts to declarative.
          Hide
          darwinjs Darwin Sanoy added a comment - - edited

          I combined the script solution Nuno Costa posted here with information from this thread (https://stackoverflow.com/questions/43879733/jenkinsfile-declarative-pipeline-defining-dynamic-env-vars) that I can run groovy before the "pipeline" stanza for the following working solution to make the job name (as it appears in the console) available again.  It would be much better if there were just a new environment variable for this as I have to redo the parsing in any powershell scripts I use (or very painfully allow groovy substitution in the powershell script which leads to endless escaping of the powershell code):

           

          def jobnameparts = JOB_NAME.tokenize('/') as String[]
          def jobconsolename = jobnameparts[0]
          pipeline {
            agent any
            stages {
              steps {
                echo "${jobconsolename}"
                nunit testResultsPattern: "TempBuildPipelineData-${jobconsolename}-\$BUILD_NUMBER/*.xml"
              }
            }
          }
          Show
          darwinjs Darwin Sanoy added a comment - - edited I combined the script solution Nuno Costa posted here with information from this thread ( https://stackoverflow.com/questions/43879733/jenkinsfile-declarative-pipeline-defining-dynamic-env-vars)  that I can run groovy before the "pipeline" stanza for the following working solution to make the job name (as it appears in the console) available again.  It would be much better if there were just a new environment variable for this as I have to redo the parsing in any powershell scripts I use (or very painfully allow groovy substitution in the powershell script which leads to endless escaping of the powershell code):   def jobnameparts = JOB_NAME.tokenize( '/' ) as String [] def jobconsolename = jobnameparts[0] pipeline { agent any stages { steps { echo "${jobconsolename}" nunit testResultsPattern: "TempBuildPipelineData-${jobconsolename}-\$BUILD_NUMBER/*.xml" } } }
          Hide
          medianick Nick Jones added a comment -

          I've created and successfully tested a Shared Library function with the following:

          def call() {
            def jobNameParts = env.JOB_NAME.tokenize('/') as String[]
            jobNameParts.length < 2 ? env.JOB_NAME : jobNameParts[jobNameParts.length - 2]
          }
          

          This takes care of the possibility that the multibranch pipeline is nested several folders deep (per Jesse Glick's comment that this should be the penultimate – next to last – token).

          Show
          medianick Nick Jones added a comment - I've created and successfully tested a Shared Library function with the following: def call() { def jobNameParts = env.JOB_NAME.tokenize( '/' ) as String [] jobNameParts.length < 2 ? env.JOB_NAME : jobNameParts[jobNameParts.length - 2] } This takes care of the possibility that the multibranch pipeline is nested several folders deep (per Jesse Glick 's comment that this should be the penultimate – next to last – token).

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ncosta Nuno Costa
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: