从 Azure Pipelines 在 PowerShell 中合并 git 个分支

Merge git branches in PowerShell from Azure Pipelines

我在 Azure 管道上工作,运行 在 Windows 自托管代理上工作。

我们有一个手动触发的 yaml 管道,构建项目,创建工件并部署到我们的暂存环境。

目前它可以工作,它从 github 获取开发分支并按照提到的那样做。

我想更改此设置,以便将开发分支合并到发布分支,当版本达到 PROD 时,我们始终重复使用相同的发布分支。在未来的步骤中,在批准后,发布分支应该合并到主分支。 我不太了解git,我用了很多年的SourceSafe

我不知道哪种方法最好:

#1

-Download develop branch as currently
-Build and create the package and deploy
-Download release branch
-Download the develop branch over it to get the release + develop
-Push back to git

#2

-Download release branch
-Download the develop branch over it to get the release + develop
-Push back to git
-Build and create the package and deploy

第二种方法似乎最好,但随着 Azure 下载的默认开发...我想编写一个 PowerShell 函数来执行此操作,并且还可以重复使用以将版本合并到 master,我最终编写了这个命令行脚本但它不起作用:

parameters:
- name: 'SourceBranch'
  default: 'develop'
  type: string
- name: 'TargetBranch'
  default: 'release'
  type: string
- name: 'Tag'
  default: ''
  type: string

- task: CmdLine@2
  inputs:
      script: |
        git checkout ${{parameters.TargetBranch}}
        git tag ${{parameters.Tag}}
        git push --tags

        git pull ${{parameters.SourceBranch}}

我尝试将 SourceBranch 的值更改为 developorigin\develop 但我总是得到:

fatal: 'whatever I used' does not appear to be a git repository

我不确定我的代码,如果我的想法很好,所以如果你能帮助我提供一些指导,那将非常有帮助。

知道以后,我会重用脚本,源是release,目标是master! 谢谢

-- 更新 1 ---------------------- 以下是管道中默认步骤生成的日志:

##[section]Starting: Checkout [myOrg]/[myProject]@develop to s
git version 2.26.2.windows.1
##[command]git config --get remote.origin.url
##[command]git clean -ffdx
##[command]git reset --hard HEAD
HEAD is now at bd75f7d [***]
##[command]git config gc.auto 0
##[command]git config --get-all http.https://github.com/[myOrg]/[myProject].extraheader
##[command]git config --get-all http.proxy
##[command]git -c http.extraheader="AUTHORIZATION: basic ***" -c http.proxy="http://[***]:443" fetch --force --tags --prune --progress --no-recurse-submodules origin
##[command]git checkout --progress --force e99a6[***]b4bdf
Note: switching to 'e99a6[***]b4bdf'.
You are in 'detached HEAD' state...
HEAD is now at e99a6ca Update README.md
##[command]git config http.https://github.com/[myOrg]/[myProject].extraheader "AUTHORIZATION: basic ***"
##[command]git config http.proxy "http://[***]:443"
##[section]Finishing: Checkout [myOrg]/[myProject]@develop to s

然后,我的脚本创建的日志:

git checkout release
Previous HEAD position was e99a6ca Update README.md
Switched to branch 'release'
git status
On branch release
nothing to commit, working tree clean
git tag - OECD.Glue.Contacts.API_9457
git push --tags
To https://github.com/[myOrg]/[myProject]
* [new tag]         [myProject]_9457 -> [myProject]_9457
git pull origin\develop
fatal: 'origin\develop' does not appear to be a git repository
fatal: Could not read from remote repository.

我再次尝试,但提供了“only develop”或“[myOrg]/[myProject]@develop”作为源分支,但我总是得到同样的错误!

-- 更新 2 ----------------------

默认情况下,管道签出开发分支,我尝试使用它来防止这种无用的签出:

- checkout: none

但是得到了这个日志:

git status
->  On branch release
->  nothing to commit, working tree clean
git tag ${{parameters.Tag}}
git push --tags
fatal: could not read Username for 'https://github.com': terminal prompts disabled

使用下面的结帐,删除了错误,我认为它可以帮助解决我的问题或遇到此错误的任何人:

- checkout: self
  clean: true
  persistCredentials: true

按照建议,我使用了:

git push

但是我有这个错误:

-> fatal: The current branch release has no upstream branch.

我试过这个:

git push --verbose --repo=release

然后我得到:

Pushing to release
fatal: 'release' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

对我来说仍然很模糊,我真的需要先结帐开发吗? 它是否与我们在 GitHub 中的默认分支始终是 'develop' 这一事实有关?

如何判断我是否有足够的访问权限?在 GitHub 上使用相同的帐户,我可以比较:“base:release <- compare:develop”并请求 PR! 比较更改表明有 137 次提交是在开发时完成的,但不是在发布时完成的,这是正确的,而且这些分支可以自动合并。

命令:“git push release”是否表示从 Build Agent 本地暂存的回购更新 github 中的发布分支?

非常感谢。

-- 回顾----------------------

回顾一下,这个项目目前有三个分支。

我想要一个将开发合并到发布,然后从发布到母版的脚本 我有这个管道:

- name: projectName
  value: '***'

stages:
  - stage: InitRelease
    jobs:
    - job: Branch
      steps:
        - checkout: self
          clean: true
          persistCredentials: true
        - template: git-branch-source-2-target.yml@templates
          parameters:
            TargetBranch: 'release'
            Tag: '${{ variables.projectName }}_${{ variables.buildId }}'

git-branch-source-2-target.yml:

parameters:
- name: 'SourceBranch'
  default: 'develop'
  type: string

- name: 'TargetBranch'
  default: 'release'
  type: string

- name: 'Tag'
  default: ''
  type: string

steps:
- task: CmdLine@2
  inputs:
      script: |
        git checkout ${{parameters.TargetBranch}}
        git tag ${{parameters.Tag}}
        git push --tags
        git pull origin ${{parameters.SourceBranch}}
        git push --verbose --repo=${{parameters.TargetBranch}}

这是我的输出:

Switched to branch 'release'
To https://github.com/myOrg/myProject
 * [new tag]         myProject_9527 -> myProject_9527
From https://github.com/myOrg/myProject
 * branch            develop    -> FETCH_HEAD
Already up to date.
Pushing to release
fatal: 'release' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

你走在正确的轨道上。

您需要更新 git 命令以在分支名称前包含“origin”,并且您可能希望在 运行 拉动之后推送更改,否则它们会只在 Build Agent 上本地暂存。

这应该可以解决问题:

- task: CmdLine@2
  inputs:
      script: |
        git checkout ${{parameters.TargetBranch}}
        git tag ${{parameters.Tag}}
        git push --tags

        git pull origin ${{parameters.SourceBranch}}
        git push