Azure Devops如何在任务中获取安全的变量值

时间:2020-09-15 17:38:12

标签: azure azure-devops azure-pipelines

我在管道变量页面上将连接字符串声明为安全的,但是在管道中我无法获取该值。我已经阅读了文档中的相关页面,但是当我想在任务中使用值时没有提供示例。 https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables

The task where I want to get the value you can see below
- task: AzureWebAppContainer@1
        displayName: Deploy to App Service
        inputs:
          azureSubscription: 'free_subscription(id)'
          appName: 'app-service'
          containers: 'containerregistry.azurecr.io/something:$(tag)'
          **WANT TO USE IT HERE**
          appSettings: 'CONNECTION_STRING=$(CONNECTION_STRING)'

$(CONNECTION_STRING)返回空字符串

编辑

更新了代码,但仍然无法正常工作...应用设置中的连接字符串仍然为空。

- stage: Deploy
    displayName: Deploy to App Service
    jobs:
    - job: Deploy
      displayName: Deploy
      pool:
        vmImage: ubuntu-latest
      steps:
      - bash: echo "##vso[task.setvariable variable=CONNECTION_STRING]$CONNECTION_STRING"
        displayName: 'Set variable'
      - task: AzureWebAppContainer@1
        displayName: Deploy to App Service
        inputs:
          azureSubscription: 'free_subscription(id)'
          appName: 'app-service'
          containers: 'appnamecontainerregistry.azurecr.io/repository:$(tag)'
          appSettings: |
            [
              {
                "name": "CONNECTION_STRING",
                "value": "$(CONNECTION_STRING)",
                "slotSetting": false
              }
            ]

3 个答案:

答案 0 :(得分:1)

选择一个秘密Pipeline变量时,您会忽略以下内容: enter image description here

这是referenced here,因此取决于您的操作系统是Linux还是Windows,它将是: Linux:$CONNECTION_STRING Windows:%CONNECTION_STRING%

答案 1 :(得分:1)

我已经阅读了文档中的相关页面,但没有提供 我想在任务中使用值的示例。

要将脚本中定义的变量用作下一步的输入,可以选中Use variables as task inputs。有关更多详细信息,请检查我的another issue

应用设置中的连接字符串仍然为空。

据我所知,建议的appsettings格式为-key value而不是上面的json格式。 样本:

          appSettings: |
            -Port 5000 -RequestTimeout 5000 
            -WEBSITE_TIME_ZONE "Eastern Standard Time"

我建议您在AzureWebAppContainer步骤之后使用Azure App Service Settings task来配置ConnectionString,而不是在AzureWebAppForContainer任务中修改ConnectionString。您可以在connectionStrings的{​​{1}}输入中使用此格式来配置所需的内容:

AzureAppServiceSettings

答案 2 :(得分:1)

我找到了2种有效的解决方案

按变量部分(我更喜欢这种方式)

 variables:
tag: '$(Build.BuildId)'
# Set secret variable to pipeline variable
CONNECTION_STRING_UNSECRET: $(CONNECTION_STRING)

- stage: Deploy
    displayName: Deploy to App Service
    jobs:
    - job: Deploy
      displayName: Deploy
      pool:
        vmImage: ubuntu-latest
      steps:
      - task: AzureWebAppContainer@1
        displayName: Deploy to App Service
        inputs:
          azureSubscription: 'free_subscription(id)'
          appName: 'app-service'
          containers: 'appnamecontainerregistry.azurecr.io/repository:$(tag)'
          appSettings: |
        -CONNECTION_STRING $(CONNECTION_STRING_UNSECRET)

并通过bash脚本

    - stage: Deploy
            displayName: Deploy to App Service
            jobs:
            - job: Deploy
              displayName: Deploy
              pool:
                vmImage: ubuntu-latest
              steps:
    - bash: |
          echo "##vso[task.setvariable variable=CONNECTION_STRING_UNSECRET]$ 
 CONNECTION_STRING_ENV_VARIABLE"
        displayName: 'Set variable'
        env:
          CONNECTION_STRING_ENV_VARIABLE: $(CONNECTION_STRING)
              - task: AzureWebAppContainer   @1
                displayName: Deploy to App Service
                inputs:
                  azureSubscription: 'free_subscription(id)'
                  appName: 'app-service'
                  containers: 'appnamecontainerregistry.azurecr.io/repository:$(tag)'
                  appSettings: |
                -CONNECTION_STRING $(CONNECTION_STRING_UNSECRET)

顺便说一句,如果您在控制台中看到变量的“ ***”值,则表明您的步骤已成功读取该值。

感谢@Lance Li-MSFT提供的链接,它们确实为我提供了帮助。