大家好,我是本期的微软MVP实验室研究员贠乾。Azure Pipeline 自身曾经提供了内置变量。不同于上述形式,明天我将带来如何在 Azure DevOps Pipeline 运行时创立、应用动静长期变量,实现变量的动静自定义。接下来让咱们在试验中一探到底吧!
思路浅析
在咱们分享的 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 DevOps Pipeline 运行中创立应用动静长期变量,应用动静长期变量替换 Azure Pipeline 管道变量。
我的项目整体架构图
Pipeline 变量定义、输入
在此阶段,咱们须要利用 azure_cli_script 工作,创立动静长期变量,输入参数,其中最次要的是将动静长期变量输入,Task yaml 如下所示
输入的变量用于同一个 stage,不同 job
- stage: script jobs: - job: azure_cli_script steps: - task: AzureCLI@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: Bash@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: Bash@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/yamltrigger:- remote_statspool: vmImage: ubuntu-lateststages:- stage: script jobs: - job: azure_cli_script steps: - task: AzureCLI@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: Bash@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: Bash@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 的内联脚本中应用治理内变量控制参数
运行 Pipeline,查看配置输入
因为咱们曾经在 azure-pipelines-1.yaml 文件中指定了工作分支 “remote_stats”,当咱们只有触发 “remote_stats” 分支的 “push” 或者 “pull_request” 动作都会触发 Azure DevOps Pipeline 的运行。
雷同 stage 内的 job 输入:
不同 stage 的 job 输入:
总结
本期试验,咱们学习了如何在 Azure DevOps Pipeline 运行期间创立的动静长期变量以及变量的输入,使得咱们更加灵便的在任意 job 中申明自定义的动静长期变量,并将动静长期变量利用到任意的 job 中,这种形式有区别与Pipeline 管道内变量,尤其是在定义阶段和应用语法上,具体内容参考官网文档。
相干链接:
- 在脚本中设置变量
https://docs.microsoft.com/zh... - github 代码地址
https://github.com/yunqian44/... - Terraform 在 Azure DevOps 中的应用系列https://www.cnblogs.com/Allen...
微软最有价值专家(MVP)
微软最有价值专家是微软公司授予第三方技术专业人士的一个寰球奖项。29年来,世界各地的技术社区领导者,因其在线上和线下的技术社区中分享专业知识和教训而取得此奖项。
MVP 是通过严格筛选的专家团队,他们代表着技术最精湛且最具智慧的人,是对社区投入极大的激情并乐于助人的专家。MVP 致力于通过演讲、论坛问答、创立网站、撰写博客、分享视频、开源我的项目、组织会议等形式来帮忙别人,并最大水平地帮忙微软技术社区用户应用 Microsoft 技术。
更多详情请登录官方网站:https://mvp.microsoft.com/zh-cn
长按辨认二维码
关注微软中国MSDN