Home > database >  How to fail Azure devops pipeline task specifically for failures in build-steps.yaml file - bash
How to fail Azure devops pipeline task specifically for failures in build-steps.yaml file - bash

Time:01-06

enter image description here although terraform validate has an error the build didn't stop , and here is the part in build-steps.yaml:

 - task: TerraformCLI@0
    displayName: "Run Terraform fmt"
    inputs:
      command: 'fmt'
      commandOptions: '-check -recursive'
      allowTelemetryCollection: false

  - bash: |
     find . -type f \
       -name "*.tf" | \
       xargs -I % dirname % | \
       sort -u | \
       xargs -I ? bash -c \
         '(cd ? && terraform init -backend=false && terraform validate ; rm -rf .terraform)' \ 

    displayName: "Run Terraform validate" 

I've tried continueOnError: false, but it didn't work.

- task: TerraformCLI@0
    displayName: "Run Terraform fmt"
    inputs:
      command: 'fmt'
      commandOptions: '-check -recursive'
      allowTelemetryCollection: false

 - bash: |
    find . -type f \
      -name "*.tf" | \
      xargs -I % dirname % | \
      sort -u | \
      xargs -I ? bash -c \
        '(cd ? && terraform init -backend=false && terraform validate ; rm -rf .terraform)' \ 

   displayName: "Run Terraform validate" 
   continueOnError: false

CodePudding user response:

Firstly, you can use the TerraformCLl task to run terraform validate.

- task: TerraformCLI@0
   displayName: "Run Terraform fmt"
   inputs:
     command: 'validate'
     commandOptions: 'xxxx'
     allowTelemetryCollection: false

Can I stop the build while using the bash?

You can try to enable the option "Fail on Standard Error" for the bash task. If this is true, this task will fail if any errors are written to the StandardError stream.

For example:

 - bash: |
    find . -type f \
      -name "*.tf" | \
      xargs -I % dirname % | \
      sort -u | \
      xargs -I ? bash -c \
        '(cd ? && terraform init -backend=false && terraform validate ; rm -rf .terraform)' \ 

   displayName: "Run Terraform validate" 
   failOnStderr: true

Alternately, use set -e inside the script:

 - bash: |
    set -e
    find . -type f \
      -name "*.tf" | \
      xargs -I % dirname % | \
      sort -u | \
      xargs -I ? bash -c \
        '(cd ? && terraform init -backend=false && terraform validate ; rm -rf .terraform)' \ 

   displayName: "Run Terraform validate" 
  • Related