【问题标题】:Azure DevOps pipelines passing environment variables into PS Script on an Ubuntu HostAzure DevOps 管道将环境变量传递到 Ubuntu 主机上的 PS 脚本
【发布时间】:2020-12-11 10:31:07
【问题描述】:

我有一个运行我的 Terraform 代码的阶段,但我需要将我的 Azure SQL 服务器上的代理 IP 列入白名单,所以我有一个 PowerShell 脚本可以处理这个问题,但是,脚本中的环境变量(例如 $ env:company_name) 都显示为 null 我相信因为它的 bash。不确定.... YAML 的部分如下所示。关于我如何做到这一点的任何想法?谢谢

************************来自管道的 YAML************************ **

    steps:
  - checkout: self
  - task: Bash@3
    displayName: 'Install AZ Modules'
    inputs:
      targetType: 'inline'
      script: |
       sudo /usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File "$(Build.Repository.LocalPath)/STAGE2/Scripts/buildazmodule.ps1"
  - task: PowerShell@2
    displayName: 'Add Firewall Rules'
    inputs:
      targetType: filePath
      filePath: './STAGE2/Scripts/firewallrules.ps1'
  - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
    inputs:
      terraformVersion: '0.12.28'
  - script: terraform version
    displayName: 'Terraform Version'
  - script: az cloud set --name $(cloud)
    displayName: 'Set Cloud'
  - script:  az login --service-principal -u $(client_id) -p $(client_secret) --tenant $(tenant_id)
    displayName: 'Log Into Azure'
  - script: terraform init -backend-config=resource_group_name=$(sg_resource_group) -backend-config="storage_account_name=$(sg_name)" -backend-config="container_name=$(blob_storage)" -backend-config="access_key=$(sg_accesskey)" -backend-config="key=$(state_file)" -backend-config="environment=$(cloud_environment_name)"
    displayName: 'Terraform Init'
    workingDirectory: $(System.DefaultWorkingDirectory)/STAGE2
  - script: terraform plan -var="client_id=$(client_id)" -var="client_secret=$(client_secret)" -var="tenant_id=$(tenant_id)" -var="subscription_id=$(subscription_id)" -var="environment=$(cloud_environment_name)" -var="company_name=$(company_name)" -var="cloudsitename=$(cloudsitename)" -var="envtype=$(envtype)"  -var="builddate=$(builddate)"  -var="is_public=$(is_public)" -var="region=$(region)"  -var="os_image_skey=$(os_image_skey)"  -var="is_osmanaged_ad=$(is_osmanaged_ad)" -var="remote_tenant=$(remote_tenant)" -var="is_fedramp=$(is_fedramp)" -var="level=$(level)" -var="onestream_version=$(onestream_version)" -out="out.plan"
    displayName: 'Terraform Plan'
    workingDirectory: $(System.DefaultWorkingDirectory)/STAGE2
  - script: terraform apply out.plan
    displayName: 'Terraform Apply'
    workingDirectory: $(System.DefaultWorkingDirectory)/STAGE2

************************部分脚本代码************************ ****

$envtype = $env:envtype
$CompanyName = $env:company_name
$CompanyNameTemp = $CompanyName.ToLower() #input production environment name e.g. the "tmfgroup" in rg"tmfgroup" ***PIPELINE VAR***
$CompanyName = ($CompanyNametemp.subString(0, [System.Math]::Min(8, $CompanyNametemp.Length))).Trim()
$sgname = "sg${CompanyName}"
$rgname = "rg${CompanyName}"
$kvName = "kv${CompanyName}"
$sqlName = "sql${CompanyName}${envtype}"

*************************** 错误************************ ***

无效操作:/home/vsts/work/1/s/STAGE2/Scripts/firewallrules.ps1:4 线 | 4 | $CompanyNameTemp = $CompanyName.ToLower() #输入生产环境... | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |您不能在空值表达式上调用方法。

【问题讨论】:

  • 你可以试试$env:COMPANY_NAME吗?
  • 嗨@erwabo。这张票有更新吗?随时让我知道答案是否可以解决此问题。只是提醒this

标签: powershell azure-devops yaml azure-pipelines


【解决方案1】:

同意 Krzysztof Madej。

据我所知,linux系统区分大小写。

在 azure devops 中设置变量时,会在环境变量中转换为大写字母。

例如:

所以在Linux系统中使用环境变量时,需要将格式改为 $env:COMPANY_NAME.

顺便说一句,您可以使用脚本输出所有环境变量(例如env | sort)。

然后你可以检查环境变量格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2020-10-23
    • 2019-10-09
    相关资源
    最近更新 更多