Azure Devops中是否有$(SourceVersion)的短7位数字版本?

时间:2019-07-10 15:04:16

标签: azure-devops yaml

我正在尝试将内部版本名称设置为...

$(BuildDefinitionName)_$(versionMajor).$(versionMinor).$(versionPatch)+$(SourceBranchName).$(SourceVersion),例如 OurBigLibraryCI_1.2.3+master.10bbc577

但是,我找不到任何包含提交哈希的“短”(7位)版本的预定义变量。 $(SourceVersion)保存完整的SHA-1哈希。

如何在基于Yaml的管道中缩短呢?

4 个答案:

答案 0 :(得分:4)

您可以通过反引号使用传统的命令替换来获取短git哈希(SHA-1),假设代码已在$(Build.SourcesDirectory)中检出:

  - bash: |
      short_hash=`git rev-parse --short=7 HEAD`  ## At least 7 digits, more if needed for uniqueness
      echo ""
      echo "Full git hash:  $(Build.SourceVersion)"
      echo "Short git hash: $short_hash"
      echo "##vso[task.setvariable variable=short_hash]$short_hash"  ## Store variable for subsequent steps
    workingDirectory: $(Build.SourcesDirectory)
    displayName: Get short git hash

输出:

Full git hash:  f8d63b1aaa20cf348a9b5fc6477ac80ed23d5ca0
Short git hash: f8d63b1

然后,管道中的以下步骤可以通过变量$(short_hash)使用短哈希。

(这比手动将整个git哈希值缩减为七个字符更好,因为如果需要,它会添加额外的数字来唯一标识提交,请参见https://stackoverflow.com/a/21015031/1447415.

更新:改进版

以下改进版本检查git哈希是否匹配(完整哈希以短哈希开头),否则将失败:

  - bash: |
      short_hash=`git rev-parse --short=7 HEAD`
      echo ""
      echo "Full git hash:  $(Build.SourceVersion)"
      echo "Short git hash: $short_hash"
      echo ""
      ## Fail step if full hash does not start with short hash
      if [[ $(Build.SourceVersion) != $short_hash* ]]; then
        echo "--> Hashes do not match! Aborting."
        exit 1
      fi
      echo "--> Hashes match. Storing short hash for subsequent steps."
      ## Store variable for subsequent steps
      echo "##vso[task.setvariable variable=short_hash]$short_hash"
    workingDirectory: $(Build.SourcesDirectory)
    displayName: Get short git hash

答案 1 :(得分:3)

df_relevant_data = df.loc[(df.index.get_level_values(1) >= 78) & (df.index.get_level_values(1) <= 120), :]

for location_id, data_of_location_id in df_relevant_data.groupby("location_id"):

        for hour in range(81, 123, 3):

            top_hour_data = data_of_location_id.loc[(location_id, hour), ['temp', 'size']] # e.g. 81
            bottom_hour_data = data_of_location_id.loc[(location_id, (hour - 3)), ['temp', 'size']] # e.g. 78

            difference = top_hour_data.values - bottom_hour_data.values
            bottom_bump = difference * (1/3) # amount to add to calculate the 79th hour
            top_bump = difference * (2/3) # amount to add to calculate the 80th hour

            df.loc[(location_id, (hour - 2)), ['temp', 'size']] = bottom_hour_data.values + bottom_bump
            df.loc[(location_id, (hour - 1)), ['temp', 'size']] = bottom_hour_data.values + top_bump

以下是 vmImage的示例:“ ubuntu-latest” 。步骤:

  1. 从预定义的GitHash中拆分7个字符
  2. 将其分配给管道变量。不要混淆- script: | echo $sourceVersion commitHash=${sourceVersion:0:7} echo $commitHash echo "##vso[task.setvariable variable=commitHash]$commitHash" ## Set variable for using in other tasks. env: { sourceVersion: $(Build.SourceVersion) } displayName: Git Hash 7-digit workingDirectory: #workingDirectory - task: Docker@2 displayName: Build & Push image inputs: command: 'buildAndPush' containerRegistry: '$(myRegistry)' repository: $(myContainerRepository) Dockerfile: $(myDockerfile) buildContext: '$(myBuildContext)' tags: $(commitHash) ## The variable was defined above.
  3. 通过$(commitHash)使用它

了解更多:

  1. Assign variable in Azure pipeline
  2. Using script in Azure pipeline

答案 2 :(得分:2)

您可以使用gitversion,在运行gitversion任务后,它将在$(GitVersion.ShortSha)变量下显示shortsha。

另一方面,shortsha只是真实sha的前7个字符,因此您可以使用某种bash \ powershell脚本将长sha分成短sha

In Git, what is the difference between long and short hashes?

答案 3 :(得分:1)

  

如何在基于Yaml的管道中缩短呢?

没有可用的现成变量来获取Azure Devops中$(SourceVersion)的 7 位版本。因为display_text = ", ".join([ "<a href={}>{}</a>".format( reverse( 'admin:{}_{}_change'.format("admin", "rack"), args=(items.pk, )), items) for items in obj.freezer.rack.all() ]) if display_text: return mark_safe(display_text) 8 位版本。

因此,要解决此问题,就像@ 4c74356b41所说的那样,我们必须使用bash \ powershell脚本将长阴影分割成短阴影。

您可以查看我的以下示例以获取更多详细信息:

ShortSha

结果:

steps:

- script: |
   echo $(Build.SourceVersion)

   set  TestVar=$(Build.SourceVersion)

   set MyCustomVar= %TestVar:~0,7%

   echo %MyCustomVar%

  displayName: 'Command Line Script'

因此,我们可以得到$(SourceVersion)的7位数字版本为========================== Starting Command Output =========================== ##[command]"C:\WINDOWS\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "C:\VS2017Agent\_work\_temp\be5f6293-77d8-41b7-a537-49e3b2e7bc6c.cmd"" cb124539c4cb7f19dc8e50e1b021f93c5ffaf226 cb12453 ##[section]Finishing: Command Line Script

希望这会有所帮助。