How to Sync an Azure AppService git based from Powershell script? How to Sync an Azure AppService git based from Powershell script? powershell powershell

How to Sync an Azure AppService git based from Powershell script?


Having just come across a similar situation myself, it seems this is the solution (or at least, a solution)

If you initially delete the repo, and then re-add it, it will - obviously - force a resync.

Remove-AzureRmResource -ResourceGroupName $AppServiceResourceGroupName `                -ResourceType Microsoft.Web/sites/SourceControls `                -Name $AppServiceWebAppName/Web `                -ApiVersion 2015-08-01 `                -Force$props = @{    RepoUrl = "https://github.com/{account}/{repo}"    Branch = "master"    isManualIntegration = "false" }########## -- Configure Source Control --##########New-AzureRmResource -ResourceGroupName $AppServiceResourceGroupName `                -ResourceType Microsoft.Web/sites/SourceControls `                -Name $AppServiceWebAppName/Web `                -PropertyObject $props `                -ApiVersion 2015-08-01 `                -Force


What the Sync button does is git pull from BitBucket to Azure. You can accomplish the same thing by pushing to Azure directly.

First, setup:

  1. Copy the Git clone url in the Azure Portal (back up two blades to the main web app portal page). Mine looks like https://robrich@thedescriptivename.scm.azurewebsites.net:443/thedescriptivename.git
  2. Set / get Git deployment credentials. Go to the "Deployment Credentials" blade in the Azure site, and set the credentials you'd like to use. If you've done this previously (which I think you have) then you can just use the credentials you set previously.
  3. Open a command prompt or powershell in the project's working directory (where the .git folder is).
  4. Add a second remote (like origin) named "azure": git remote add azure https://{that_url_you_copied_above}.

Now let's experiment from the command line:

  1. Change and commit something that isn't pushed to Jenkins.
  2. Open a command prompt or powershell in the project's working directory (where the .git folder is). This can be the same cmd you used above.
  3. Push the changes: git push origin azure.
  4. It'll invite you to login if you haven't pushed this way before. Use the credentials you created / remembered from above.
  5. Watch the portal notice a new change, and deploy the new code.

Now that it works, let's add it to powershell:

  1. git push origin azure

Note that you'll either need to modify the URL above to include the password or find another way to authenticate from within the script -- you probably don't want the pop-up in the middle of each run.

Why is this better or worse? You just jumped around Jenkins, so you're not running your test suite before deploying. You are deploying much faster though. Weigh these carefully.


Here's how to mimic the "Sync" button with an API call*.Site hooked up with "External Repository" deployment method.

Request:

POST /deploy HTTP/1.1Host: $SiteLevelUsername:SiteLevelPassword@WebAppName.scm.azurewebsites.netContent-Type: application/jsonAccept: application/jsonX-SITE-DEPLOYMENT-ID: WebAppNameCache-Control: no-cache{    "format":"basic",    "url":"https://username:password@example.com/git/reponame.git"} 

You’ll also need a Content-Length header in there. You can get away with Transfer-encoding: chunked instead, if in a hurry.

Response (empty body):

200 OK

* Source: I figured it out looking at https://github.com/projectkudu/kudu/blob/master/Kudu.Services/ServiceHookHandlers/GenericHandler.cs and a Kudu trace of a successful "Sync" from the portal.

D:\home\LogFiles\kudu\trace>head 2016-04-08T10-43-27_2a1598_086_POST_deploy_200_3s.xml<step title="Incoming Request" date="2016-04-08T10:43:27.636" instance="2a1598" url="/deploy?scmType=ExternalGit" method="POST" type="request"pid="35604,2,68" Connection="Keep-Alive" Content-Length="107"Content-Type="application/json; charset=utf-8" Accept="application/json"Accept-Language="en-US" Expect="100-continue"Host="WebAppName.scm.azurewebsites.net"User-Agent="Azure-Portal/5.16.00298.15"x-ms-client-request-id="xxxxxxxxxxxx"x-ms-client-session-id="xxxxxxxxxxx"X-SITE-DEPLOYMENT-ID="WebAppName"

Same request with Azure PowerShell:

# Action syncInvoke-AzureRmResourceAction -ResourceGroupName <ResourceGroupName> `                             -ResourceType Microsoft.Web/sites `                             -ResourceName <WebAppName> `                             -Action sync `                             -ApiVersion 2015-08-01 `                             -Force -Verbose# Expected output:# ----------------# VERBOSE: Performing the operation "Invoking the 'sync' action# on the resource." on target # subscriptions/xx-xx-xx-xx/resourceGroups/xxxx/providers/Microsoft.Web/sites/xxxx".

and curl:

:: cmd.exe uses ^ (caret) to escape new lines (the equivalent of \ in bash)C:\>curl -k -v https://$siteLevelUsername:SiteLevelPassword@WebAppName.scm.azurewebsites.net/deploy ^     -H "Transfer-encoding: chunked" -H "X-SITE-DEPLOYMENT-ID: WebAppName" ^     -H "Content-type: application/json" -H "Accept: application/json" ^     --data-ascii "{ \"format\":\"basic\", \"url\":\"http://user:pass@example.com/git/repo.git\" }"> POST /deploy HTTP/1.1> Authorization: Basic xxxxxxxxxxxxxxxxxxx=> User-Agent: curl/7.28.1> Host: WebAppName.scm.azurewebsites.net> Transfer-encoding: chunked> X-SITE-DEPLOYMENT-ID: WebAppName> Content-type: application/json> Accept: application/json>> 66* upload completely sent off: 109 out of 102 bytes< HTTP/1.1 200 OK< Cache-Control: private< Content-Length: 0