Hi. I have a Terraform plan running as part of Azure DevOps CI/CD pipeline YAML template that runs with -detailed-exitcode CLI switch. This template has been used by many other pipelines without issue. However now I have come across some strange behaviour.
Hoping someone can assist with why this might be occurring…
The PLAN runs ok, however the pipeline fails/errors with exit code 1 as shown below.
Plan: 2 to add, 0 to change, 1 to destroy.
─────────────────────────────────────────────────────────────────────────────
Saved the plan to: myplanfile.tfplan
To perform exactly these actions, run the following command to apply:
terraform apply "myplanfile.tfplan"
Terraform plan exit code = 2
##[error]Script failed with exit code: 1
- Exit code 2 would be expected since there are changes. I have added simple
Write-Host "Terraform plan exit code = $LASTEXITCODE"at the end of Terraform Plan task in the pipeline, and this confirms exit code 2, however the pipeline still fails as per extract above. - I have enabled
TF_LOG=debug, and alsoTF_LOG=traceto further investigate, however these do not show any errors in the entire log. - Weirdly, if I add
terraform show -json $(planfile)after the plan runs, then, my write-output correctly shows exit code 2, and the plan file is displayed in json, and the entire pipeline then succeeds! Very strange. - Also if I remove the
-detailed-exitcodeswitch, then all works as expected, however we wish to leave this in for drift detection, so removing this is not viable.
My Terraform template looks like this:
# TERRAFORM PLAN TASK
parameters:
- name: azureDevOpsServiceConnectionName
type: string
steps:
- task: AzureCLI@2
displayName: 'Terraform PLAN'
inputs:
azureSubscription: ${{ parameters.azureDevOpsServiceConnectionName }}
scriptType: pscore
scriptLocation: inlineScript
workingDirectory: $(workingDirectory)
inlineScript: |
terraform plan '--var-file myvars.tfvars -out $(planfile) -detailed-exitcode'
Write-Host "Terraform plan exit code = $LASTEXITCODE"
env:
ARM_USE_OIDC: true
ARM_ADO_PIPELINE_SERVICE_CONNECTION_ID: $(service-connection-id)
Hoping someone will be able to advise/assist. Thanks