如何在Azure管道中获取github存储库名称

use*_*953 5 azure azure-devops azure-pipelines

Azure 中是否有任何变量可以用来获取源文件夹的名称(存储库名称)。我尝试使用Build.SourcesDirectorySystem.DefaultWorkingDirectory,两者都返回/Users/runner/work/1/s。我希望得到MyProjectFolderName这是我的项目在 github 中的源目录。

Bow*_*SFT 6

如果你checkout self repo(这是默认情况),那么只要按照Daniel的建议就可以了:

$(Build.Repository.Name)

yml 文件是这样的:

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      str = "$(Build.Repository.Name)"
      
      str.split("/")
      
      #get the last name
      print(str.split("/")[-1])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果您只签出一个存储库而不签出自己,则只需使用以下 yml:

trigger:
- none

pool:
  vmImage: ubuntu-latest
resources:
  repositories:
  - repository: 222
    type: github
    name: xxx/222
    endpoint: xxx
  - repository: 333
    type: github
    name: xxx/333
    endpoint: xxx
variables:
 - name: checkoutreporef
   value: $[ resources.repositories['333'].name ]
steps:
- checkout: 333
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      str = "$(checkoutreporef)"
      
      str.split("/")
      
      #get the last name
      print(str.split("/")[-1])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果您签出两个或更多存储库,下面的 yml 将帮助您获取存储库名称:

trigger:
- none

pool:
  vmImage: ubuntu-latest
resources:
  repositories:
  - repository: 222
    type: github
    name: xxx/xxx
    endpoint: xxx
  - repository: 333
    type: github
    name: xxx/xxx
    endpoint: xxx
steps:
- checkout: 222
- checkout: 333
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      import os
      
      #get current sub folders name
      def getfoldersname():
          folders = [f for f in os.listdir('.') if os.path.isdir(f)]
          return folders
      #print each folder name
      def printfoldersname():
          for folder in getfoldersname():
              print(folder)
      
      printfoldersname()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述