azure-devopsterraformtaskpipelineshow

How to write terraform plan to a tab in azure devops pipeline summary?


Some Terraform extensions like this are providing a feature to publish Terraform plan to a tab in the pipeline run overview/summary.

Terraform task published by Microsoft DevLabs is not currently supporting this feature.

I'm trying to make use of the pipeline logging command uploadsummary as a workaround for this.

  # Write terraform show output in default format to a markdown file
  - task: TerraformTaskV4@4
    name: TerraformShow
    displayName: Terraform Show
    inputs:
      provider: 'azurerm'
      environmentServiceNameAzureRM: $(sctfbackend)
      command: 'show'
      commandOptions: 'tfplan -no-color'
      outputTo: 'file'
      outputFormat: 'default'
      fileName: '$(working_dir)/TerraformPlan.md'
      workingDirectory: $(working_dir)

  # Display plan in the pipeline build summary
  - task: Bash@3
    displayName: Show plan summary
    inputs:
      targetType: 'inline'
      workingDirectory: '$(working_dir)'
      script: |
        echo "##vso[task.uploadsummary]$(working_dir)/TerraformPlan.md"

But it reads the input file as Markdown. So the formatting of the output looks ugly in the tab. The # in the plan output will be taken as heading1 in Markdown. + will be taken as bullets for list...etc as shown below:

enter image description here

How can I fix this to get a cleaner Terraform plan tab in the pipeline summary?


Solution

  • I found the workaround.

    This snippet can be used in the pipeline to achieve the same.

      # NOTE: Some lines in the output were missing while using this task.
      # Write terraform show output in default format to a markdown file
      - task: TerraformTaskV4@4
        name: TerraformShow
        displayName: Terraform Show
        inputs:
          provider: 'azurerm'
          environmentServiceNameAzureRM: $(sctfbackend)
          command: 'show'
          commandOptions: 'tfplan -no-color'
          outputTo: 'file'
          outputFormat: 'default'
          fileName: '$(working_dir)/TerraformPlan.md'
          workingDirectory: $(working_dir)
    
      # Display plan in the pipeline build summary
      - task: Bash@3
        displayName: Show plan summary
        inputs:
          targetType: 'inline'
          workingDirectory: '$(working_dir)'
          script: |
            ls -la
            sed -i '1 i\```' TerraformPlan.md
            echo '```' >> TerraformPlan.md
            echo "##vso[task.uploadsummary]$(working_dir)/TerraformPlan.md"
    

    The first step is to get the plan file using -out. Then show the plan with the flag -no-color to avoid the coloring characters in the output.

    Write it to a markdown file.

    Enclosing the content of that file in a code block of markdown will provide much cleaner formatting.

    Then publish the file to the pipeline summary.

    enter image description here