-1

need to tag image with first 7 characters of commit has value in azure build pipeline. but unable to get it , missing syntax issue somewhere.

tried as below

  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        # Write your commands here

        echo "##vso[task.setvariable variable=dockertag] $(build.sourceversion) | cut -c-7)"
        echo "$dockertag"

dockertag is used as tag in docker build task in pipeline.

it does not output anything . am i missing anything or do we have any alternative way of doing it.

ramesh reddy
  • 429
  • 2
  • 5
  • 12
  • i friend, any update for this issue? Please consider [accepting it as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) If my answer below helps to resolve this issue. Also feel free to let me know if you need any further assistance. Just a reminder :) – LoLance Jun 01 '20 at 05:19

1 Answers1

0

When using ##vso[task.setvariable way to set variable in script, the variable's value should work for subsequent tasks but not current task. Try something like this:

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          # Write your commands here
          echo "##vso[task.setvariable variable=dockertag]$(Build.SourceVersion)|cut -c-7"
          echo $(Build.SourceVersion)
          echo done

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          echo $(Build.SourceVersion)
          echo $(dockertag)
          echo '**********'
          echo $dockertag
          echo '**********'

Result:

enter image description here

So if your first bash task works well to set the dockertag variable, your following steps can use the variable via format $(dockertag), not $dockertag.

Also, it should be echo "##vso[task.setvariable variable=dockertag]$(Build.SourceVersion)|cut -c-7".

LoLance
  • 25,666
  • 1
  • 39
  • 73