【问题标题】:git commit and git tag in azure devops yml based pipeline基于 azure devops yml 的管道中的 git commit 和 git tag
【发布时间】:2020-07-17 00:30:45
【问题描述】:

有没有一种很好的方法来提交更改文件并在基于 azure DevOps yaml 的管道中创建标签?

我的场景将是基于节点 js 的构建: 每次构建时,都会使用npm version patch 更改 package.json 版本 最后,它将 package.json 推送到构建分支(显然条件分支==master),并将标记和推送分支。

肮脏的方式可以是:

- bash : |
     git add filename.ext
     git push origin HEAD:branchName
     git tag -a tagName -m 'tag message' 
     git push --tags
  displayName: 'Git Commit and Tag from pipeline'

【问题讨论】:

  • 你的解决方案可能是你能得到的最好的。我只是警告您正确配置构建(我的意思是触发器)或添加短语[Skip CI] 以避免循环依赖。
  • 同意。我的 CI 触发器有条件跳过如果提交来自 CI

标签: azure-devops


【解决方案1】:

您是对的,要在 azure devops 管道中提交更改并推送到源代码库,您可能必须在脚本任务中运行 git 命令。

在脚本任务中,repo 克隆 url 中的 accountName 需要替换为 $(System.AccessToken) 以进行身份​​验证(例如,https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName)。

您可以查看以下示例来标记和推送 azure 分支。

- bash: | 
        git config --global user.email "your@email.com"
        git config --global user.name "yourUsername"

        #git add filename.ext
        git add .
        git commit -m "message" 

        git push https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName HEAD:master -q

        git tag -a tagName -m 'tag message'
        git push https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName tagName 

【讨论】:

  • 太棒了!我没有太多的声誉可以给你+1,因此在评论中指定
  • 嗨@SamitKumarPatel 如果有帮助,您可以accept it as answer
【解决方案2】:

您可以在 yaml 构建步骤中执行此操作,但重要的是您需要管道具有执行推送的权限,最简单的方法是保留凭证,如下所示。

然后您需要授予Project Collection Build Service 创建标签贡献(添加/删除文件)权限。

我还发现我必须设置工作目录才能推送工作

steps:
# Check out the code and keep the token so we can tag the repository
- checkout: self
  persistCredentials: true

--- rest of pipeline

# Tag the current branch with the version number
- script: |
    git add filename.ext
    git push origin
    git tag -a tagName -m 'tag message' 
    git push --tags
  workingDirectory: $(Build.SourcesDirectory)
  displayName: 'Git Commit and Tag from pipeline'

【讨论】:

    【解决方案3】:

    Azure DevOps 现在发布了很好的文档“启用脚本以运行 Git 命令”。请看here

    1. 你必须配置
    git config --global user.email "you@example.com"
    git config --global user.name "Your Name"
    
    1. 转到项目设置 --> 选择存储库 --> 从列表中选择存储库(您要从管道提交的存储库)--> 单击权限(在顶部)--> 在搜索框中,搜索Project Collection Build Service,一旦出现在下拉列表中,选择它并设置您想要的权限(附图供您参考)

    然后你就完成了。您可以根据您在上述步骤中设置的权限进行所有您想要的 git 活动。 注意 - 请特别注意您的构建触发器配置,否则当您从存储库中的管道提交任何内容时,您的管道将一次又一次地触发......并且将变为循环。为避免这种情况,请在您的提交消息中添加 [skip ci] 以避免循环构建。

    【讨论】:

      【解决方案4】:

      对于标记,您可以使用“标签源”功能,该功能也可用于基于 yaml 的管道。 参考这个: https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml#label-sources

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-09
        • 2019-10-07
        • 1970-01-01
        • 2021-08-12
        • 2021-07-22
        • 2022-12-15
        • 2021-02-28
        • 1970-01-01
        相关资源
        最近更新 更多