Search code examples
azure-devopsterraform

Verification that Azure Yaml build pipeline Terraform graph command is correct


Can anyone verify this build step is correct Specifically the customCommand attribute? - task: TerraformTaskV4@4 displayName: 'Generate Terraform Graph' inputs: provider: 'azurerm' workingDirectory: '$(Pipeline.Workspace)/${{ parameters.ARTIFACT_NAME }}/${{ parameters.ENVIRONMENT }}' command: 'custom' customCommand: 'graph -type=plan | dot -Tpng > $(Build.ArtifactStagingDirectory)/graph.png' environmentServiceNameAzureRM: ${{ parameters.SERVICE_ARM_NAME }}

I am not skilled in Terraform but I am looking to output a graph providing a visual representation of the Terraform script.


Solution

  • I tried your task, it cannot recognize the command in TerraformTaskV4@4 task, screenshot below:

    enter image description here

    Instead, you can use AzureCLI@2 with the command directly, it works on my side. I published the png file for checking.

    pool:
      vmImage: Ubuntu-latest
    
    parameters:
      - name: SERVICE_ARM_NAME 
        default: ARMConn2
    
    steps:
    - task: TerraformInstaller@1
      inputs:
        terraformVersion: 'latest'
    
    - script: |
        sudo apt-get update
        sudo apt-get install -y graphviz
      displayName: 'Install Graphviz'       # install the tool to use `dot` in command below
    
    - task: AzureCLI@2
      inputs:
        azureSubscription: 'ARMConn2'
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          terraform init
          terraform graph -type=plan | dot -Tpng > $(Build.ArtifactStagingDirectory)/graph.png
        workingDirectory: '$(System.DefaultWorkingDirectory)'  # i used default working directory for testing
    
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/graph.png'
        artifact: 'test'
        publishLocation: 'pipeline'
    

    enter image description here

    enter image description here

    PS: In your script, $(Pipeline.Workspace) is predefined variables which points to /home/vsts/work/1, however the working directory is /home/vsts/work/1/s which has /s at the end. Please consider to use $(System.DefaultWorkingDirectory) if needed.