简体   繁体   English

Terraform 不会在 Azure DevOps 管道中初始化

[英]Terraform won't initialise in Azure DevOps Pipeline

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.).我有一个 Azure 管道,我在其中运行 Terraform 预构建和 Terraform 构建后。 The first task works and Terraform initialises.第一个任务有效,Terraform 初始化。 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.然而第二个任务失败但代码完全相同,唯一的区别是 Terraform 的工作目录是第二个任务的一个文件夹。 I can't figure out why the Second task keeps failing.我不明白为什么第二个任务一直失败。 Please see code bellow for the yml file.请参阅下面的 yml 文件代码。

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:我的麻烦模板 YAML 文件:

 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 我的工作 Terraform 模板文件

 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.请注意,如果您使用麻烦的模板 yaml 文件并在其上放置一个池并将其作为管道运行,它可以工作并初始化 terraform 并构建,但是当从原始 Z6EEDC03A68A69933C763E674F2D 文件传递时它会失败。

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.经过几个小时的故障排除,原来运行 Terraform 后期构建的原始 YAML 文件,权限被搞砸了。 So a simple re-make of the file fixed this.所以一个简单的文件重新制作解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM