azure-pipelines-tasks-terraform
azure-pipelines-tasks-terraform copied to clipboard
##[error]Terraform command 'show' failed with exit code '1'. ##[error]SyntaxError: Unexpected end of JSON input ##[error]SyntaxError: Unexpected end of JSON input
I am getting error while running the terraform show command. getting error as below :
##[error]Terraform command 'show' failed with exit code '1'. ##[error]SyntaxError: Unexpected end of JSON input ##[error]SyntaxError: Unexpected end of JSON input
Also, when I am trying to open this file terrform.tfplan file in vscode ("terraform plan -out terraform.tfplan -detailed-exitcode") , it is giving me some binary or unsupported text encoding result.
Here is my code below:
- task: TerraformCLI@0
displayName: Terraform Plan
inputs:
command: 'plan'
workingDirectory: '$(System.DefaultWorkingDirectory)/terraformtest/aks_provision'
environmentServiceName: 'terraform-aks-svc'
providerAzureRmSubscriptionId: '01a0d38e-38ad-442e-be3f-ad1e760bd4f5'
commandOptions: '-out=$(System.DefaultWorkingDirectory)/terraform.tfplan -detailed-exitcode'
allowTelemetryCollection: false
publishPlanResults: 'PlanResults'
- task: TerraformCLI@0
displayName: Terraform show
inputs:
command: 'show'
allowTelemetryCollection: false
inputTargetPlanOrStateFilePath: '$(System.DefaultWorkingDirectory)/terraform.tfplan'
- bash: |
echo "TERRAFORM_PLAN_HAS_CHANGES==$TERRAFORM_PLAN_HAS_CHANGES"
echo "TERRAFORM_PLAN_HAS_DESTROY_CHANGES===$TERRAFORM_PLAN_HAS_DESTROY_CHANGES"
if [ "$TERRAFORM_PLAN_HAS_CHANGES" = true ] && [ "$TERRAFORM_PLAN_HAS_DESTROY_CHANGES" = false ] ; then
echo "##vso[task.setvariable variable=HAS_CHANGES_ONLY;isOutput=true]true"
echo "##vso[task.logissue type=warning]Changes with no destroys detected, it is safe for the pipeline to proceed automatically"
fi
if [ "$TERRAFORM_PLAN_HAS_CHANGES" = true ] && [ "$TERRAFORM_PLAN_HAS_DESTROY_CHANGES" = true ] ; then
echo "##vso[task.setvariable variable=HAS_DESTROY_CHANGES;isOutput=true]true"
echo "##vso[task.logissue type=warning]Changes with Destroy detected, pipeline will require a manual approval to proceed"
fi
if [ "$TERRAFORM_PLAN_HAS_CHANGES" != true ] ; then
echo "##vso[task.logissue type=warning]No changes detected, terraform apply will not run"
fi
name: "setvar"
displayName: "Vars > Set Variables for next stage"
I'm also getting the above issue. Is there any workaround on this?
Getting the same error - from what I read, it looks like the terraform.tfplan file is getting encoded and outputs gibberish data.
https://stackoverflow.com/questions/49385346/terraform-plan-output-what-is-the-encoding-being-used
Apparently, it's just how terraform protects the plan file and using this extension, i can't seem to read the JSON output of it & this is causing my pipeline to fail and not execute the terraform apply command.
Kindly suggest a fix for this.
I'm able to reproduce this issue. When I have an environment set up to test I will see what's happening. In the meantime you could add a task after the plan task which runs terraform show on the command line.
What is the status of this? :)
I ran into this, and it turned out to be a configuration/confusion issue. I had specified a value for publishPlanResults and was trying to use that file with show, and getting this error.
It turns out that file is merely the text (stdout I think) of the plan command. The file needed for show and apply is actually different and comes from the -out parameter, which produces what turns out to be a zip file.
Using the following config:
- task: TerraformCLI@1
displayName: Terraform Plan
inputs:
command: 'plan'
commandOptions: '-out=plan.tfplan'
publishPlanResults: 'plan.txt'
creates a plan.tfplan (zip format) that can be used with show and apply, and a plan.txt which just ends up showing up on the Terraform Plan tab and I think the name is completely unimportant.
Also confusingly, if you specify the same name for both, the result is the text file, but there's no other error or hint of anything being wrong.
IMHO there are two issues here:
- The
terraform showcommand has a really poor error message. It would be much easier if it just said this wasn't a valid plan file, rather than this misleading JSON error (though this is an issue in terraform itself) - The documentation in this code could be clearer. It says:
Publish Plan Results Name The name to give the published plan. If provided, the plan will be published and made available on the Terraform Plan view/tab.
Instead it could be something like:
If provided, a file with this name will be created and published on the Terraform Plan view/tab. This is not the same format as the file produced by the
-outparameter, and must use a different name if using both.
Ah thanks for catching that. I have already created #339 because, as you say, the name of that file is irrelevant. Having it is just creating a lot of confusion.
EDIT: In the initial post, though, that doesn't seem to be what's happening.