Azure 自托管代理管道上的 Git

时间:2021-04-06 15:39:10

标签: github azure-pipelines

我正在为 GitHub 网站构建 Azure 管道,以便在 Windows 自托管代理上运行。 GitHub 项目的默认分支是 develop,所有开发者都提交到这个分支。我想要一个脚本,该脚本将在测试版本时将开发合并到发布,并在生产中将发布合并到母版。

我是 git 命令的新手,我知道管道在代理上的服务帐户下运行,在代理后面,管道以某种方式模拟到另一个帐户以连接到 GitHub。

为了测试我的脚本,我以服务帐户登录服务器并运行以下命令:

REM At start, the pipeline is on the develop branch so I move to release branch
C:\Agent\_work\29\s> git checkout release
>Updating files: 100% (928/928), done.
>Previous HEAD position was a62***: *comment*
>Switched to a new branch 'release'
>Branch 'release' set up to track remote branch 'release' from 'origin'.

C:\Agent\_work\29\s>git tag "branchTests"

C:\Agent\_work\29\s>git status
>On branch release
>Your branch is up to date with 'origin/release'.
>
>nothing to commit, working tree clean

REM I understood I had to first get the release branch and then pull the develop branch over it before pushing it all back
C:\Agent\_work\29\s>git pull origin develop
fatal: could not read Username for 'https://github.com': No such file or directory

C:\Agent\_work\29\s>git push --verbose --repo=release --set-upstream 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.

我有两个问题:

  1. 我的脚本在这种情况下是否正确?
  2. 这个用户名错误可能是因为我使用了服务帐户吗?我应该以某种方式模仿管道所做的同一个帐户吗?

谢谢。

########## 更新 1

我注意到如果我不禁用管道中的结帐,我不需要添加这个

git config --global user.email "you@example.com"
git config --global user.name "xxx"
git remote set-url origin https://user:{GitHubPAT}@github.com/xxx/xxx.git

我有两个文件:

stages:
  - stage: InitRelease
    jobs:
    - job: Branch
      steps:
        - checkout: self
          clean: true
          persistCredentials: true
        - template: git-branch-source-2-target.yml@templates
          parameters:
            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
  enabled: true
  displayName: 'GIT Release'
  inputs:
      script: |
        git checkout ${{parameters.SourceBranch}}
        git pull origin 

        git checkout ${{parameters.TargetBranch}}
        git pull origin 

        git tag ${{parameters.Tag}}
        git push --tags

        git merge ${{parameters.SourceBranch}} 
        git push origin --all --verbose

谢谢!

1 个答案:

答案 0 :(得分:0)

在管道的cmd任务中运行以下命令:

git config --global user.email "you@example.com"
git config --global user.name "xxx"

git remote set-url origin https://user:{GitHubPAT}@github.com/xxx/xxx.git
git checkout develop
git pull origin
git checkout release
git merge develop
git push origin --all

enter image description here