Deploy to azure functions using powershell

前端 未结 5 2165
一生所求
一生所求 2021-02-07 10:20

Is there any way, I can deploy to azure functions using powershell scripts? CI will not work for us because we use octopus deploy to deploy to all of our production services. So

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 10:46

    Just in case there are people like me who need step by step solutions. Here is the procedure to deploy azure functions using powershell (non ARM way)

    1. Create an azure function with the following structure

      myFunctionName(Folder)
      |
      |_ function.json (contains info on input, output, trigger)
      |_ run.csx (contains code)
      |_ [OPTIONAL] project.json (for nuget packages)
      |_ [OPTIONAL] bin(Folder)
         |_ all custom DLLs go in this folder
      
    2. Create a zip of the myFunctionName folder - let's name it my.zip. Make sure that after zipping my.zip contains the myFunctionName folder and all its contents

    3. Find your publish profile username and password as described here, namely

        $creds = Invoke-AzureRmResourceAction -ResourceGroupName YourResourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName YourWebApp/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
    
        $username = $creds.Properties.PublishingUserName
        $password = $creds.Properties.PublishingPassword
    

    and then invoke the Kudu REST API using powershell as follows

        $username = '' #IMPORTANT: use single quotes as username may contain $
        $password = ""
        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    
        $apiUrl = "https://.scm.azurewebsites.net/api/zip/site/wwwroot"
        $filePath = ".zip"
        Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $filePath -ContentType "multipart/form-data"
    
    1. Go to .scm.azurewebsites.net -> Debug menu at the top -> CMD. In the page that appears, navigate to site -> wwwroot. You should see the contents of your zip file extracted there and you can also verify that your azure function is available in the azure portal.

    REFERENCES

    1. https://github.com/projectkudu/kudu/wiki/REST-API#sample-of-using-rest-api-with-powershell

    2. http://markheath.net/post/deploy-azure-functions-kudu-powershell

提交回复
热议问题