Deploy Code from GitLab Repository to Azure Web App using PowerShell

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


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.


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

https://docs.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.