Yaml 以自定义变量作为条件的Azure管道

Yaml 以自定义变量作为条件的Azure管道,yaml,azure-pipelines,Yaml,Azure Pipelines,我们希望优化我们的管道,以避免运行特定情况下不需要的步骤我创建了一个变量shouldTriggerAnyBuild,但它似乎总是为真(或被忽略),因为指定的步骤总是在运行,即使后面的步骤(条件的组合来源)都没有运行 脚本有什么问题,或者如何调试它 trigger: - master - stage - release/* pool: vmImage: 'macOS-latest' variables: shouldTriggerAnyBuild: $[ or(and(eq(varia

我们希望优化我们的管道,以避免运行特定情况下不需要的步骤
我创建了一个变量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

脚本有什么问题

都写为<代码>条件:变量。shouldTriggerAnyBuild将不生效。 相反,您可以对条件步骤使用
condition:eq(变量['SHOULDTRIGGERANYBUILD'],'True')
。我认为这是你问题的直接原因。另外,可以随意使用
condition:eq(变量['shouldTriggerAnyBuild'],'True')
如果您愿意,它也可以工作

如何调试它

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
这里有一种在必要时调试值的快速方法:

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')
由于变量是
和(eq(变量['Build.SourceBranch'],'refs/heads/master')的组合,而不是(startsWith(变量['Build.SourceVersionMessage'],'release:'))
eq(变量['Build.SourceBranch'],'refs/heads/stage')
startsWith(变量['Build.SourceBranch'],'refs/heads/release/')
via,您可以将它们分开,以调试
shouldTriggerAnyBuild
如何像往常一样展开
True

通过这种方式,您可以轻松地对其进行调试,以了解变量是如何展开的:

注意:

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

大多数情况下,如果一个以前的任务失败,我们选择跳过当前任务,因此可以考虑结合<代码> SudieDead()/<代码>和变量>代码>例子

脚本有什么问题

都写为<代码>条件:变量。shouldTriggerAnyBuild将不生效。 相反,您可以对条件步骤使用
condition:eq(变量['SHOULDTRIGGERANYBUILD'],'True')
。我认为这是你问题的直接原因。另外,可以随意使用
condition:eq(变量['shouldTriggerAnyBuild'],'True')
如果您愿意,它也可以工作

如何调试它

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
这里有一种在必要时调试值的快速方法:

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')
由于变量是
和(eq(变量['Build.SourceBranch'],'refs/heads/master')的组合,而不是(startsWith(变量['Build.SourceVersionMessage'],'release:'))
eq(变量['Build.SourceBranch'],'refs/heads/stage')
startsWith(变量['Build.SourceBranch'],'refs/heads/release/')
via,您可以将它们分开,以调试
shouldTriggerAnyBuild
如何像往常一样展开
True

通过这种方式,您可以轻松地对其进行调试,以了解变量是如何展开的:

注意:

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


大多数情况下,如果一个以前的任务失败,我们选择跳过当前任务,因此可以考虑结合<代码> SudieDead()/<代码>和变量>代码>示例。

经过多次尝试和错误,我们发现存在
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

经过大量的尝试和错误,我们发现存在
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

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