Azure Terraform(十一)Azure DevOps Pipeline 內的動態臨時變數的使用

Grant_Allen 發表於 2022-04-28

思路淺析

  在我們分析的 Azure Terraform 系列文中有介紹到關於 Terraform 的狀態檔案遠端儲存的問題,我們在  Azure DevOps Pipeline 的 Task Job 加 azure_cli_script 執行內聯指令碼(該指令碼幫我們建立好 Terraform 狀態檔案儲存所在的 Azure Resource Group、 Azure Storage Account、Azure KeyVault 等資源),需要注意的是,內聯指令碼中有使用動態變數,該變數臨時儲存 Azure Storage Account 的 Account Key,如下圖所示:

Azure Terraform(十一)Azure DevOps Pipeline 內的動態臨時變數的使用

本篇文章,我繼續帶領大家,分析如何在 Azure DevOps Pipeline 執行中建立使用動態臨時變數,使用動態臨時變數替換 Azure Pipeline 管道變數。

專案整體架構圖

Azure Terraform(十一)Azure DevOps Pipeline 內的動態臨時變數的使用

Pipeline 變數定義、輸出

在此階段,我們需要利用 azure_cli_script 任務,建立動態臨時變數,輸出引數,其中最主要的是將動態臨時變數輸出,Task yaml 如下所示

輸出的變數用於同一個 stage,不同 job

- stage: script
  jobs:
   - job: azure_cli_script
     steps: 
      - task: [email protected]2
        displayName: 'Azure CLI :Create Storage Account,Key Vault And Set KeyVault Secret'
        name: 'output_variable'
        inputs:
          azureSubscription: 'Microsoft Azure Subscription(xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)'
          scriptType: 'bash'
          addSpnToEnvironment: true
          scriptLocation: 'inlineScript'
          inlineScript: |
              # create azure resource group
              az group create --location eastasia --name $(terraform_rg)
      
              # create azure storage account
              az storage account create --name $(storage_account) --resource-group $(terraform_rg) --location eastasia --sku Standard_LRS
      
              # create storage account container for tf state 
              az storage container create --name $(storage_account_container) --account-name $(storage_account)
      
              # query storage key and set variable
              ACCOUNT_KEY=$(az storage account keys list --resource-group $(terraform_rg) --account-name $(storage_account) --query "[?keyName == 'key1'][value]" --output tsv)
      
              # create azure keyvault
              az keyvault create --name $(keyvault) --resource-group $(terraform_rg) --location eastasia --enable-soft-delete false
      
              # set keyvault secret,secret value is ACCOUNT_KEY
              az keyvault secret set --name $(keyvault_sc) --vault-name $(keyvault)  --value $ACCOUNT_KEY

              # set secret varivale and add to environment
              echo "##vso[task.setvariable variable=ACCOUNT_KEY;isOutput=true]$ACCOUNT_KEY"
              #echo "##vso[task.setvariable variable=ACCOUNT_KEY;issecret=true;isOutput=true]$ACCOUNT_KEY"
       
   - job: same_stage_echo
     dependsOn: azure_cli_script
     variables:
       ACCOUNT_KEY: $[dependencies.azure_cli_script.outputs['output_variable.ACCOUNT_KEY']]
     steps:
       - task: [email protected]3
         displayName: 'Bash :output temporary variables in different jobs on the same stage'
         inputs:
           targetType: 'inline'
           script: |
             # echo ACCOUNT_KEY
             echo "ACCOUNT_KEY is $ACCOUNT_KEY"

輸出變數用於不同 stage

- stage: echo_varibale
  dependsOn: script
  jobs:
    - job: different_stage_echo
      variables:
        ACCOUNT_KEY: $[stageDependencies.script.azure_cli_script.outputs['output_variable.ACCOUNT_KEY']]
      steps:
        - task: [email protected]3
          displayName: 'Bash :output temporary variables in same jobs on the same stage'
          inputs:
            targetType: 'inline'
            script: |
              # echo ACCOUNT_KEY
              echo "ACCOUNT_KEY is $ACCOUNT_KEY"

以下為完整的   azure-pipelines-1.yaml

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- remote_stats

pool:
  vmImage: ubuntu-latest

