首页 > 解决方案 > 如何在 Azure DevOps 中使用 yaml 从管道运行 git 命令

问题描述

我只是想从 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

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

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

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

标签: azure-devopsazure-pipelines

解决方案


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

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

- checkout: self
  persistCredentials: true

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

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)

来源:多个存储库文档

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

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


推荐阅读