【问题标题】:Azure pipeline with custom variables as condition以自定义变量为条件的 Azure 管道
【发布时间】:2020-09-20 22:50:14
【问题描述】:

我们希望优化我们的管道以避免运行特定情况不需要的步骤。
我已经创建了变量shouldTriggerAnyBuild,但它似乎总是正确的(或被忽略),因为指定的步骤总是运行,即使后面的任何步骤(条件组合的位置)都没有运行。

脚本有什么问题,或者我该如何调试它?

trigger:
- master
- stage
- release/*

pool:
  vmImage: 'macOS-latest'

variables:
  shouldTriggerAnyBuild: $[ or(and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), not(startsWith(variables['Build.SourceVersionMessage'], 'release:'))), eq(variables['Build.SourceBranch'], 'refs/heads/stage'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')) ]
  
steps:
- task: UseRubyVersion@0
  inputs:
    versionSpec: '~> 2.6'

- script: echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-29;google_apis;x86'
  displayName: 'install/setup android sdkmanager'
  condition: variables.shouldTriggerAnyBuild

- script: gem install bundler
  displayName: 'gem install bundler'
  condition: variables.shouldTriggerAnyBuild

- script: bundle install
  displayName: 'bundle install'
  condition: variables.shouldTriggerAnyBuild

- script: bundle exec fastlane ciBuildDev
  displayName: 'build dev'
  condition: and(eq(variables['Build.SourceBranch'], 'refs/heads/master'),  not(startsWith(variables['Build.SourceVersionMessage'], 'release:')))

- script: bundle exec fastlane ciDeployToTest
  displayName: 'build stage'
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/stage')

- script: bundle exec fastlane ciDeployToGooglePlay
  displayName: 'build release'
  condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')
    
- task: PublishBuildArtifacts@1
  displayName: "Publish artifacts .apk"
  condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')
  inputs:
    PathtoPublish: ./app/build/outputs/apk/prod/app-prod-unsigned.apk
    ArtifactName: Prod_app_apk
        

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops

【问题讨论】:

  • 嗨 Morten,如果您在条件语句中使用 condition: eq(variables['shouldTriggerAnyBuild'], 'True'),同样的问题仍然存在?

标签: yaml azure-pipelines


【解决方案1】:

脚本有什么问题?

Conditions 写作expressionscondition: variables.shouldTriggerAnyBuild 不会生效。 相反,您可以使用condition: eq(variables['SHOULDTRIGGERANYBUILD'], 'True') 进行条件步骤。我认为这是你问题的直接原因。此外,如果您愿意,请随时使用condition: eq(variables['shouldTriggerAnyBuild'], 'True'),它也可以使用。

如何调试?

这是在必要时调试值的快速方法:

1.将第二个值改为不可能的变量,跳过该步骤时可以检查自定义变量的真实值:

2.如果你想让所有事情都清楚,那么你可以这样做:

- task: CmdLine@2
  inputs:
    script: |
      echo Hello world
  condition: eq(and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), not(startsWith(variables['Build.SourceVersionMessage'], 'release:'))),'True')

- task: CmdLine@2
  inputs:
    script: |
      echo Hello world
  condition: eq(eq(variables['Build.SourceBranch'], 'refs/heads/stage'),'True')

- task: CmdLine@2
  inputs:
    script: |
      echo Hello world
  condition: eq(startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'),'True')

由于您的变量是and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), not(startsWith(variables['Build.SourceVersionMessage'], 'release:')))eq(variables['Build.SourceBranch'], 'refs/heads/stage')startsWith(variables['Build.SourceBranch'], 'refs/heads/release/') 通过Or function 的组合,因此您可以将它们分开来调试shouldTriggerAnyBuild 如何像往常一样扩展True

这样,你可以很容易地调试它们,找出变量是如何展开的:

注意:

1.Or函数:如果任何参数为真,则评估True

2. 大多数情况下,如果先前的任务之一失败,我们会选择跳过当前任务,因此您可以考虑将succeeded() 和您的变量shouldTriggerAnyBuild 结合起来。示例here

【讨论】:

    【解决方案2】:

    经过大量的试验和错误,我们发现有Build.SourceVersionMessage,这意味着我们不能将它用作变量,所以我们最终得到了如下所示的破解:

    trigger:
      - master
      - stage
      - release/*
    
    pool:
      vmImage: "macOS-latest"
    
    variables:
      # we can't use Build.SourceVersionMessage up here because it's not defined when the variables is set here
    
      isMaster: ${{eq(variables['Build.SourceBranch'], 'refs/heads/master')}}
      isStage: ${{eq(variables['Build.SourceBranch'], 'refs/heads/stage')}}
      isRelease: ${{startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')}}
      releaseMessagePrefix: "release:"
    
    jobs:
      - job:
        steps:
          - task: UseRubyVersion@0
            displayName: "set ruby version"
            inputs:
              versionSpec: "~> 2.6"
    
          - task: Bash@3
            displayName: "set commitMessage variable"
            inputs:
              targetType: inline
              script: echo '##vso[task.setvariable variable=commitMessage]$(Build.SourceVersionMessage)'
    
          - script: echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-29;google_apis;x86'
            displayName: "install/setup android sdkmanager"
            condition: or(and(variables.isMaster, not(startsWith(variables.commitMessage, variables.releaseMessagePrefix))), ${{variables.isStage}}, ${{variables.isRelease}})
    
          - script: gem install bundler
            displayName: "gem install bundler"
            condition: or(and(variables.isMaster, not(startsWith(variables.commitMessage, variables.releaseMessagePrefix))), ${{variables.isStage}}, ${{variables.isRelease}})
    
          - script: bundle install
            displayName: "bundle install"
            condition: or(and(variables.isMaster, not(startsWith(variables.commitMessage, variables.releaseMessagePrefix))), ${{variables.isStage}}, ${{variables.isRelease}})
    
          - script: bundle exec fastlane ciBuildDev
            displayName: "Build: dev"
            condition: and(variables.isMaster, not(startsWith(variables.commitMessage, variables.releaseMessagePrefix)))
    
          - script: bundle exec fastlane ciDeployToTest
            displayName: "Build: stage"
            condition: ${{variables.isStage}}
    
          - script: bundle exec fastlane ciDeployToGooglePlay
            displayName: "Build: release"
            condition: ${{variables.isRelease}}
    
          - task: PublishBuildArtifacts@1
            displayName: "Publish: .apk"
            condition: ${{variables.isRelease}}
            inputs:
              PathtoPublish: ./app/build/outputs/apk/prod/app-prod-unsigned.apk
              ArtifactName: Prod_app_apk
    

    【讨论】:

      猜你喜欢
      • 2022-08-07
      • 2020-09-08
      • 2021-12-07
      • 2021-10-12
      • 2020-06-16
      • 2020-08-11
      • 2020-11-03
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多