有什么方法可以将变量用于与Azure的服务连接?

发布于 2025-02-11 06:34:53 字数 741 浏览 6 评论 0 原文

我一直在写一些地形,并使用Azure Devops部署管道。但是,如果我使用变量$(ServiceConnection)进行服务连接,则失败了以下错误:

有一个资源授权问题:“管道无效。无法找到服务连接或尚未授权使用的服务。 我尝试授权它,但没有运气。有解决方法吗?

该任务是使用Terraform的YAML任务:

- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
   displayName: 'Terraform Init'
   inputs:
     command: init
     workingDirectory: $(Agent.BuildDirectory)/a/azuredirectory/terraform
     backendType: azurerm
     backendServiceArm: $(serviceconnection)
     backendAzureRmResourceGroupName: $(ResourceGroupName)
     backendAzureRmStorageAccountName: $(StorageAccountName)
     backendAzureRmContainerName: $(ContainerName)
     backendAzureRmKey: $(AzureRmKey)

I have been writing some terraform and using Azure Devops to deploy the pipeline. However if I use a variable $(serviceconnection) for the service connection it fails with the following error:

There was a resource authorization issue: "The pipeline is not valid. Job DeployDev: Step TerraformCLI1 input backendServiceArm references service connection $(serviceconnection) which could not be found. The service connection does not exist or has not been authorized for use.
I Have tried authorising it but no luck. Is there any workaround?

The task is a YAML task to use terraform as below :

- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
   displayName: 'Terraform Init'
   inputs:
     command: init
     workingDirectory: $(Agent.BuildDirectory)/a/azuredirectory/terraform
     backendType: azurerm
     backendServiceArm: $(serviceconnection)
     backendAzureRmResourceGroupName: $(ResourceGroupName)
     backendAzureRmStorageAccountName: $(StorageAccountName)
     backendAzureRmContainerName: $(ContainerName)
     backendAzureRmKey: $(AzureRmKey)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光是把杀猪刀 2025-02-18 06:34:53

您需要使用模板表达式语法对于服务连接变量:

backendServiceArm: ${{ variables.serviceconnection }}

我想是因为在管道运行之前需要知道服务连接。

样品用例。使用称为 variable.dev.yaml 的变量文件:

variables:
  serviceconnection: my-dev-service-connection-name
...

然后,您可以在管道中引用该文件:

jobs:
- job: myJob
  ...
  variables:
  - template: ./variable.dev.yaml
  steps:
  - task: AzureCLI@2
    inputs:
      azureSubscription: ${{ variables.serviceconnection  }}
...

You need to use a Template expression syntax for the service connection variable:

backendServiceArm: ${{ variables.serviceconnection }}

I imagine it's because the service connection needs to be known before the pipeline runs.

Sample use case. Using a variable file called variable.dev.yaml:

variables:
  serviceconnection: my-dev-service-connection-name
...

You could then reference that in your pipeline:

jobs:
- job: myJob
  ...
  variables:
  - template: ./variable.dev.yaml
  steps:
  - task: AzureCLI@2
    inputs:
      azureSubscription: ${{ variables.serviceconnection  }}
...
友欢 2025-02-18 06:34:53

如果要使用运行时变量(例如$(ServiceConnection)),现在不支持它。

可以使用 $ {{variables.serviceconnection}} ,如Thomas所推荐。但是,这种做法意味着您必须提前指定变量(在运行管道之前)。

对于服务连接,您可以直接指定一个值,也可以使用“编译时变量” $ {{XXX}},该{{xxx}}将在运行之前使用值扩展然后填充服务连接部分。在$(xxx)的使用中,无法获得任务的服务连接,因为这是一个运行时值。

运行前需要指定服务连接。管道运行期间变量的更改(运行时更改)将不会由后续任务的服务连接部分获取。

您正在使用运行时变量。

但是运行时间变量不支持服务连接或Azure订阅。该变量将在运行时间初始化。

您可以按照下面的方法进行使用以使用不同的服务连接:

但仍然需要点,在管道运行之前,参数会扩展,硬码不可避免,这是设计。

在此官方文件中也很明显:

