如何正确使用Azure Pipeline创建条件和变量

问题描述 投票:0回答:1

我将 azure-pipelines.yaml 文件放入项目中,如下所示:

# Using Piper general purpose pipeline for Azure with pipeline optimization

trigger:
  - main

resources:
  repositories:
    - some repositories

      

extends:
  template: sap-piper-pipeline.yml@piper-pipeline-azure
  parameters:
    acceptancePostSteps:
      - task: piper@1
        displayName: Prepare-Wiremock
        inputs:
          stepName: shellExecute
          flags: '--verbose true --sources ".pipeline/prepareWiremock.sh"'
          dockerImage: 'ppiper/cf-cli:latest'
      - task: piper@1
        displayName: Execute-Integration-Tests
        inputs:
          stepName: shellExecute
          flags: '--verbose true --sources .pipeline/executeIntegrationTests.sh'
          dockerImage: 'devxci/mbtci-java17-node18:latest'
      - task: piper@1
        displayName: Deploy to Dev
        inputs:
          stepName: cloudFoundryDeploy
          flags: "--apiEndpoint some endpoint"

在我的例子中,我有一个名为 api-test-framework 的新 maven-project,作为当前 junit 测试的一部分,并且只有在特定文件夹中采用了一些更改(例如使用路径)时,我才需要将此测试的执行添加到当前管道: src/test/java/base/特定文件夹

我尝试以这种方式做到这一点(我刚刚在部署到开发任务之前添加了它):

- task: PowerShell@2
        displayName: Check for changes
        inputs:
          targetType: 'inline'
          script: |
              $changes = git diff --name-only origin/main -- folder/where/I/need/to/check/changes
              if ($changes) {
                Write-Output "##vso[task.setvariable variable=isChangesDetected]true"
              }
              else {
                Write-Output "##vso[task.setvariable variable=isChangesDetected]false"
              }
      - task: piper@1
        displayName: Execute-Prompt-Tests
        inputs:
          stepName: shellExecute
          flags: '--verbose true --sources .pipeline/execute-api-tests.sh'
          dockerImage: 'devxci/mbtci-java17-node18:latest'
          condition: and(succeeded(), eq(variables['isChangesDetected'], 'true'))

我不是 DevOps 工程师,无法检查它,但也许有人知道适合我的情况的最佳方法。 也许它可以做得更简单,例如没有第一个任务,只需要一些我可以声明的变量?

azure variables azure-devops conditional-statements pipeline
1个回答
0
投票

没有其他方法比使用'

git diff
'命令检查目录“
src/test/java/base/specificfolder
”中是否有更改更好了。 Azure Pipelines 也没有预定义的变量可用于获取已修改文件的列表。

所以,我建议您保留“

git diff
”命令。

- task: PowerShell@2
  displayName: Check for changes
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "Getting the count of commits associated with this build..."
      $prefix_url = "$(System.CollectionUri)"
      $project = "$(System.TeamProject)"
      $buildId = $(Build.BuildId)
      $url = "${prefix_url}${project}/_apis/build/builds/${buildId}/changes?includeSourceChange=true&api-version=7.0"
      $headers = @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
      $response=Invoke-RestMethod -Uri $url -Headers $headers
      $changesCount = $response.count
      Write-Host "The count of commits is $changesCount."

      Write-Host "Detecting if the commits contain modified files under the specified folder (src/test/java/base/specificfolder)..."
      $changes = git diff HEAD HEAD~$changesCount --name-only -- src/test/java/base/specificfolder
      if ($changes.count -gt 0) {
        Write-Output "##vso[task.setvariable variable=isChangesDetected]true"
      }
      else {
        Write-Output "##vso[task.setvariable variable=isChangesDetected]false"
      }

- task: piper@1
  displayName: Execute-Prompt-Tests
  condition: and(succeeded(), eq(variables['isChangesDetected'], 'true'))
  inputs:
    stepName: shellExecute
    flags: '--verbose true --sources .pipeline/execute-api-tests.sh'
    dockerImage: 'devxci/mbtci-java17-node18:latest'
© www.soinside.com 2019 - 2024. All rights reserved.