0

I'm trying to generate release notes in an azure piplelines stage and push the note to an azure service bus.

How do I expose the variable in a bash script then consume it in a subsequent job in the same stage?

I'm using a bash task to execute a git command and trying to export it as an environment variable which I want to use in the following job.

  - stage: PubtoAzureServiceBus
    variables:
      COMMIT_MSG: "alsdkfgjdsfgjfd"
    jobs:
      - job: gitlog
        steps:
          - task: Bash@3
            inputs:
              targetType: 'inline'
              script: |
                # Write your commands here
                
                export COMMIT_MSG=$(git log -1 --pretty=format:"Author: %aN%n%nCommit: %H%n%nNotes:%n%n%B")
                env | grep C
      - job:
        pool: server
        dependsOn: gitlog
        steps:
          - task: PublishToAzureServiceBus@1
            inputs:
              azureSubscription: 'Slack Release Notifications'
              messageBody: |
                {
                  "channel":"XXXXXXXXXX",
                  "username":"bot",
                  "iconEmoji":"",
                  "text":":airhorn: release :airhorn: \n`$(COMMIT_MSG)`"
                }
              signPayload: false
              waitForCompletion: false

1 Answer 1

1

You need to use logging syntax and output variables like it is shown here:

trigger: none

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: A
  jobs:
  - job: A1
    steps:
     - bash: echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
     # or on Windows:
     # - script: echo ##vso[task.setvariable variable=shouldrun;isOutput=true]true
       name: printvar

- stage: B
  dependsOn: A
  jobs:
  - job: B1
    condition: in(stageDependencies.A.A1.result, 'Succeeded', 'SucceededWithIssues', 'Skipped')
    steps:
    - script: echo hello from Job B1
  - job: B2
    variables:
      varFromA: $[ stageDependencies.A.A1.outputs['printvar.shouldrun'] ]
    steps:
    - script: echo $(varFromA) # this step uses the mapped-in variable

Please take a look here to check documentation.

So you need to replace

                export COMMIT_MSG=$(git log -1 --pretty=format:"Author: %aN%n%nCommit: %H%n%nNotes:%n%n%B")

wit logging command with isOutput=true

and then map it as here

jobs:
- job: A
  steps:
  - bash: |
       echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
    name: ProduceVar  # because we're going to depend on it, we need to name the step
- job: B
  dependsOn: A
  variables:
    # map the output variable from A into this job
    varFromA: $[ dependencies.A.outputs['printvar.shouldrun']
  steps:
  - script: echo $(varFromA) # this step uses the mapped-in variable

as you want to share variable between jobs (not stages as it shown in the first example).

Sign up to request clarification or add additional context in comments.

4 Comments

is the output generated by the echo command in job A1 and saved in a variable called printvar?
Yes. But important is to have isOutput=true
where do I run the git log command? is that appended to echo "##vso..."
Before, and assign result to variable. and then use it here ##vso[task.setvariable variable=shouldrun;isOutput=true]<put variable here>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.