我需要用提交标记 docker 图像在 docker 构建任务中但不完整哈希只有 azure 管道中哈希的前 7 个字符

i need to tag the docker image with commit has in docker build task but not complete hash only first 7 characters of hash in azure pipelines

需要使用提交的前 7 个字符标记图像在 Azure 构建管道中具有价值。但无法获取,某处缺少语法问题。

尝试如下

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

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

docker标签在管道中的docker构建任务中用作标签。

它不输出任何东西。我是否遗漏了任何东西,或者我们是否有其他替代方法。

当使用##vso[task.setvariable方式在脚本中设置变量时,变量的值应该适用于后续任务而不是当前任务。尝试这样的事情:

    - 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 '**********'

结果:

因此,如果您的第一个 bash 任务可以很好地设置 dockertag 变量,则您的后续步骤可以通过格式 $(dockertag) 而不是 $dockertag 使用变量。

另外,应该是echo "##vso[task.setvariable variable=dockertag]$(Build.SourceVersion)|cut -c-7"