我一直在写一些地形,并使用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)
发布评论
评论(4)
您需要使用模板表达式语法对于服务连接变量:
我想是因为在管道运行之前需要知道服务连接。
样品用例。使用称为
variable.dev.yaml
的变量文件:然后,您可以在管道中引用该文件:
You need to use a Template expression syntax for the service connection variable:
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
:You could then reference that in your pipeline:
如果要使用运行时变量(例如$(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
一个解决方法对我有用。
创建一个通用变量组(库),并定义一个或多个
serviceConnection
名称。现在,该组需要被声明为根级或管道级别。
One workaround working for me.
Create a common variable group (Library) and define one or more
serviceconnection
names.Now, this group needs to be declared as root-level or pipeline-level.
一种对我有用的方法是将变量组的声明群移至管道脚本的根源。
之前:
之后:
One approach that worked for me, is to move the declaration of variables group to the root of the pipeline script.
Before :
After :