本文介绍了使用 PowerShell 将代码从 GitLab 存储库部署到 Azure Web App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 PowerShell 脚本设置从 GitLab 存储库到 Azure 应用程序的持续部署.我知道您可以按照以下方式手动执行此操作:

I would like to setup continuous deployment from a GitLab repository to an Azure App using a PowerShell script. I'm aware that you can do this manually as per:

https://christianliebel.com/2016/05/auto-deploying-to-azure-app-services-from-gitlab/

但是,我正在尝试使用 Powershell 自动执行此操作.我查看了 GitHub 的这个示例脚本:

However, I'm trying to automate this with Powershell. I've looked at this sample script for GitHub:

https://docs.microsoft.com/en-us/azure/app-service/scripts/app-service-powershell-continuous-deployment-github

但是由于 GitLab 没有提供者,并且现有的提供者都没有接受 GitLab URL,所以我不确定如何继续.我查看了在 Azure 门户中使用 GitLab 设置手动部署(使用外部存储库选项)并导出资源组模板以获取存储库如何连接到应用程序的详细信息,但我收到错误:

But as there is no provider for GitLab, and none of the existing providers accept a GitLab URL, I'm unsure of how to proceed. I've looked at setting up a manual deployment with GitLab in the Azure Portal (using the External Repository option) and exporting the resource group template to get details of how the repository is connected to the App, but I get the error:

Could not get resources of the type 'Microsoft.Web/sites/sourcecontrols'.
Resources of this type will not be exported. (Code: ExportTemplateProviderError, Target: Microsoft.Web/sites/sourcecontrols)

目前,我正在解决这个问题,方法是在 GitHub 中镜像我的 GitLab 存储库,并使用从那里到 Azure 的持续部署管道.请注意,这是针对托管在 GitLab.com 中的存储库,而不是在自托管 GitLab 服务器中.该项目没有设置 Windows Runner.

At the minute, I'm working around this by mirroring my GitLab repository in GitHub, and using the continuous deployment pipeline from there to Azure. Note, this is for a repository hosted in GitLab.com, not in a self-hosted GitLab server. There is no Windows Runner setup for the project.

如何使用 PowerShell 脚本直接从 GitLab 到 Azure 设置持续部署?运行设置脚本后,对 GitLab 存储库的每个后续提交/合并都应自动部署到 Azure.最好,这个 PowerShell 脚本应该使用 AzureRM 模块,但我愿意接受使用 PowerShell Core 和新的 Az 模块(基于 Azure CLI)的解决方案.我正在使用的特定测试存储库是公开的(https://gitlab.com/MagicAndi/geekscode.net),但这并不是解决方案与私有存储库一起工作的具体要求(但如果可以,那就更好了!).

How can I use a PowerShell script to setup a Continuous Deployment directly from GitLab to Azure? Once the setup script is run, each subsequent commit/merge to the GitLab repository should then automatically be deployed to Azure. Preferably, this PowerShell script should use the AzureRM modules, but I'm willing to accept a solution that uses PowerShell Core and the new Az module (based on the Azure CLI). The specific test repository I'm using is public (https://gitlab.com/MagicAndi/geekscode.net), but it isn't a specific requirement for the solution to work with private repositories (but if it does, even better!).

2018 年 17 月 12 日更新

我已将赏金授予 the-fish,因为他的回答最能满足我的特定需求.但是,鉴于 Windows Powershell 和 Azure RM 模块已被弃用,取而代之的是 PowerShell Core 和新的 Az 模块(使用 Azure CLI),我创建了一个 新问题专门询问使用 Azure CLI 和 Powershell 的规范答案核.我计划在 2 天内向我开放这个问题时提供赏金.谢谢.

I've awarded the bounty to the-fish as his answer best met my specific needs. However, given that Windows Powershell and the Azure RM module are being deprecated in favour of PowerShell Core and the new Az module (using the Azure CLI), I've created a new question asking specificially for a canonical answer using the Azure CLI and Powershell Core. I plan on offering a bounty for this question when it is open to me in 2 days. Thanks.

推荐答案

假设存储库是公开的,您可以使用以下内容:

Assuming the repository is public you could use the following:

$gitrepo="<replace-with-URL-of-your-own-repo>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"

# Create a resource group.
New-AzureRmResourceGroup -Name myResourceGroup -Location $location

# Create an App Service plan in Free tier.
New-AzureRmAppServicePlan -Name $webappname -Location $location `
    -ResourceGroupName myResourceGroup -Tier Free

# Create a web app.
New-AzureRmWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
    -ResourceGroupName myResourceGroup

# Configure deployment from your repo and deploy once.
$PropertiesObject = @{
    repoUrl = "$gitrepo";
    branch = "master";
    isManualIntegration = $true
}
Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName myResourceGroup `
    -ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webappname/web `
    -ApiVersion 2018-02-01 -Force

如果它是私人的,请告诉我,这可能会更困难.如果您查看 CLI 源代码,您会发现它们目前仅支持 GitHub 的访问令牌.

Let me know if it's private, that may be more difficult. If you look at the CLI source you can see they currently only support access tokens with GitHub.

https://github.com/Azure/azure-cli/blob/4f87cb0d322c60ecc6449022467bd580a0accd57/src/command_modules/azure-cli-appservice/azure/cli/command_modules/appservice/custom.py#L964-L1027

这篇关于使用 PowerShell 将代码从 GitLab 存储库部署到 Azure Web App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:39