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

Git with interpolated string fails using environment variable

      Running the Jenkinsfile attached using the blueocean Jenkins plugin generates the below error message.

      I only get this issue when I use a double quoted string and inserting the Environment variable.

        1. git_error.txt
          7 kB
        2. Jenkinsfile
          0.2 kB
        3. Jenkinsfile
          0.2 kB

          [JENKINS-43685] Git with interpolated string fails using environment variable

          This isn't a project infrastructure issue, so moved it to the JENKINS project, and assigned it to the correct plugin.

          Also, I don't know whether you attached the correct Jenkinsfile. When I download the attachment I see a very bare pipeline with no reference to environment variables or git.

          Christopher Orr added a comment - This isn't a project infrastructure issue, so moved it to the JENKINS project, and assigned it to the correct plugin. Also, I don't know whether you attached the correct Jenkinsfile. When I download the attachment I see a very bare pipeline with no reference to environment variables or git.

          I just uploaded a new Jenkinsfile, which should illustrate the issue described.

          Stephen Robinson added a comment - I just uploaded a new Jenkinsfile, which should illustrate the issue described.

          Ok, the second attached Jenkinsfile makes more sense. This is happening because the variable reference is ambiguous.

          In the step git "git@example.org:user/$REPO.git", the variable name is being interpreted as REPO.git, and so Jenkins tries to access the git property of the REPO object, which isn't possible, as REPO resolves to a string.  Hence the error message you saw.

          If you use git "git@example.org:user/${REPO}.git", then it will work as expected.

          Perhaps the declarative pipeline plugin can do a better job of detecting such ambiguities, so I'll leave this open, but you should have no problems so long as you ensure that your variable references are unambiguous.

          Christopher Orr added a comment - Ok, the second attached Jenkinsfile makes more sense. This is happening because the variable reference is ambiguous. In the step git "git@example.org:user/$REPO.git" , the variable name is being interpreted as  REPO.git , and so Jenkins tries to access the git property of the REPO object, which isn't possible, as REPO resolves to a string.  Hence the error message you saw. If you use git "git@example.org:user/${REPO}.git" , then it will work as expected. Perhaps the declarative pipeline plugin can do a better job of detecting such ambiguities, so I'll leave this open, but you should have no problems so long as you ensure that your variable references are unambiguous.

          Thank you for clarifying my mistake. Would you say it better practise to always use ${} syntax instead? This seems more fool proof.

          Stephen Robinson added a comment - Thank you for clarifying my mistake. Would you say it better practise to always use ${} syntax instead? This seems more fool proof.

          > Would you say it better practise to always use ${} syntax instead?

          Yeah, that's certainly what I always do within a double-quoted string.

          The documentation gives an example with that style when discussing string interpolation, though I don't know if that style is used consistently throughout the rest of the docs:
          https://jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation

          Christopher Orr added a comment - > Would you say it better practise to always use ${} syntax instead? Yeah, that's certainly what I always do within a double-quoted string. The documentation gives an example with that style when discussing string interpolation, though I don't know if that style is used consistently throughout the rest of the docs: https://jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation

          Liam Newman added a comment -

          Bulk closing resolved issues.

          Liam Newman added a comment - Bulk closing resolved issues.

            abayer Andrew Bayer
            stevorobs3 Stephen Robinson
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: