簡體   English   中英

在Azure DevOps管道中從PowerShell運行Terraform

[英]Running Terraform from PowerShell in an Azure DevOps Pipeline

我正在嘗試在YAML配置的Azure DevOps管道中從PowerShell步驟運行Terraform ,但是我無法接受它的咒罵:它沒有初始化狀態,而是打印使用說明並退出。

我已經嘗試了-backend-config參數的帶/不帶引號的多種組合,但似乎沒有任何效果。

如果我在本地將腳本剪切並粘貼到ps1文件中並運行它,那么一切都會按預期進行。

我對./terraform.exe init咒罵是./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. ARM_SUBSCRIPTION_ID
  2. ARM_CLIENT_ID
  3. ARM_CLIENT_SECRET
  4. ARM_TENANT_ID
  5. ARM_OBJECT_ID
  6. ARM_ACCESS_KEY

您還可以使用此處記錄的環境變量來初始化Terraform變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM