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

Git with interpolated string fails using environment variable

    Details

    • Similar Issues:

      Description

      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.

        Attachments

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

          Issue Links

            Activity

            Hide
            orrc 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.

            Show
            orrc 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.
            Hide
            stevorobs3 Stephen Robinson added a comment -

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

            Show
            stevorobs3 Stephen Robinson added a comment - I just uploaded a new Jenkinsfile, which should illustrate the issue described.
            Hide
            orrc 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.

            Show
            orrc 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.
            Hide
            stevorobs3 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.

            Show
            stevorobs3 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.
            Hide
            orrc 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

            Show
            orrc 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

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                stevorobs3 Stephen Robinson
              • Votes:
                1 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: