【问题标题】:Conditions based on resources on yaml pipeline基于 yaml 管道上的资源的条件
【发布时间】:2020-11-02 08:54:38
【问题描述】:

我有 2 个 Git 存储库:一个包含应用程序的源代码,一个用于 DevOps 管道。

对于流水线的第二阶段,我想检查触发流水线的分支是否是master。 但问题是变量 Build.SourceBranchName 包含 DevOps 存储库的信息,而不是包含源代码的存储库(名为 hidden 的存储库)。

我没有看到任何可用于此目的的变量。

resources:
  repositories:
  - repository: 'hidden'
    type: git
    name: 'hidden/hidden'
    trigger:
    - master
    - develop

trigger: none

stages:
- stage: Build
  displayName: Build stage

  variables:
  - name: BuildConfiguration
    value: 'Release'
  - name: ApiSolution
    value: 'Company.WebApi.csproj'

  jobs:
  - job: BuildAPI
    displayName: 'Build API'

    pool:
      vmImage: 'ubuntu-latest'

    steps:
    - checkout: self
    - checkout: hidden

    - script: dir $(Build.SourcesDirectory)
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: 'env | sort'
[...] 

- stage: DeployDev
  displayName: 'Deploy dev stage'

  dependsOn: Build
  condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'master'))

  jobs:
  - deployment: DeployServiceApp
    displayName: 'Deploy system to the dev environment job'

    workspace:
      clean: all
      
    environment: 
      name: 'xyz'
      resourceType: 'VirtualMachine'

    strategy:
      runOnce:
        deploy:
          steps:
[...]

有人对此有解决方案吗?

【问题讨论】:

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


    【解决方案1】:

    请尝试通过$[ resources.repositories['other'].ref ] 获取详细信息,如here 所示

    我今天测试了它,现在它可以工作了:

    name: ref-name
    
    trigger: none
    pr: none
    
    resources:
      repositories:
        - repository: devops
          type: github
          name: kmadof/devops-templates
          endpoint: kmadof
        - repository: azure
          type: git
          name: 'DevOps Manual/azure-functions'
    
    variables:
      test: "true"
      azure.ref: $[ resources.repositories['azure'].ref ] 
      tools.ref: $[ resources.repositories['devops'].ref ] 
      isMain: $[eq(resources.repositories['devops'].ref, 'refs/heads/master')]
    
    steps:
    - checkout: self
    - checkout: devops
    - checkout: azure
    - script: env | sort
      displayName: Print env variables
    - bash: |
        echo "Tools version: $TOOLS_REF"
        echo "Tools version: $AZURE_REF"
        echo "Tools version: $ISMAIN"
    
    

    【讨论】:

    • 它仅适用于“管道”资源,我正在尝试为“存储库”资源执行此操作。我试过“resources.repository.hidden.sourceBranch”,但它不起作用
    • @Thibault 抱歉,我在添加原始回复时不知何故盲目。我编辑了它应该对你有帮助的东西。
    • 奇怪。我对其进行了测试,但获取ref 存在问题。我在developer community 上创建了一张票
    • @Thibault 如果您仍然需要这个,请再次检查。看起来它现在可以工作了:)
    • 它确实适用于这种情况。感谢您的跟进。我希望它可以与 compile if 语句一起使用,但这不起作用。我将不得不适应。还是谢谢!
    【解决方案2】:

    我们可以添加任务bash并添加脚本printenv来打印所有环境变量,然后我找到了这个变量BUILD_SOURCEBRANCH,它是完整的触发器分支名称,例如refs/heads/master

    结果:

    另外,如果你在环境变量中找不到你需要的变量,我们可以添加power shell脚本并使用这个API来检查触发分支。

    【讨论】:

    • 我可能需要所有资源的源分支的名称。不仅是启动管道的那个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2020-10-17
    • 2020-07-15
    • 2022-10-21
    • 2021-12-07
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多