我们的应用程序将VSTS用于CI流,并且我们要求每次代码合并回master时,补丁版本应增加一个。
我们已经创建了一个shell脚本,它将提升应用程序的版本并标记repo,但现在我们正在流中寻找注入它的最佳位置
-
主提交钩子构建-这里的问题是我们用某些策略保护了主分支(必须解决所有注释、项目必须构建等)。因此,运行脚本的生成代理无法访问推送更改。
-
Pull Request build-此选项将把脚本集成到用于验证构建的构建作业中。这实际上是有效的,但当更新被推送到分支时,这会触发一个无限构建循环,因为PR会自动重建分支。总的来说,这个选项似乎更脆弱。
我很想解决选项1,但尽管尝试授予代理适当的角色,它仍然无法访问回购。我们得到以下错误:
TF402455: Pushes to this branch are not permitted; you must use a pull request to update this branch.
有没有一种标准的方法可以做到这一点,而我却没有?
若要通过CI构建更新文件版本并为新提交添加标记,可以添加PowerShell任务。详细步骤如下:
-
在构建定义中设置权限和配置
首先转到版本控制选项卡,允许以下项目用于构建服务:
- 分支创建:允许
- 贡献:允许
- 读取:继承的允许
- 标记创建:继承的允许
然后在构建定义中,在变量选项卡中将变量
system.prefergit
添加为true
,并在选项选项卡中启用允许脚本访问OAuth令牌。更多详细信息,您可以参考脚本中的Run Git命令。
-
添加powershell脚本
git -c http.extraheader="AUTHORIZATION: bearer %SYSTEM_ACCESSTOKEN%" fetch git checkout master # Get the line of the file that contain the version # split the line, and only get the version number major.minor.patch. # check the version numbers (such as if patch less than 9) and increase the version #replace the old version with new version in the file git add . git commit -m 'change the version as $newVersion' git tag $newVersion git push origin master --tags
注意:即使新版本和标签可以成功推送到远程回购,PowerShell任务也可能失败。因此,您应该使用自定义选项将PowerShell任务之后的任务设置为"即使前一个任务失败"。
您可以尝试根据上的文档更新分支策略https://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies?view=azure-devops和https://learn.microsoft.com/en-us/azure/devops/organizations/security/permissions?view=azure-devops#git存储库对象级
尝试将分支机构策略更新为"允许"推送时绕过策略">