如何在 Azure DevOps 中使用 yaml 从管道运行 git 命令

Lee*_*Lee 5 azure-devops azure-pipelines

我只想从 YAML 文件运行 Git 命令。这是我的 YAML 文件中的内容:

steps:
- checkout: self
  persistCredentials: true
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      git config user.name "my_name"
      git config user.password "my_password"
      git clone https://my_repo@dev.azure.com/the_repo_stuff
      git checkout dev
      git checkout -b newer-branch
      git commit -a -m 'new branch commit'
      git push --set-upstream origin newer-branch
Run Code Online (Sandbox Code Playgroud)

我越来越fatal: could not read Password for 'https://my_repo@dev.azure.com': terminal prompts disabled

我使用的密码是我在 Azure DevOps 的“克隆”窗口中生成的密码。

现在,我的目标只是让这个脚本创建一个分支。最终,我想传递变量并使其变得更复杂。

J.W*_*icz 7

我有点困惑为什么你需要这样的东西。

如果使用 pipeline.yaml 所在的同一存储库,您应该能够使用 git 命令,因为

- checkout: self
  persistCredentials: true
Run Code Online (Sandbox Code Playgroud)

如果您想签出不同的存储库,请考虑使用服务连接多个存储库选项来签出:

resources:
  repositories:
  - repository: MyGitHubRepo # The name used to reference this repository in the checkout step
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
  - repository: MyBitbucketRepo
    type: bitbucket
    endpoint: MyBitbucketServiceConnection
    name: MyBitbucketOrgOrUser/MyBitbucketRepo
  - repository: MyAzureReposGitRepository # In a different organization
    endpoint: MyAzureReposGitServiceConnection
    type: git
    name: OtherProject/MyAzureReposGitRepo

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- checkout: MyAzureReposGitRepository
- script: |
     git checkout -b new-branch
     git push --set-upstream origin newer-branch

- script: dir $(Build.SourcesDirectory)
Run Code Online (Sandbox Code Playgroud)

来源:多个存储库文档

另请记住,多个存储库将更改默认存储库路径:

如果您使用默认路径,添加第二个存储库签出步骤将更改第一个存储库代码的默认路径。例如,当tools是唯一的存储库时,名为tools的存储库的代码将被检出到C:\agent_work\1\s,但如果添加第二个存储库,则tools将被检出到C:\agent_work\ 1\s\工具。如果您有任何步骤依赖于原始位置中的源代码,则必须更新这些步骤。


wal*_*zzi 2

克隆 Azure 存储库时单击“生成 Git 凭据”选项后,您将看到下面的面板。

在此输入图像描述

因此,您可以使用以下脚本为此存储库创建一个分支

git clone https://username:password@dev.azure.com/organization/project/_git/repository_name
cd repository_name
git checkout dev
git checkout -b newer-branch
git commit -a -m 'new branch commit'
git push --set-upstream origin newer-branch
Run Code Online (Sandbox Code Playgroud)

  • 所以,我很高兴这对你有用,而且我毫不怀疑它对你有用,但我有点困惑*为什么*需要它 - `checkout:self` 和 `persistCredentials 的组合:true` 已经为我提供了允许脚本通过额外的步骤访问 Git 的功能? (2认同)