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

Assign variable from shell output using declarative pipeline

      Currently, we can capture the output of a shell step inside of a script block, but have no way to do that in clean declarative pipeline syntax. For example, this post on StackOverflow must instead be:

      // Git committer email
      script {
        GIT_COMMIT_EMAIL = sh (
          script: 'git --no-pager show -s --format=\'%ae\'',
          returnStdout: true
        ).trim()
      }
      echo "Git committer email: ${GIT_COMMIT_EMAIL}"
      

      It also doesn't work to wrap it in a declarative environment block, though that may perhaps be a reasonable place to extend the syntax.

          [JENKINS-55771] Assign variable from shell output using declarative pipeline

          Bryce Schober created issue -
          Bryce Schober made changes -
          Description New: Currently, we can capture the output of a shell step inside of a {{script{}}} block, but have no way to do that in clean declarative pipeline syntax. For example, [this post on StackOverflow|https://stackoverflow.com/a/38783622] must instead
          Bryce Schober made changes -
          Component/s New: workflow-durable-task-step-plugin [ 21715 ]
          Component/s Original: pipeline-build-step-plugin [ 21707 ]
          Component/s Original: pipeline-stage-step-plugin [ 21709 ]
          Bryce Schober made changes -
          Description Original: Currently, we can capture the output of a shell step inside of a {{script{}}} block, but have no way to do that in clean declarative pipeline syntax. For example, [this post on StackOverflow|https://stackoverflow.com/a/38783622] must instead New: Currently, we can capture the output of a shell step inside of a {{script{}}} block, but have no way to do that in clean declarative pipeline syntax. For example, [this post on StackOverflow|https://stackoverflow.com/a/38783622] must instead be:

          {code:groovy}
          // Git committer email
          script {
            GIT_COMMIT_EMAIL = sh (
              script: 'git --no-pager show -s --format=\'%ae\'',
              returnStdout: true
            ).trim()
          }
          echo "Git committer email: ${GIT_COMMIT_EMAIL}"
          {code}
          Bryce Schober made changes -
          Component/s New: pipeline [ 21692 ]
          Bryce Schober made changes -
          Description Original: Currently, we can capture the output of a shell step inside of a {{script{}}} block, but have no way to do that in clean declarative pipeline syntax. For example, [this post on StackOverflow|https://stackoverflow.com/a/38783622] must instead be:

          {code:groovy}
          // Git committer email
          script {
            GIT_COMMIT_EMAIL = sh (
              script: 'git --no-pager show -s --format=\'%ae\'',
              returnStdout: true
            ).trim()
          }
          echo "Git committer email: ${GIT_COMMIT_EMAIL}"
          {code}
          New: Currently, we can capture the output of a shell step inside of a {{script}} block, but have no way to do that in clean declarative pipeline syntax. For example, [this post on StackOverflow|https://stackoverflow.com/a/38783622] must instead be:

          {code:groovy}
          // Git committer email
          script {
            GIT_COMMIT_EMAIL = sh (
              script: 'git --no-pager show -s --format=\'%ae\'',
              returnStdout: true
            ).trim()
          }
          echo "Git committer email: ${GIT_COMMIT_EMAIL}"
          {code}

          It also doesn't work to wrap it in a declarative {{environment}} block, though that may perhaps be a reasonable place to extend the syntax.
          Bryce Schober made changes -
          Assignee New: Devin Nusbaum [ dnusbaum ]
          Devin Nusbaum made changes -
          Component/s New: pipeline-model-definition-plugin [ 21706 ]
          Component/s Original: pipeline [ 21692 ]
          Component/s Original: workflow-durable-task-step-plugin [ 21715 ]
          Devin Nusbaum made changes -
          Labels New: pipeline

          Devin Nusbaum added a comment - - edited

          There might be an existing issue to track this kind of thing already, but IIUC it is somewhat by design (CC abayer). In your example at least you could move your echo into the script you run, but perhaps that is not your real use case.

          Devin Nusbaum added a comment - - edited There might be an existing issue to track this kind of thing already, but IIUC it is somewhat by design (CC abayer ). In your example at least you could move your echo into the script you run, but perhaps that is not your real use case.

            Unassigned Unassigned
            bryceman Bryce Schober
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: