簡體   English   中英

如何將應用服務名稱從我的 terraform 部署傳遞到 devops 中的應用服務部署?

[英]How do I pass the app service name from my terraform deployment to an app service deployment in devops?

我在 Azure Devops 中有一個發布管道,分為兩個階段:

  1. 創建基礎設施 - 在 azure 中創建資源(資源組、應用程序服務計划等)
  2. 部署工件 - 將 .NET 構建工件部署到在第 1 階段創建的 Azure 應用程序服務

在第 2 階段,我正在運行任務“部署 Azure 應用服務”,它需要幾個參數 {Azure 訂閱和應用服務名稱}。 每個參數都提供一個選項下拉列表供您選擇。 這是有問題的,因為應用服務還不存在,所以沒有辦法引用它,下拉列表是空白的。

有沒有辦法在第 1 階段使用 output 變量並將它們提供給第 2 階段的參數?

我認為這可以使用 null 資源首先將一些數據寫入文件來實現:

resource "null_resource" "create_app_service_data" {
  count = var.deploy ? 1: 0
  provisioner "local-exec" {
    command = "chmod +x ${path.module}/scripts/write_json.sh; ${path.module}/scripts/write_json.sh"
    environment = {
      APP_SERV_NAME = split(output.app_serv_id, "/")[8]
      APP_SERV_SUB_ID = split(output.app_serv_id, "/")[2]
      APP_SERV_RG_NAME = split(output.app_serv_id, "/")[4]
    }
  }
}

在您的 Terraform 中執行此步驟后,您將在與 TF 代碼相同的工作目錄中擁有一個文件,因此您需要做一些工作才能將其放入您希望使用的目錄中,這里有一些要運行的示例 bash到那里:

#!/bin/bash
APP_SERV_NAME = "${APP_SERV_NAME}"
APP_SERV_SUB_ID = "${APP_SERV_SUB_ID}"
APP_SERV_RG_NAME = "${APP_SERV_RG_NAME}"

JSON_STRING=$( jq -n \
                  --arg app_serv_name "refs/heads/$FEATURE_BRANCH_NAME" \
                  --arg sub_id "refs/heads/$FEATURE_BRANCH_NAME" \
                  --arg rg_name "refs/heads/$FEATURE_BRANCH_NAME" \
                  '{"app_serv_name": $app_serv_name, "sub_id": $sub_id, "rg_name": $rg_name}')

echo "$JSON_STRING" > app_service_data.json

之后,您需要設置一些 output 變量以用於后續任務/工作/階段,如下所示:

- bash: |
    APP_SERV_NAME=$(cat app_service_data.json | jq '.app_serv_name')
    SUB_ID=$(cat app_service_data.json | jq '.sub_id')
    echo "##vso[task.setvariable variable=appServiceName;isoutput=true]$APP_SERV_NAME"
    echo "##vso[task.setvariable variable=subscriptionId;isoutput=true]$SUB_ID"
  displayName: 🚨Set Outputs For Deployment🚨
  name: setVariablesForDeployment

從那里我想你可以像這樣擴展任務中的變量:

- task: AzureRmWebAppDeployment@4
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: $(setVariablesForDeployment.subscriptionId)
    appType: 'webApp'
    WebAppName: $(setVariablesForDeployment.appServiceName)

在此處查看有關 Azure DevOps 的 output 變量的更多文檔。 我意識到這里有很多連接的步驟,但我並沒有敏銳地意識到如何獲取 Terraform 輸出的值並將它們放入管道中。

暫無
暫無

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

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