【问题标题】:Terraform won't initialise in Azure DevOps PipelineTerraform 不会在 Azure DevOps Pipeline 中初始化
【发布时间】:2022-02-01 10:33:19
【问题描述】:

我有一个 Azure 管道,在其中运行 Terraform 预构建和 Terraform 后构建。在中间部署了一些人工制品文件。(后构建和预构建的原因。)。第一个任务有效并且 Terraform 初始化。然而第二个任务失败但代码完全相同,唯一的区别是 Terraform 的工作目录是第二个任务的一个文件夹。我不明白为什么第二个任务一直失败。 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'

此管道与模板管道位于不同的存储库中,但它在第一个任务中不是问题,代码基本相同。

这是我在第二个任务中不断遇到的错误:

##[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

更新:

我的麻烦模板 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 }}'
我的工作 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 }}'

请注意,如果您使用麻烦的模板 yaml 文件并在其上放置一个池并将其作为管道运行,它会工作并初始化 terraform 和构建,但是从原始 yaml 文件传递​​时它会失败。

【问题讨论】:

  • 您能与我们分享您的模板吗?
  • 做过两个模板都是一样的。
  • 我已经添加了另一个工作 yaml 文件以防万一

标签: azure-devops yaml terraform azure-pipelines terraform-provider-azure


【解决方案1】:

在解决这个问题几个小时后,原来运行 Terraform 后期构建的原始 YAML 文件的权限被弄乱了。所以一个简单的文件重新制作解决了这个问题。

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 2022-10-05
    • 1970-01-01
    • 2020-05-01
    • 2021-09-10
    • 2020-03-10
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    相关资源
    最近更新 更多