使用PowerShell将代码从GitLab存储库部署到Azure Web App [英] Deploy Code from GitLab Repository to Azure Web App using PowerShell

查看:101
本文介绍了使用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/zh-CN/azure/app-service/scripts/app-service-powershell-continuous-deployment-github

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

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!).

更新17/12/2018

我已将奖励授予 the-fish ,因为他的回答最能满足我的特定需求.但是,鉴于不赞成使用Windows Powershell和Azure RM模块,而使用PowerShell Core和新的Az模块(使用Azure CLI),我创建了

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.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