-1

I have an Azure Pipeline where I run a Terraform pre build and a Terraform post Build.In the middle some artefact files get deployed.(reason for the post and pre build.). The first task works and Terraform initialises. Yet the second task fails but the code is exactly the same the only difference is the working directory of Terraform is one folder more on the second task. I can't figure out why the Second task keeps failing. Please see code bellow for the yml file.

pool:
  name: 'DotNet6_Terraform'
  

resources: 
  repositories: 
  - repository: Terraform
    name: main_repo/Terraform
    path:
    - include: /Terraform
    type: git 
    ref: main #branch name
  - repository: Website
    name: second_repo/Website
    path:
    - include: /Website 
    type: git 
    ref: main #branch name
  - repository: Server
    name: third_repo/Server
    path:
    - include: /Server
    type: git 
    ref: server #branch name
  
trigger: 
  branches:
    include:
    - master

variables:
  buildConfiguration: 'Release'

stages:
- stage: run_terraform_pre_build 
  displayName: Building Terraform Applications and Deploying Web Apps
  #dependsOn: build_files
  jobs:
  - job: building_terraform_applications
    steps: 
     - checkout: Terraform
     - checkout: Website
  - template: /example/runterraform.yml@Terraform
    parameters: 
      terraformWorkingDirectory: '$(System.DefaultWorkingDirectory)/example'
      serviceConnection: 'subvalue'
      azureSubscription: 'subvalue'
      appconnectionname: 'subvalue'
      backendresourcegroupname: 'DevOpsTerraform'
      backendstorageaccountname: 'devopsterraform'
      backendcontainername: 'devopsterraformstatefile'
      RG: 'Example'
      azureLocation: 'UK South'
      terraformVersion: '1.0.4'
      artifactName: 'Website'

- stage: run_terraform_post_build 
  displayName: Apply Post Build Settings
  dependsOn: run_terraform_pre_build
  jobs:
  - job: apply_post_build_settings
    steps: 
     - checkout: Terraform 
  - template: example/PostBuild/runterraformpostbuild.yml@Terraform
    parameters: 
      terraformWorkingDirectory: '$(System.DefaultWorkingDirectory)/example/PostBuild'
      serviceConnection: 'subvalue'
      azureSubscription: 'subvalue'
      appconnectionname: 'subvalue' 
      backendresourcegroupname: 'DevOpsTerraform'
      backendstorageaccountname: 'devopsterraform'
      backendcontainername: 'devopsterraformstatefile'
      RG: 'Example'
      azureLocation: 'UK South'
      terraformVersion: '1.0.4'
      artifactName: 'Website'

This pipeline sits on a different repository to the template pipeline but its not an issue in the first task and the code is mostly the same.

This is the error I keep getting on the second task:

##[error]Error: There was an error when attempting to execute the process 'C:\azp\agent\_work\_tool\terraform\1.0.4\x64\terraform.exe'. This may indicate the process failed to start. Error: spawn C:\azp\agent\_work\_tool\terraform\1.0.4\x64\terraform.exe ENOENT

UPDATE:

My Troublesome Template YAML File:

parameters:

- name: terraformWorkingDirectory
  type: string
  default: '$(System.DefaultWorkingDirectory)/Example/PostBuild'

- name: serviceConnection
  type: string
  default: 'value'

- name: azureSubscription
  type: string
  default: 'value'

- name: appconnectionname
  type: string
  default: 'value'

- name: RG
  type: string
  default: 'RG'

- name: azureLocation
  type: string
  default: 'UK South'

- name: terraformVersion
  type: string
  default: '1.0.4'

- name: artifactName
  type: string
  default: 'Website'

- name: backendresourcegroupname   
  type: string
  default: DevOpsTerraform

- name: backendstorageaccountname
  type: string
  default: devopspostrunjobs

- name: backendcontainername
  type: string
  default: devopsterraformstatefile


