简体   繁体   English

如何使用来自 Gitlab CI 运行器的 az cli 登录到 Azure?

[英]How to log in to Azure using az cli from a Gitlab CI runner?

I have generated a zip file of a Node.JS-based web app in Gitlab, and I am trying to deploy it as an Azure "web app" using az webapp deploy .我在 Gitlab 中生成了一个基于 Node.JS 的 Web 应用程序的 zip 文件,我正在尝试使用az webapp deploy将其部署为 Azure“Web 应用程序”。 This works fine on my local machine where I am logged in, but I can't for the life of me figure out how I can log in to Azure from the Gitlab runner, so that I can run that same command.这在我登录的本地计算机上运行良好,但我一生无法弄清楚如何从 Gitlab 运行程序登录到 Azure,以便我可以运行相同的命令。 I've tried:我试过了:

  • Using the Publish Profile (already need to be logged in for that!)使用发布配置文件(已经需要登录了!)
  • Creating a managed identity with roles on the app (but I don't have access to AD)在应用程序上创建具有角色的托管标识(但我无权访问 AD)
  • Creating the managed system identity in the app's "Identity" pane (can't find any associated password?!)在应用程序的“身份”窗格中创建托管系统身份(找不到任何关联的密码?!)
  • Generating a JWT token to store in Gitlab as described in this question (I don't have access to the App Registrations functionality)本问题所述,生成 JWT 令牌以存储在 Gitlab 中(我无权访问 App Registrations 功能)

I don't want to use Azure to rebuild the application using the webhook system, I already have a known-working ZIP package that I want to deploy.我不想使用 Azure 来使用 webhook 系统重建应用程序,我已经有一个要部署的已知工作 ZIP 包。 My only hangup is logging in.我唯一的挂断是登录。

How can I log in to Azure -- ie what incantations do I have to provide to az login -- from a Gitlab CI runner, in order to deploy my website from a zipped Gitlab artifact to the App Service?我如何从 Gitlab CI 运行器登录到 Azure(即我必须向az login提供什么咒语),以便将我的网站从压缩的 Gitlab 工件部署到应用服务?

(note: I am a teacher and trying to figure this out for my students; it is possible that I am working with a somehow-limited Azure but my local IT doesn't support us for this and of course neither will Microsoft.) (注意:我是一名教师,并试图为我的学生解决这个问题;我可能正在使用某种受限的 Azure,但我的本地 IT 不支持我们,当然微软也不支持。)

If you cannot access the app registrations as discussed in a similar question and have no federation configured, your only options are to use a username and password (eg a user's username and password to authenticate to AAD), use a device code flow, or self-host your GitLab runner on Azure with a managed identity.如果您无法访问在类似问题中讨论的应用程序注册并且没有配置联合,您唯一的选择是使用用户名和密码(例如,用户的用户名和密码来向 AAD 进行身份验证)、使用设备代码流或 self - 使用托管标识在 Azure 上托管 GitLab 运行器。

Using username and password使用用户名和密码

To use username is password is straightforward:使用用户名是密码很简单:

az login --tenant $YOUR_TENANT_ID -u $YOUR_USERNAME -p $YOUR_PASSWORD

However, this may not be possible if you normally do not login to Azure using a username and password (for example, you use OAuth or other federated login for the Azure portal and users have no passwords set).但是,如果您通常不使用用户名和密码登录到 Azure(例如,您对 Azure 门户使用 OAuth 或其他联合登录并且用户没有设置密码),则这可能是不可能的。 In which case, you will need to use the device code flow.在这种情况下,您将需要使用设备代码流。

Using device code使用设备代码

To use device code flow, you will need to monitor the job output, copy the URL shown, and login from your browser every time your job runs.要使用设备代码流,您需要监控作业输出,复制显示的 URL,并在每次作业运行时从浏览器登录。 In your job, add the following:在您的工作中,添加以下内容:

az login --tenant $YOUR_TENANT_ID --use-device-code

In the job output you will see a message similar to the following:在作业输出中,您将看到类似于以下内容的消息:

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code YOUR-CODE-WILL-APPEAR-HERE to authenticate要登录,请使用网络浏览器打开页面https://microsoft.com/devicelogin并输入代码 YOUR-CODE-WILL-APPEAR-HERE 进行身份验证

Copy the code from the message, open your browser to the device login page and enter the code to allow your job to proceed.复制消息中的代码,打开浏览器到设备登录页面并输入代码以允许您的工作继续进行。

Note: It is possible for organizations to disable this login method, in which case you will see an error when trying to login this way.注意:组织可以禁用此登录方法,在这种情况下,您在尝试以这种方式登录时会看到错误。

Self-hosting GitLab runner on Azure with a managed identity在 Azure 上使用托管标识自托管 GitLab 运行器

Lastly, if you're not able to use any of the above methods, you can deploy the GitLab runner to Azure itself as an application that uses a managed identity (for example on AKS, ACI, or on a VM with a managed identity).最后,如果您无法使用上述任何方法,您可以将 GitLab 运行器作为使用托管标识的应用程序部署到 Azure 本身(例如在 AKS、ACI 或具有托管标识的 VM 上) .

For example, you can configure a shell runner on an Azure VM.例如,您可以在 Azure VM 上配置 shell 运行器。 Azure VMs with a managed identity will not require az login to perform az cli commands.具有托管标识的 Azure VM 不需要az login来执行 az cli 命令。

Creating the managed system identity in the app's "Identity" pane (can't find any associated password?!)在应用程序的“身份”窗格中创建托管系统身份(找不到任何关联的密码?!)

The reason you can't find any associated password is because managed identities can only be leveraged from Azure services -- for example, Azure VMs using a managed identity are able to use az cli without logging in.找不到任何关联密码的原因是,只能从 Azure 服务中利用托管标识 - 例如,使用托管标识的 Azure VM 无需登录即可使用 az cli。

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

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