Azure DevOps管道将环境变量传递到Ubuntu主机上的PS脚本中

时间:2020-08-22 05:50:17

标签: powershell azure-devops yaml azure-pipelines

我有一个运行Terraform代码的舞台,但是我需要将Azure IP服务器上的代理IP列入白名单,因此我有一个PowerShell脚本可以处理此问题,但是脚本中的环境变量(例如$ env:company_name)都显示为null,我相信这是因为它的重击。不确定... 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}"

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

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

1 个答案:

答案 0 :(得分:0)

同意Krzysztof Madej。

据我所知,Linux系统是区分大小写的。

在天蓝色devops中设置变量时,它将在环境变量中转换为大写字母。

例如:

enter image description here

因此,当您在Linux系统中使用环境变量时,需要将格式更改为 $env:COMPANY_NAME

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

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