【问题标题】:Auto merge using git commands in Azure .yml pipeline在 Azure .yml 管道中使用 git 命令自动合并
【发布时间】:2022-12-15 12:42:12
【问题描述】:

我需要在部署完成后通过 azure 管道自动进行合并。
例如:我有一个分支 - 'release/1.0.0' 用于部署,部署后该分支需要自动合并到主分支中而无需任何拉取请求。

我正在使用 bash 任务来执行如下 git 命令:

- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/release/') }}:
  - task: Bash@3
    displayName: "Auto merge release"
    name: "Auto_merge"
    inputs:
      targetType: "inline"
      script: |
        git config --global user.name "AzureDevOps Agent"
        git config --global user.email "you@example.com" 
        git checkout origin/main
        git fetch
        git merge --ff $(Build.SourceBranchName)
        git push origin

这是天蓝色管道中的错误:

你能帮我解决这个问题吗?

注意:我不想创建任何拉取请求来处理自动合并,只是不想让 ADO 中的拉取请求混乱。
我想通过 git merge 实现这一目标,而无需任何拉取请求

【问题讨论】:

  • 看我的回答,我写了一个demo,没有PR。 :)

标签: git azure azure-devops azure-pipelines automerge


【解决方案1】:

关于git merge,有option --ff,没有-ff

使用--ff,尽可能将合并解析为快进(仅更新分支指针以匹配合并的分支;不创建合并提交)。如果不可能(当合并历史不是当前历史的后代时),创建合并提交。

确保使用正确的语法。

【讨论】:

  • 这是粘贴代码时的一个错误,我已经尝试了 --ff 和 --no-ff。
  • @krishna 然后你不应该在你的管道中的 git merge 命令上出现错误“未知开关'f'。
  • @krishna 确保键入 -(连字符减号),而不是“–”(减号),如 seen here
【解决方案2】:

Vonc 的回答已经解决了您的第一个问题。

我用相关脚本编写了一个 YAML 来实现你的要求(没有 PR):

trigger:
- none

pool:
  vmImage: ubuntu-latest


variables: #Define variables here.
 personal_access_token: xxx
 organization_name: xxx
 project_name: xxx


steps:
- checkout: self
  persistCredentials: true
  
- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/release/') }}:

  - script: echo This is release.
    displayName: 'Release Task'
  - task: Bash@3
    displayName: auto_merge
    inputs:
      targetType: 'inline'
      script: |
        git clone https://$(personal_access_token)@dev.azure.com/$(organization_name)/$(project_name)/_git/$(Build.Repository.Name)
        cd $(Build.Repository.Name)
        str=$(Build.SourceBranch)
        #get the value after the "refs/heads/" string
        branch_name=${str#refs/heads/}
        echo $branch_name
        merge_branch="origin/"$branch_name
        echo $merge_branch
        git fetch origin main
        git merge --ff $merge_branch
        git push

我在我这边工作得很好:

【讨论】:

  • 如果您需要 PAT 安全,您可以使用经典变量定义或变量组来保护它们。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 2013-02-13
  • 2011-05-03
  • 1970-01-01
  • 2012-09-12
  • 2021-11-14
  • 2020-08-09
相关资源
最近更新 更多