microsoft / azure-pipelines-terraform

Azure Pipelines tasks for installing Terraform and running Terraform commands in a build or release pipeline.
MIT License
101 stars 62 forks source link

Terraform Plan - Output Variable - Missing jsonPlanFilePath #117

Closed lupusllc closed 1 year ago

lupusllc commented 1 year ago

When doing a Terraform plan in V2 we get an output variable for jsonPlanFilePath but not for V3. Information on this variable is also missing from the readme from V3.

I tried using -out and specifying a tfstate file to output to, but the results were the same with or without -out in V3. I also tried using Terraform show, but it just printed the results to the screen.

V3: image image

V2: image image

mericstam commented 1 year ago

Hi. The instructions for V3 are incorrect and unclear. I am fixing that in the next update. The V2 version of plan did internally first the plan and then again with plan -out {filename}, then a show -json {filename}. this behavior was a bit unclear and not as clean as we wanted it. So for V3 you need to do plan with commandoptions: -out={filename} and then do show with setting outputTo 'file' and outputFormat to 'json' and commandOptions {filename}. You also need to set fileName to '{filename}.json'

the outputvariable will be: showFilePath (https://github.com/microsoft/azure-pipelines-terraform/blob/main/Tasks/TerraformTask/TerraformTaskV3/README.md#output-variables)

example:

- task: TerraformTaskV3@3
      name: plan
      displayName: 'terraform plan'
      inputs:
        commandOptions: '-out=<filename>'
        provider: 'azurerm'
        command: 'plan'
        environmentServiceNameAzureRM: '<environmentServiceConnection>'

- task: TerraformTaskV3@3
      name: show
      displayName: 'terraform show'
      inputs:
        provider: 'azurerm'
        command: 'show'
        commandOptions: '<filename>'
        outputTo: 'file'
        outputFormat: json
        fileName: '<filename>.json'
        environmentServiceNameAzureRM: '<environmentServiceConnection>'
mericstam commented 1 year ago

closing this as a valid solution. feel free re-open if you diagree

DavidJFowler commented 1 year ago

@mericstam this does not appear to work with TerraformTaskV4@4 . An output variable with name showFilePath is not generated. A normal variable with this name is set as can be seen by this extract from the pipeline log:

##[debug]set showFilePath=/home/vsts/work/1/s/planFile.json
##[debug]Processed: ##vso[task.setvariable variable=showFilePath;isOutput=false;issecret=false;]/home/vsts/work/1/s/planFile.json
##[debug]task result: Succeeded
##[debug]Processed: ##vso[task.complete result=Succeeded;]
Finishing: Terraform: Show
mericstam commented 1 year ago

@DavidJFowler please refer to a closed issue rather the posting directly in it. It will be a lot simpler for us to see a new issue the issue list. This time I will reopen it.

mericstam commented 1 year ago

no change between v3 and v4 other then updated version of the azure-pipelines-task-lib/task lib which is used for setting variables. my assumption was that they where previously automatically set as output. My guess is that azure-pipelines-task-lib/task, setVariable function has changed behavior. I will take a look

mericstam commented 1 year ago

Looks like there was a bit of mix of the setting of output variables. changed them all to output where they should be output.