stages:
- stage: script
  jobs:
   - job: azure_cli_script
     steps: 
      - task: [email protected]2
        displayName: 'Azure CLI :Create Storage Account,Key Vault And Set KeyVault Secret'
        name: 'output_variable'
        inputs:
          azureSubscription: 'Microsoft Azure Subscription(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx)'
          scriptType: 'bash'
          addSpnToEnvironment: true
          scriptLocation: 'inlineScript'
          inlineScript: |
              # create azure resource group
              az group create --location eastasia --name $(terraform_rg)
      
              # create azure storage account
              az storage account create --name $(storage_account) --resource-group $(terraform_rg) --location eastasia --sku Standard_LRS
      
              # create storage account container for tf state 
              az storage container create --name $(storage_account_container) --account-name $(storage_account)
      
              # query storage key and set variable
              ACCOUNT_KEY=$(az storage account keys list --resource-group $(terraform_rg) --account-name $(storage_account) --query "[?keyName == 'key1'][value]" --output tsv)
      
              # create azure keyvault
              az keyvault create --name $(keyvault) --resource-group $(terraform_rg) --location eastasia --enable-soft-delete false
      
              # set keyvault secret,secret value is ACCOUNT_KEY
              az keyvault secret set --name $(keyvault_sc) --vault-name $(keyvault)  --value $ACCOUNT_KEY

              # set secret varivale and add to environment
              echo "##vso[task.setvariable variable=ACCOUNT_KEY;isOutput=true]$ACCOUNT_KEY"
              #echo "##vso[task.setvariable variable=ACCOUNT_KEY;issecret=true;isOutput=true]$ACCOUNT_KEY"
       
   - job: same_stage_echo
     dependsOn: azure_cli_script
     variables:
       ACCOUNT_KEY: $[dependencies.azure_cli_script.outputs['output_variable.ACCOUNT_KEY']]
     steps:
       - task: [email protected]3
         displayName: 'Bash :output temporary variables in different jobs on the same stage'
         inputs:
           targetType: 'inline'
           script: |
             # echo ACCOUNT_KEY
             echo "ACCOUNT_KEY is $ACCOUNT_KEY"

- stage: echo_varibale
  dependsOn: script
  jobs:
    - job: different_stage_echo
      variables:
        ACCOUNT_KEY: $[stageDependencies.script.azure_cli_script.outputs['output_variable.ACCOUNT_KEY']]
      steps:
        - task: [email protected]3
          displayName: 'Bash :output temporary variables in same jobs on the same stage'
          inputs:
            targetType: 'inline'
            script: |
              # echo ACCOUNT_KEY
              echo "ACCOUNT_KEY is $ACCOUNT_KEY"

*****重點*****:管道內變數與動態臨時變數使用區別

Pipeline 管道內使用方式:$(變數名稱)

動態臨時變數使用方式:$變數名稱

配置 Pipeline 管道變數

使用 Azure CLI 建立 Azure Storage Account、Azure Key Vault 的內聯指令碼中使用管理內變數控制引數

變數名 變數值
terraform_rg Web_Test_TF_RG
storage_account cnbatetfstorage
storage_account_container tf-state
keyvault cnbate-terraform-kv
keyvault_sc terraform-stste-storage-key
container_key cnbate.tf.stats

執行 Pipeline,檢視配置輸出

由於我們已經在 azure-pipelines-1.yaml 檔案中指定了工作分支 “remote_stats”,當我們只要觸發 “remote_stats” 分支的 “push” 或者 “pull_request” 動作都會觸發 Azure DevOps Pipeline 的執行。

相同 stage 內的 job 輸出

Azure Terraform(十一)Azure DevOps Pipeline 內的動態臨時變數的使用

不同 stage 的 job 輸出

Azure Terraform(十一)Azure DevOps Pipeline 內的動態臨時變數的使用

總結

本期實驗,我們學習瞭如何在 Azure DevOps Pipeline 執行期間建立的動態臨時變數以及變數的輸出,使得我們更加靈活的在任意 job 中宣告自定義的動態臨時變數,並將動態臨時變數應用到任意的 job 中,這種方式有區別與Pipeline 管道內變數,尤其是在定義階段和使用語法上,詳細內容參考官方文件。

在指令碼中設定變數:https://docs.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts

github 程式碼地址:https://github.com/yunqian44/Terraform_Cnbate_Traffic_Manager

Terraform 在 Azure DevOps 中的使用系列:https://www.cnblogs.com/AllenMaster/category/1876925.html 

相關文章