jason-johnson / azure-pipelines-tasks-terraform

Azure Pipelines extension for Terraform
MIT License
124 stars 53 forks source link

##[error]Terraform command 'show' failed with exit code '1'. ##[error]SyntaxError: Unexpected end of JSON input ##[error]SyntaxError: Unexpected end of JSON input #275

Open shankersumit opened 2 years ago

shankersumit commented 2 years ago

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"
scorpioailabs commented 2 years ago

I'm also getting the above issue. Is there any workaround on this?

KubeKD16 commented 2 years ago

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.

jason-johnson commented 2 years ago

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.

MatiasNielsen commented 11 months ago

What is the status of this? :)

gregmac commented 11 months ago

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:

  1. The terraform show command 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)
  2. 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 -out parameter, and must use a different name if using both.

jason-johnson commented 11 months ago

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.

b0bu commented 1 month ago

I'm only getting this error when trying to output the plan as plan.json, without the redirect > the command works fine. I really need a way for this show output to be written to memory or disk.

        - task: TerraformCLI@1
          displayName: Terraform Show
          inputs:
            inputTargetPlanOrStateFilePath: '$(System.DefaultWorkingDirectory)/plan'
            command: 'show'
            commandOptions: '-json -no-color > plan.json'

the solution I have for dealing with this ideal or not is

        - task: TerraformInstaller@1
          displayName: Set Terraform Version
            terraformVersion: $(terraform_version)
        - bash: |
             terraform show -json -no-color > plan.json