Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy Code from GitLab Repository to Azure Web App using PowerShell

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

like image 801
Tangiest Avatar asked Oct 05 '18 11:10

Tangiest


People also ask

How do I manually deploy to Azure app?

Deploy by using an FTP client. In the Azure portal, download the publish profile for the web app that you want to deploy your code to. Then, upload the files to \site\wwwroot by using the same publish profile FTP credentials.


2 Answers

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.

like image 61
Alex KeySmith Avatar answered Oct 05 '22 22:10

Alex KeySmith


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.

like image 23
Bsquare ℬℬ Avatar answered Oct 05 '22 23:10

Bsquare ℬℬ