azure-pipelines-tasks-terraform icon indicating copy to clipboard operation
azure-pipelines-tasks-terraform copied to clipboard

`terraform apply [plan]` gives an error when used together with `publishPlanResults`

Open gdespoulain opened this issue 3 years ago • 2 comments

Bug Description

Hello! :)

I'm using terraform plan -out="terraform.tfplan" in one stage, and then applying said plan using the plan text file (passed as an artifact) in another stage: terraform apply terraform.tfplan. During the plan task I use the publishPlanResults as option.

The plan is valid and shown properly in the Terraform Plan tab. However, I get this error during apply:

Error: Failed to load "terraform.tfplan" as a plan file
Error: zip: not a valid zip file

I researched a bit and found out it seems to be a problem for Terraform to process the plan after it has been used by publishPlanResults. At least it's the theory that has been discussed at the end of this thread: https://github.com/hashicorp/terraform/issues/22396 where they could also reproduce the problem.

To Reproduce

Steps to reproduce the behavior:

1. Setup pipeline as:

stages:
  - stage: Plan
    jobs:
      - job: Plan
        steps:
          - task: TerraformCLI@0
            ...
            inputs:
              command: plan
              publishPlanResults: plan.tfplan
              ...
          - publish:
              artifact: plan
              ...

  - stage: Apply
    ...
    jobs:
      - job: Apply
        ...
        steps:
          - download: ...
          - task: TerraformCLI@0
            inputs:
              command: apply
              commandOptions: '--input=false $(Pipeline.Workspace)/plan/plan.tfplan'

2. Execute pipeline

Let both stages run. The plan is saved accordingly and available in apply stage.

  1. See error

The error appears in the log. It's gone when I remove the publishPlanResults option.

Error: Failed to load "terraform.tfplan" as a plan file
Error: zip: not a valid zip file

Expected behavior

I would like to use both the publishPlanResults option and a saved plan :)

Agent Configuration

  • VM: ubuntu
  • Terraform version: 1.1.4

Thank you!

gdespoulain avatar Jan 31 '22 09:01 gdespoulain

I can confirm this issue.

rolanddb avatar Mar 28 '22 09:03 rolanddb

I have the same issue using gitlab ci !

Dranaxel avatar Mar 31 '22 06:03 Dranaxel

I can also confirm this issue. Any plan, when it will be fixed?

Morksen avatar Jan 16 '23 09:01 Morksen

Hi. I was facing same issue, but I was using publishPlanResults: tfplan (tfplan was same as my -out) After changing publishPlanResults to something random, it worked. Somewhere in documentation it states you should not use the same file as the output plan. If yuo do this, the output plan gets overwritten with text file, and it is not binary anymore, and hence it fails to apply.

So change publishPlanResults: plan.tfplan to publishPlanResults: plan

ricklahaye avatar Jan 20 '23 10:01 ricklahaye

Hi, the "publishPlanResults" is meant to be the name of the tab in Azure Devops web page which shows the plan output, not the name of the terraform plan used to store terraform data. As @ricklahaye says, naming these the same will cause issues.

Not a bug, but we can take a look if the documentation can make this situation clearer.

jason-johnson avatar Mar 22 '23 12:03 jason-johnson

Thank you! :)

gdespoulain avatar Mar 23 '23 16:03 gdespoulain

I just got bitten by this today (wasted 4 hours before finding this issue). Something in the docs would be great!

Va55ago avatar Aug 17 '23 16:08 Va55ago

Will be resolved by #339.

jason-johnson avatar Sep 11 '23 13:09 jason-johnson