简体   繁体   English

如何在 azure 管道中实现等效的 az login

[英]How to implement the equivalent of az login in azure pipeline

I have an integration test that needs to access the cosmosdb in azure cloud.我有一个集成测试需要访问 azure 云中的 cosmosdb。 Locally I need to login to the azure and then run the test在本地我需要登录到 azure 然后运行测试

az login 
poetry run pytest test/integration 

My question is how can I implement the equivalent of az login in the azure DevOps pipeline?我的问题是如何在 azure DevOps 管道中实现等效的az login Isn't the Azure DevOps Server is already connected to the Azure cloud naturally? Azure DevOps Server 不是已经自然连接到 Azure 云了吗?

The recommended way to access resources in Azure is to create an Azure Resource Manager -type service connection from Project Settings -> Service Connections -> New Service Connection -> Azure Resource Manager -> Service Principal (automatic).在 Azure 中访问资源的推荐方法是从 Project Settings -> Service Connections -> New Service Connection -> Z3A580F142203677F1F1F0BC30898F6 自动创建 Azure Resource Manager 类型的服务连接 -> Z3A580F142203677F1F1F0BC30898F6 Creating a service connection requires you to have sufficient rights to the target Azure Subscription.创建服务连接需要您对目标 Azure 订阅拥有足够的权限。

A service connection is essentially a service principal that then needs to be authorized to access the resources needed.服务连接本质上是一个服务主体,然后需要授权它来访问所需的资源。 In this case, you'd have to grant this service principal the (absolutely minimal) access it needs in order to perform the tests.在这种情况下,您必须向该服务主体授予执行测试所需的(绝对最小的)访问权限。

I'm not an expert on Cosmos DB, but I do know that it supports multiple ways of securely accessing the data, so you might want to explore options like logging in with access keys as well.我不是 Cosmos DB 方面的专家,但我知道它支持多种安全访问数据的方式,因此您可能还想探索使用访问密钥登录等选项。 You can run Az Cli commands in pipeline with the Azure Cli task like this:您可以使用 Azure Cli 任务在管道中运行 Az Cli 命令,如下所示:

- task: AzureCLI@2
  displayName: Azure CLI
  inputs:
    azureSubscription: '$(azureSubscriptionName)'
    scriptType: pscore
    scriptLocation: inlineScript
    inlineScript: |          
      az command parameters

With the service connection name in $(azureSubscriptionName) and actual command (s) with inlineScript (or reference a physical script if that's more suitable).使用 $(azureSubscriptionName) 中的服务连接名称和带有 inlineScript 的实际命令(或者如果更合适,请参考物理脚本)。 Just remember that you are operating in Azure via the service connection/principal, so you need to grant that sufficient rights to do what needs to be done.请记住,您是通过服务连接/主体在 Azure 中操作的,因此您需要授予足够的权限来执行需要执行的操作。

There are multiple ways to do this, so a bit of exploration is required.有多种方法可以做到这一点,因此需要进行一些探索。 Also, there seems to be something called Cosmod DB emulator and you might want to check that out first - when running unit/integration tests, the best practice would be not having to access an actual resource at all.此外,似乎有一个叫做 Cosmod DB 模拟器的东西,你可能想先检查一下——在运行单元/集成测试时,最好的做法是根本不必访问实际资源。

(If you are actually running tests on Azure DevOps Server, and not Azure DevOps Service, there might be some additional obstacles I'm not aware of - haven't used the on-premises version in a while) (如果您实际上是在 Azure DevOps 服务器上运行测试,而不是 Azure DevOps 服务,可能还有一些我不知道的其他障碍 - 有一段时间没有使用本地版本)

https://docs.microsoft.com/en-us/azure/devops/pipelines/library/connect-to-azure?view=azure-devops https://docs.microsoft.com/en-us/azure/cosmos-db/secure-access-to-data?tabs=using-primary-key https://docs.microsoft.com/en-us/azure/cosmos-db/role-based-access-control https://docs.microsoft.com/en-us/azure/cosmos-db/tutorial-setup-ci-cd https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-cli?view=azure-devops https://docs.microsoft.com/en-us/azure/devops/pipelines/library/connect-to-azure?view=azure-devops https://docs.microsoft.com/en-us/azure/cosmos -db/secure-access-to-data?tabs=using-primary-key https: //docs.microsoft.com/en-us/azure/cosmos-db/role-based-access-control https://docs .microsoft.com/en-us/azure/cosmos-db/tutorial-setup-ci-cd https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-cli?视图=天蓝色 devops

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM