在Azure DevOps管道中从PowerShell运行Terraform

时间:2018-10-02 09:25:13

标签: powershell azure-devops terraform azure-pipelines

我正在尝试在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'

1 个答案:

答案 0 :(得分:0)

我将删除mounted符号周围的空格。

我更喜欢使用记录在here中的环境变量:

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

您还可以使用here中所述的环境变量来初始化Terraform变量。