jobs:
  - job: Run_Terraform
    displayName: Installing and Running Terraform Post Build Steps
    steps:
      - checkout: self
      - task: TerraformInstaller@0
        displayName: install
        inputs:
          terraformVersion: '${{ parameters.terraformVersion }}'
      - task: CmdLine@2
        inputs:
          script: |
            echo  '$(System.DefaultWorkingDirectory)' 
            dir
      - task: TerraformTaskV2@2
        displayName: init
        inputs:
          provider: azurerm
          command: init
          backendServiceArm: '${{ parameters.serviceConnection }}'
          backendAzureRmResourceGroupName: '${{ parameters.backendresourcegroupname }}'
          backendAzureRmStorageAccountName: '${{ parameters.backendstorageaccountname }}'
          backendAzureRmContainerName: '${{ parameters.backendcontainername }}'
          backendAzureRmKey: terraform.tfstate
          workingDirectory: '${{ parameters.terraformWorkingDirectory }}'
      - task: TerraformTaskV1@0
        displayName: plan
        inputs:
          provider: azurerm
          command: plan
          commandOptions: '-input=false'
          environmentServiceNameAzureRM: '${{ parameters.serviceConnection }}'
          workingDirectory: '${{ parameters.terraformWorkingDirectory }}'
      - task: TerraformTaskV1@0
        displayName: apply
        inputs:
          provider: azurerm
          command: apply
          commandOptions: '-input=false -auto-approve'
          environmentServiceNameAzureRM: '${{ parameters.serviceConnection }}'
          workingDirectory: '${{ parameters.terraformWorkingDirectory }}'
My Working Terraform Template File

parameters:
  - name: terraformWorkingDirectory
    type: string
    default: $(System.DefaultWorkingDirectory)/Example
  - name: serviceConnection
    type: string
    default: value
  - name: azureSubscription
    type: string
    default: value
  - name: appconnectionname
    type: string
    default: value
  - name: backendresourcegroupname   
    type: string
    default: DevOpsTerraform
  - name: backendstorageaccountname
    type: string
    default: devopsterraform
  - name: backendcontainername
    type: string
    default: devopsterraformstatefile
  - name: RG
    type: string
    default: RG
  - name: azureLocation
    type: string
    default: UK South
  - name: terraformVersion
    type: string
    default: 1.0.4
  - name: artifactName
    type: string
    default: Website

jobs:
  - job: Run_Terraform
    displayName: Installing and Running Terraform
    steps:
      - checkout: Terraform
      - task: TerraformInstaller@0
        displayName: install
        inputs:
          terraformVersion: '${{ parameters.terraformVersion }}'
      - task: CmdLine@2
        inputs:
          script: |
            echo  '$(System.DefaultWorkingDirectory)' 
            dir
      - task: TerraformTaskV2@2
        displayName: init
        inputs:
          provider: azurerm
          command: init
          backendServiceArm: '${{ parameters.serviceConnection }}'
          backendAzureRmResourceGroupName: '${{ parameters.backendresourcegroupname }}'
          backendAzureRmStorageAccountName: '${{ parameters.backendstorageaccountname }}'
          backendAzureRmContainerName: '${{ parameters.backendcontainername }}'
          backendAzureRmKey: terraform.tfstate
          workingDirectory: '${{ parameters.terraformWorkingDirectory }}'
      - task: TerraformTaskV1@0
        displayName: plan
        inputs:
          provider: azurerm
          command: plan
          commandOptions: '-input=false'
          environmentServiceNameAzureRM: '${{ parameters.serviceConnection }}'
          workingDirectory: '${{ parameters.terraformWorkingDirectory }}'
      - task: TerraformTaskV1@0
        displayName: apply
        inputs:
          provider: azurerm
          command: apply
          commandOptions: '-input=false -auto-approve'
          environmentServiceNameAzureRM: '${{ parameters.serviceConnection }}'
          workingDirectory: '${{ parameters.terraformWorkingDirectory }}'

Please note if you take the troublesome template yaml file and put a pool on it and run it as a pipeline it works and initialises terraform and builds but when passed from the original yaml file it fails.

Jason
  • 510
  • 5
  • 27

2 Answers2

0

After a few hours trouble shooting this, it turned out that the original YAML file that was running the Terraform post build, the permissions were messed up on it. So a simple re-make of the file fixed this.

Jason
  • 510
  • 5
  • 27
0

I have followed this solution from here, Instead of rerunning just that release, I reran the pipeline. error is solved Error: spawn terraform ENOENT during Azure Pipeline Terraform