16

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/

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

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

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)

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.

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

Update 17/12/2018

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.

Tangiest
  • 43,737
  • 24
  • 82
  • 113
  • Hi, great question, when you say looking for a PowerShell option that sounds like a manual trigger of some sort. Are you after a complete continuous deployment e.g. a deploy on each commit / merge to master etc. Or is it sufficient to have a manual trigger? – Alex KeySmith Nov 08 '18 at 10:07
  • Hi, is this for GitLab.com or a self-hosted GitLab server? Do you have a windows runner set up for this project? – antonyoni Nov 14 '18 at 13:16
  • Is this for a public or private repository? – The Fish Dec 11 '18 at 17:19
  • @TheFish did you find any solution for private repo? – Vrushal Raut Sep 01 '22 at 10:44

3 Answers3

4

It sounds like you are looking for a direct deploy from GitLab to Azure Apps, however I'd suggest using a deployment pipeline tool to give you far more options.

Azure DevOps Services Pipelines would likely be a safe option and has a free tier and here's a very brief getting started guide for Web Apps deploys.

However it doesn't have built in support for GitLab, but there appears to be a marketplace tool for integrations with GitLab.

This doesn't appear to have the capability of release triggers, but could be triggered manually. Someone else has the question about release triggers in the marketplace Q&A so perhaps it will be in the roadmap.

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • Hi @alex-keysmith, thank you for your answer. Unfortunately, the Azure DevOps Pipeline looks to be a rebranded way of setting up a manual CI setup, and as you mention, even with the GitLab Integration for Azure Pipelines tooling, doesn't yet support updates to Azure based on repository events. The real purpose of the question is to find a way of using (PowerShell) code to configure the automatic deployment of a GitLab repo to Azure. Unfortunately, your answer doesn't meet this requirement. – Tangiest Nov 12 '18 at 12:12
  • Thanks for the feedback @MagicAndi, when you say using PowerShell, that infers intending there to be a manual trigger. But it sounds like you looking for a repository trigger of some sort (pull request etc) to trigger a deployment? Unless are you thinking of hosting the PowerShell in an Azure Automation account perhaps? – Alex KeySmith Nov 12 '18 at 13:08
  • 1
    Have you considered a gitlab webhook? https://docs.gitlab.com/ee/user/project/integrations/webhooks.html which could then trigger a PowerShell Azure Automation runtbook (azure logic apps or functions to act as an intermediary if needed? – Alex KeySmith Nov 12 '18 at 13:10
4

Trying to reproduce your situation, I used the Azure CLI:

https://learn.microsoft.com/en-us/cli/azure/?view=azure-cli-latest

Which is ready to be used in a script in powershell like you want.

First, I tried to get all information about current deployment with a Bitbucket repository, which was configured as a Microsoft.Web/sites/sourcecontrols/Bitbucket:

az webapp deployment source show --name XXXXXXX --resource-group YYYYY

And got answer:

{
  "branch": "master",
  "deploymentRollbackEnabled": false,
  "id": "/subscriptions/00000/resourceGroups/YYYYY/providers/Microsoft.Web/sites/XXXXXXX/sourcecontrols/web",
  "isManualIntegration": false,
  "isMercurial": false,
  "kind": null,
  "location": "North Europe",
  "name": "XXXXXXX",
  "repoUrl": "https://bitbucket.org/myAccount/myRepo",
  "resourceGroup": "YYYYY",
  "type": "Microsoft.Web/sites/sourcecontrols"
}

I disconnected it, and configured it manually, specifying External Repository exactly like in your link, defining the SAME Git repository, and branch, and at end, I got almost the same result:

{
  "branch": "master",
  "deploymentRollbackEnabled": false,
  "id": "/subscriptions/00000/resourceGroups/YYYYY/providers/Microsoft.Web/sites/XXXXXXX/sourcecontrols/web",
  "isManualIntegration": true,
  "isMercurial": false,
  "kind": null,
  "location": "North Europe",
  "name": "XXXXXXX",
  "repoUrl": "https://bitbucket.org/myAccount/myRepo",
  "resourceGroup": "YYYYY",
  "tags": {
    "hidden-related:/subscriptions/00000/resourcegroups/YYYYY/providers/Microsoft.Web/serverfarms/XXXXXXX-sp": "empty"
  },
  "type": "Microsoft.Web/sites/sourcecontrols"
}

You can see the differences:

  • isManualIntegration is now True, instead of False
  • the hidden-related tag but I'm not sure it is interesting here

All that to say, you can script easily, using the official Azure Cli, the deployment definition, this way:

az webapp deployment source config --manual-integration --repository-type git --repo-url https://bitbucket.org/myAccount/myRepo --branch master --name XXXXXXX --resource-group YYYYY

Edit 15/11/2018:

Once this deployment setup done, each new commits push to the regarded branch (e.g. master in previous samples) will automatically trigger a new deployment.

To be noted that sometimes it takes few minutes for the deployment to be triggered.

Eventually, if ever you would like to automatically execute something (a Powershell command, a GNU/Bash script, an .py, .bat, or whatever), you can create a .deployment file in root of your repository.

For instance:

[config]
SCM_COMMAND_IDLE_TIMEOUT = 9000
command = bash.exe build_my_project_on_azure.sh

You can get more information in the official documentation.

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
  • Hi. As soon as the deployment is setup, the deployment will be automatically triggered when new commits are pushed in the regarded branch. I updated my answer to add this information + possibilities to automatically launch an executable if you need it. – Bsquare ℬℬ Nov 15 '18 at 12:26
  • @MagicAndi Does it reach your needs? – Bsquare ℬℬ Nov 16 '18 at 09:11
  • On Stackoverflow you could give [up-vote](https://stackoverflow.com/help/privileges/vote-up) to people's helpful answers to thank them and select any one of the answer as [correct answer](https://stackoverflow.com/help/someone-answers) too out of all. – Bsquare ℬℬ Dec 10 '18 at 10:32
2

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

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

The Fish
  • 1,039
  • 5
  • 6
  • Just to say thank you for answering. Enjoy the bounty. – Tangiest Dec 17 '18 at 10:54
  • 1
    Just to point out to anyone using this answer, it will only setup your web app with the code from the Git repository, but will not automatically deploy on further commits. This isn't a criticsim of the answer - it is a limitation of the PowerShell AzureRM module.There are 2 ways of overcoming this - either rebuild the web app using a different deployment slot to pull in any further commits, and then switch deployment slots (possible in a PowerShell script), or manually sync the latest commits in the Azure Portal's Deployment Center blade. – Tangiest Dec 19 '18 at 12:27
  • Hey @MagicAndi. This limitation seems to be a limitation of the platform rather than the cmdlets. The cmdlets are auto-generated using a swagger spec so they should match the API. They're essentially just a wrapper. This may not be possible in any language. I'll try and do some more digging. – The Fish Dec 21 '18 at 10:48