https://learn.microsoft.com/en-us/en-us/azure/azure/devops/devops/devops/pipelines/pipelines/library/library/service/service -endpoints?view = azure-devops& tabs = yaml#use-a-service-connection

If you want to use runtime variable like $(serviceconnection), it is not supported now.

You can use ${{ variables.serviceconnection }} as Thomas recommended. But this practice means that you have to specify variables in advance(Before you run the pipeline).

For service connections, you can specify a value directly or use the ’compile-time variable‘ ${{xxx}}, which will expand and then populate the service connection section with values before running. In this usage of $(xxx), the service connection of the task cannot be obtained, because this is a runtime value.

The service connection needs to be specified before running. The changes (runtime changes) of the variables during the pipeline run will not be acquired by the service connection part of the subsequent task.

You are using a runtime variable.

But run time variables aren't supported for service connection OR azure subscription. The variable will get initialized at the run time.

https://github.com/microsoft/azure-pipelines-tasks/issues/10376#issuecomment-514477023

You can follow below method to use different service connection:

https://stackoverflow.com/a/57520153/6261890

But still need point that, parameters are expanded just before the pipeline runs, hardcode the specific service connection is unavoidable, this is by design.

Also clearly in this official document:

https://learn.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#use-a-service-connection

enter image description here

携余温的黄昏 2025-02-18 06:34:53

一个解决方法对我有用。

创建一个通用变量组(库),并定义一个或多个 serviceConnection 名称。

现在,该组需要被声明为根级或管道级别。

name: Azure CI/CD

pr:
  branches:
    include:
      - develop

trigger:
  branches:
    include:
      - develop

pool:
  vmImage: ubuntu-latest

variables:
- group: CommonVariables

stages:
  - stage: Build        
    jobs:
      - job: B1
        steps:
          - task: AzureCLI@2
            displayName: Preview Bicep Changes
            inputs:
              azureSubscription: '$(azureResourceManagerServiceConnection)'
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                az --version

One workaround working for me.

Create a common variable group (Library) and define one or more serviceconnection names.

Pipeline variable group

Now, this group needs to be declared as root-level or pipeline-level.

name: Azure CI/CD

pr:
  branches:
    include:
      - develop

trigger:
  branches:
    include:
      - develop

pool:
  vmImage: ubuntu-latest

variables:
- group: CommonVariables

stages:
  - stage: Build        
    jobs:
      - job: B1
        steps:
          - task: AzureCLI@2
            displayName: Preview Bicep Changes
            inputs:
              azureSubscription: '$(azureResourceManagerServiceConnection)'
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                az --version
眼中杀气 2025-02-18 06:34:53

一种对我有用的方法是将变量组的声明群移至管道脚本的根源。

之前:

pool:
  vmImage: windows-2022

stages:
  - stage: BuildDevelopment
    variables:
      - group: group-development

    steps:
      - task: SonarCloudPrepare@1
        displayName: "Prepare SonarCloud Analysis"
        inputs:
          SonarCloud: "$(sc_endpoint_name)"
          # Error will occur when pipeline tries to access this part

之后:

pool:
  vmImage: windows-2022

variables:
  - group: group-development

stages:
  - stage: BuildDevelopment
    steps:
      - task: SonarCloudPrepare@1
        displayName: "Prepare SonarCloud Analysis"
        inputs:
          SonarCloud: "$(sc_endpoint_name)"

One approach that worked for me, is to move the declaration of variables group to the root of the pipeline script.

Before :

pool:
  vmImage: windows-2022

stages:
  - stage: BuildDevelopment
    variables:
      - group: group-development

    steps:
      - task: SonarCloudPrepare@1
        displayName: "Prepare SonarCloud Analysis"
        inputs:
          SonarCloud: "$(sc_endpoint_name)"
          # Error will occur when pipeline tries to access this part

After :

pool:
  vmImage: windows-2022

variables:
  - group: group-development

stages:
  - stage: BuildDevelopment
    steps:
      - task: SonarCloudPrepare@1
        displayName: "Prepare SonarCloud Analysis"
        inputs:
          SonarCloud: "$(sc_endpoint_name)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文