Mat*_*tze 16 yaml azure-devops
我正在尝试将我们的构建名称设置为...
$(BuildDefinitionName)_$(versionMajor).$(versionMinor).$(versionPatch)+$(SourceBranchName).$(SourceVersion) 例如
OurBigLibraryCI_1.2.3+master.10bbc577
但是我找不到任何包含提交哈希的“短”(7 位)版本的预定义变量。$(SourceVersion)保存完整的 SHA-1 哈希值。
如何在基于 yaml 的管道中缩短它?
- 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.
Run Code Online (Sandbox Code Playgroud)
以下是vmImage 的示例: "ubuntu-latest"。步:
$(azure_pipeline_variable) with ${bash_shell_variable} or $bash_shell_variable。阅读更多:
您可以通过反引号使用传统的命令替换来获取短 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
Run Code Online (Sandbox Code Playgroud)
输出:
Full git hash: f8d63b1aaa20cf348a9b5fc6477ac80ed23d5ca0
Short git hash: f8d63b1
Run Code Online (Sandbox Code Playgroud)
然后管道中的以下步骤可以通过变量使用短哈希$(short_hash)。
(这比手动将完整的 git 哈希减少到七个字符要好,因为如果需要唯一标识提交,这将添加额外的数字,请参阅/sf/answers/1471052201/。)
以下改进版本检查 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
Run Code Online (Sandbox Code Playgroud)
如何在基于 yaml 的管道中缩短这一时间?
Azure Devops 中没有现成变量可用于获取$(SourceVersion) 的7位版本。因为ShortSha是8位版本。
所以,要解决这个问题,就像@4c74356b41所说,我们必须使用bash\powershell脚本将长sha拆分为短sha。
您可以查看我的以下示例以了解更多详细信息:
steps:
- script: |
echo $(Build.SourceVersion)
set TestVar=$(Build.SourceVersion)
set MyCustomVar= %TestVar:~0,7%
echo %MyCustomVar%
displayName: 'Command Line Script'
Run Code Online (Sandbox Code Playgroud)
结果:
========================== 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
Run Code Online (Sandbox Code Playgroud)
因此,我们可以得到 $(SourceVersion) 的 7 位版本是cb12453。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
5459 次 |
| 最近记录: |