Azure Pipelines 错误“TF401444:请至少登录一次:...”

问题描述 投票:0回答:3

我想在管道中添加“创建工作项”步骤。它应该是在管道发生故障时创建错误单。我在自己的虚拟机上运行管道,因此我必须登录到 Azure,并且我想为其使用服务主体:

az login --service-principal -u <app-id> -p <password-or-cert> --tenant <tenant>

我相信这一步效果很好,因为我收到了回复(如下面的屏幕截图所示)。接下来,我执行命令来创建错误票证:

az boards work-item create --organization https://dev.azure.com/my_org --project "Project" --type Bug --title "title" --description "desc" --output json --assigned-to ...

此时我不断收到错误:

"TF401444: Please sign-in at least once as: credentials/of/tenant in a web browser to enable access to the service".

Screenshot of the execution

我已经尝试使用我的帐户凭据登录

az login -u mail -p psw

它工作正常,并且创建了错误票证,但正如您可以猜到的那样,我不想继续使用我的登录凭据在管道中登录到 Azure。 我还尝试使用 PAT 来实现它,并使用命令:

echo $(PAT) | az login

但这需要我连接到虚拟机才能登录我的帐户,并通过消息通知:

WARNING: A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with az login --use-device-code.

记录错误单后也创建了,但我不想每次执行都这样做,因为管道应该是自动化的。

你知道我怎样才能摆脱这个错误吗?

或者您对更好的日志记录有什么建议?

azure azure-devops automation azure-pipelines
3个回答
8
投票

我发现了一些解决方法:您可以使用 $(System.AccessToken) 在管道中使用“脚本”步骤登录,而不是使用 AzureCLI。

- script: az config set extension.use_dynamic_install=yes_without_prompt
displayName: 'Allow extensions'
- script: echo $(System.AccessToken) | az devops login
displayName: 'Login to DevOps'
- script: |
    az boards work-item create --organization https://dev.azure.com/org --project "abc" --type Bug ...
displayName: 'Create a bug ticket'

5
投票

根据 Azure 文档,“正确”的方法是在

System.AccessToken
环境变量中设置
AZURE_DEVOPS_EXT_PAT

# Using the script shortcut syntax
- script: az pipelines variable-group list --output table
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
  displayName: 'List variable groups using the script step'

# Using the task syntax
- task: CmdLine@2
  inputs:
    script: az pipelines variable-group list --output table
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
  displayName: 'List variable groups using the command line task'

请参阅 https://learn.microsoft.com/en-us/azure/devops/pipelines/process/tasks?view=azure-devops&tabs=yaml


0
投票

就我而言,我可以通过在 Azure DevOps 用户设置中添加服务主体来修复错误。链接是这样的:

https://dev.azure.com/< YOUR ORGANIZATION>/_settings/users

我的案例是使用 Terraform 进行配置。这里,登录信息被写入环境变量(以下输入应设置为“addSpnToEnvironment:true”)。管道任务示例:

#init and apply
- task: AzureCLI@2 
  displayName: "tf apply"
  inputs:
    azureSubscription: $(serviceConnectionName)
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: |
      #set conext by name as default / current subscription
      az account set --subscription $(platformManagemntSubscriptionId);
      
      #create variable
      $currentSubscription = $(az account list --query "[?isDefault].id" -o tsv)
      
      #create environment variables for terraform commands
      $env:ARM_CLIENT_ID       = $env:servicePrincipalId
      $env:ARM_CLIENT_SECRET   = $env:servicePrincipalKey
      $env:ARM_TENANT_ID       = $env:tenantId
      $env:ARM_SUBSCRIPTION_ID = $currentSubscription
      
      #initialize 
      terraform init -backend-config="azurerm-vending-plattformresources.tfbackend";

      #apply
      terraform apply $(Build.ArtifactStagingDirectory)/platformresources/platformresources.tfplan;
    addSpnToEnvironment: true
    workingDirectory: $(System.DefaultWorkingDirectory)/terraform/03_add_platformresources

Terraform 文档到工作项资源:https://registry.terraform.io/providers/microsoft/azuredevops/latest/docs/resources/workitem

Terraform 到提供程序配置(使用环境变量):https://registry.terraform.io/providers/microsoft/azuredevops/latest/docs/guides/authenticating_service_principal_using_a_client_certificate#provider-configuration

© www.soinside.com 2019 - 2024. All rights reserved.