首页 > 解决方案 > 在 Azure DevOps Pipeline 中从 PowerShell 运行 Terraform

问题描述

我正在尝试从配置 YAML 的 Azure DevOps 管道中的 PowerShell 步骤运行Terraform,但我无法让它接受我的咒语:它不是初始化状态,而是打印使用说明并退出。

我已经为参数尝试了多种带/不带引号的组合-backend-config,但似乎没有任何效果。

如果我将脚本剪切并粘贴到ps1本地文件中并运行它,一切都会按预期工作。

我在这里的咒语有什么问题./terraform.exe init

这是整个管道配置:

pool:
  vmImage: 'win1803'

steps:
- powershell: |
    # Download the Terraform executable into the current directory
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    $url = 'https://releases.hashicorp.com/terraform/0.11.8/terraform_0.11.8_windows_amd64.zip'
    $output = "$PSScriptRoot\terraform.zip"

    Write-Host "Downloading from $url to $output"
    $wc = New-Object System.Net.WebClient
    $wc.DownloadFile($url, $output)

    Expand-Archive $output -DestinationPath . -Force

    Get-ChildItem
  displayName: 'Download Terraform executable'
- powershell: |
    # Create a config file with secrets, and initialize Terraform
    "storage_account_name = `"$env:TerraformStateAccountName`"" | Out-File "./terraform-state.secrets.tfvars" -Append
    "access_key = `"$env:TerraformStateAccountKey`"" | Out-File "./terraform-state.secrets.tfvars" -Append
    "container_name = `"$env:TerraformStateContainer`"" | Out-File "./terraform-state.secrets.tfvars" -Append

    ./terraform.exe init -backend-config=./terraform-state.secrets.tfvars -input=false
  env:
    TerraformStateAccountName: $(Terraform.State.StorageAccountName)
    TerraformStateAccountKey: $(Terraform.State.StorageAccountKey)
    TerraformStateContainer: $(Terraform.State.ContainerName)
  displayName: 'Initialize Terraform'

标签: powershellazure-devopsterraformazure-pipelines

解决方案


=我会删除标志周围的空白。

我更喜欢使用环境变量,记录在这里

  1. ARM_SUBSCRIPTION_ID
  2. ARM_CLIENT_ID
  3. ARM_CLIENT_SECRET
  4. ARM_TENANT_ID
  5. ARM_OBJECT_ID
  6. ARM_ACCESS_KEY

您还可以使用此处记录的环境变量初始化 Terraform 变量。


推荐阅读