【问题标题】:Merge git branches in PowerShell from Azure Pipelines从 Azure Pipelines 在 PowerShell 中合并 git 分支
【发布时间】:2021-02-11 18:22:50
【问题描述】:

我正在研究 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

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

知道以后,我会重用脚本,其中 source 是 release , target 是 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.

我再次尝试,但提供“仅开发”或“[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 中的默认分支始终是“开发”这一事实有关?

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

命令:“git push release”是否意味着从 Build Agent 上本地暂存的 repo 更新 github 中的发布分支?

非常感谢。

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

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

  • develop 是开发人员承诺的地方
  • release 包含当前暂存环境中的版本
  • master 持有当前生产环境中的版本

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

- 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.

【问题讨论】:

  • 在管道期间repo是否有远程设置?
  • 我对远程、来源等术语感到困惑......这是管道的第一项工作,回购被复制到代理,我没有添加任何其他内容。您需要提取日志吗?谢谢。
  • 远程是另一个 git 存储库的术语。它甚至可以在同一台机器上。 Origin 是遥控器的默认名称。通常这是在克隆 repo 时创建的。如果推/拉失败,那么管道仓库似乎无法连接到其远程
  • 我连接回办公室并从管道中取回了日志,我会将它们添加到我编辑的更新 1 中,感谢您的帮助!
  • 嗨朋友,这张票有更新吗?下面的答案是否解决了您的问题?

标签: git azure-pipelines azure-pipelines-yaml


【解决方案1】:

你在正确的轨道上。

您需要更新您的 git 命令以在分支名称前包含“origin”,并且您可能希望在运行拉取操作后推送更改,或者它们将保持在本地构建代理上暂存。

这应该可以解决问题:

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

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

【讨论】:

  • 谢谢@Max-Morrow,我觉得我快到了,但仍然受阻:(我在更新 2 上写了不同的测试和问题!谢谢。
  • 没问题。要解决错误“致命:当前分支版本没有上游分支。”,您需要将 git push 更新为 git push --set-upstream release
  • 好的,谢谢,但我不明白它的作用以及为什么它是必要的......你能解释一下吗?我尝试从 Github UI 创建 PR 以从开发合并到发布,然后昨天发布到 master 并且它工作正常,因此用户帐户确实可以访问!我现在尝试设置上游。谢谢。
猜你喜欢
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
相关资源
最近更新 更多