azureif-statementazure-devopsazure-pipelines-yamlbuild-pipeline

Azure pipeline conditions


I'm new to azure. I was told that I need to make a condition: "if this is a request pool, then we run it depending on the content. If this is a build, then we run everything." As I see, each task already has a condition in all of this tasks:

condition: eq(dependencies.check.outputs['Check Changes.BE '], 'true')

As you can see, the task execution depends on the BE\FE variable. Task with that variables looks like this:

- task: CheckChanges@1
              name: CheckChanges
              pool: "windows-latest" 
              inputs:
                rules: |
                  [FE]
                  **/*.json
                  **/*.ts
                  **/*.js
                  **/*.tsx
                  **/*.scss
                  **/*.html
                  **/*.css
                  
                  [BE]
                  **/*.csproj
                  **/*.cs
                  **/*.yml
                  **/*.yaml

Here is the tasks with which it needs to be done:


        - job: Restore BE
          displayName: Restore BE
          dependsOn: CheckChanges
          condition: eq(dependencies.check.outputs['CheckChanges.BE'], 'true')
          steps:          
            - task: Cake@2
              displayName: Restore FE and Sitecore modules
              inputs:
                script: "$(Build.Repository.LocalPath)/src/build.cake"
                target: "Server :: Restore"
                verbosity: "Quiet"
                Version: "1.3.0"
                
        - job: Restore FE
          displayName: Restore FE
          dependsOn: CheckChanges
          condition: eq(dependencies.check.outputs['CheckChanges.FE'], 'true')
          steps:          
            - task: Cake@2
              displayName: Restore FE and Sitecore modules
              inputs:
                script: "$(Build.Repository.LocalPath)/src/build.cake"
                target: "Client :: Restore"
                verbosity: "Quiet"
                Version: "1.3.0"

       - job: Build BE
        displayName: Build BE
        dependsOn: CheckChanges
        condition: eq(dependencies.check.outputs['CheckChanges.BE'], 'true')
        steps:
          - task: Cake@2
            displayName: Generate and Build FE and Sitecore
            inputs:
              script: "$(Build.Repository.LocalPath)/src/build.cake"
              target: "Server :: Build"
              verbosity: "Quiet"
              arguments: '--BuildConfiguration "Release" --ScSiteUrl "dummy"'
              Version: "1.3.0"
              
      - job: Build FE
        displayName: Build FE
        dependsOn: CheckChanges
        condition: eq(dependencies.check.outputs['CheckChanges.FE'], 'true')
        steps:
          - task: Cake@2
            displayName: Generate and Build FE and Sitecore
            inputs:
              script: "$(Build.Repository.LocalPath)/src/build.cake"
              target: "Client :: Build"
              verbosity: "Quiet"
              arguments: '--BuildConfiguration "Release" --ScSiteUrl "dummy"'
              Version: "1.3.0"

      - job: Unit tests BE
        displayName: Unit tests BE
        dependsOn: CheckChanges
        condition: eq(dependencies.check.outputs['CheckChanges.BE'], 'true')
        steps:
          - task: Cake@2
            displayName: Run Unit tests
            inputs:
              script: "$(Build.Repository.LocalPath)/src/build.cake"
              target: "Server :: Tests"
              verbosity: "Quiet"
              Version: "1.3.0"
                
      - job: Unit tests FE
        displayName: Unit tests FE
        dependsOn: CheckChanges
        condition: eq(dependencies.check.outputs['CheckChanges.FE'], 'true')
        steps:
          - task: Cake@2
            displayName: Run Unit tests
            inputs:
              script: "$(Build.Repository.LocalPath)/src/build.cake"
              target: "Client :: Tests"
              verbosity: "Quiet"
              Version: "1.3.0"

I don't understand what I need to do. My Team Lead just say: "If it's a Pull Request, then we run it depending on the content. If it's a build, then we run everything."


Solution

  • If it's a Pull Request, then we run it depending on the content. If it's a build, then we run everything.

    You can use the Pipeline Predefined variable: Build.Reason to confirm if the pipeline is triggered by Pull Request or other method.

    You can add the predefined variable in condition and use expression to link the filters.

    For example:

    Add condition to CheckChanges task.

    - task: CheckChanges@1
      condition: eq(variables['Build.Reason'], 'PullRequest')
    

    Add condition to other jobs:

        - job: Restore BE
          displayName: Restore BE
          dependsOn: CheckChanges
          condition:  Or(eq(dependencies.check.outputs['CheckChanges.FE'], 'true'),ne(variables['Build.Reason'], 'PullRequest'))
    
          steps:          
            - task: Cake@2
              displayName: Restore FE and Sitecore modules
              inputs:
                script: "$(Build.Repository.LocalPath)/src/build.cake"
                target: "Server :: Restore"
                verbosity: "Quiet"
                Version: "1.3.0"
                
    

    Explanation:

    When the Pipeline is triggered by Pull Request, the checkChanges task will run and the tasks will run based on the content.

    Or the pipeline will run all tasks, if the pipeline is not triggered by Pull Request.

    For more detailed info, you can refer to the doc: Condition, Expression, Predefined Variable