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

p4-plugin + global pipeline shared library - no ${JOB_NAME} expansion in view

      similar to JENKINS-50393 but as a global declarative shared library

      I have a pipelines-lib/vars/basicDeclarative.groovy file with contents like:

      def call(body) {
          def config = [:]
          body.resolveStrategy = Closure.DELEGATE_FIRST
          body.delegate = config
          body()
          pipeline {
              ...
              stages {
                  stage("Checkout") {
                      steps {
                          checkout perforce(
                              ...
                              workspace: manualSpec(
                                  ...
                                  name: 'jenkins-${JOB_NAME}',
                                  ...
                                  spec: clientSpec(
                                      view: config.p4view
                                      ...
                                  )
                              ...
                              )
                          ...
                          )
                     }
                 }
             }
         }
      }

      that when called with a Jenkinsfile script like:

      #!groovy
      @Library('pipelines-lib') _
      basicDeclarative {
          p4view = """\
          //depot/foo/bar/... //jenkins-${JOB_NAME}/...
          """
      }

      results in errors like:

      ... p4 client -o jenkins-pipelines-lib-test +
      ... p4 client -i +
      P4: Unable to setup workspace: com.perforce.p4java.exception.RequestException: Error in client specification.
      Mapping '//jenkins-null/...' is not under '//jenkins-pipelines-lib-test/...'.

      note the workspace name is expanded properly but the view mapping is not

       

      many edits to try and fix formatting - preformatted/monospace does not work well with curly braces

          [JENKINS-52487] p4-plugin + global pipeline shared library - no ${JOB_NAME} expansion in view

          Paul Allen added a comment -

          Have you tried using single quotes for the workspace view as double quotes are expanded by Groovy and not the plugin.

          e.g.

           p4view = '''\
              //depot/foo/bar/... //jenkins-${JOB_NAME}/...
           '''

          Paul Allen added a comment - Have you tried using single quotes for the workspace view as double quotes are expanded by Groovy and not the plugin. e.g. p4view = '''\     //depot/foo/bar/... //jenkins-${JOB_NAME}/...  '''

          Daniel Hsueh added a comment -

          thanks for looking into this, and acknowledge the gstring/string difference may be the cause

          I'll confirm shortly

          (I have a nagging doubt/thought involving $BUILD_NUMBER but I cannot recall exactly what... I'll do some experimentation)

           

          Daniel Hsueh added a comment - thanks for looking into this, and acknowledge the gstring/string difference may be the cause I'll confirm shortly (I have a nagging doubt/thought involving $BUILD_NUMBER but I cannot recall exactly what... I'll do some experimentation)  

          Daniel Hsueh added a comment -

          my observations are (note gstring/string differences):

          workspace manualSpec name

          both "${env.JOB_NAME}" and '${JOB_NAME}' work *** but see below

          workspace manualSpec spec clientSpec view

          "${env.JOB_NAME}" results in java.lang.NullPointerException: Cannot get property 'JOB_NAME' on null object (likely env)

          '${JOB_NAME}' works with both *** above

           

          So my original problem (on me) was with the view mapping (which needs to be vanilla string).

          Not sure if you want to address the gstring view null env issue.

          Thank you for assisting me.

           

          Daniel Hsueh added a comment - my observations are (note gstring/string differences): workspace manualSpec name both "${env.JOB_NAME}" and '${JOB_NAME}' work *** but see below workspace manualSpec spec clientSpec view "${env.JOB_NAME}" results in java.lang.NullPointerException: Cannot get property 'JOB_NAME' on null object (likely env ) '${JOB_NAME}' works with both *** above   So my original problem (on me) was with the view mapping (which needs to be vanilla string). Not sure if you want to address the gstring view null env issue. Thank you for assisting me.  

          Paul Allen added a comment -

          Unfortunately I have no control over env as it is set by Jenkins.  Scope is important as certain variable are not defined until the Job starts to run.  For example inside a Library or Jenkinsfile many variables are not yet defined e.g. which slave (NODE_NAME) is going to be used.

          I'll close the issue and add a note to the docs regarding the use of single/double quotes. 

          Please let me know if you have any other concerns.

          Paul Allen added a comment - Unfortunately I have no control over env as it is set by Jenkins.  Scope is important as certain variable are not defined until the Job starts to run.  For example inside a Library or Jenkinsfile many variables are not yet defined e.g. which slave ( NODE_NAME ) is going to be used. I'll close the issue and add a note to the docs regarding the use of single/double quotes.  Please let me know if you have any other concerns.

          Daniel Hsueh added a comment -

          my tests on the workspace name suggest env is available there

          Daniel Hsueh added a comment - my tests on the workspace name suggest env is available there

          Paul Allen added a comment -

          Unable to resolve the issue as the environment is managed by Jenkins.  Documented using single quotes for Strings containing environment variables. 

          Paul Allen added a comment - Unable to resolve the issue as the environment is managed by Jenkins.  Documented using single quotes for Strings containing environment variables. 

            p4kevin Kevin Williamson
            dchsueh Daniel Hsueh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: