使用PowerShell部署到azure函数

Kri*_*shh 13 azure azure-functions

有什么办法,我可以使用PowerShell脚本部署到azure函数吗?CI不适用于我们,因为我们使用章鱼部署来部署到我们的所有生产服务.因此,如果有一种方法可以使用PowerShell脚本进行部署,那将是有益的.

谢谢!

Chr*_*SFT 10

您可以使用Kudu REST API将功能部署到Azure .您还可以在我们的模板库中看到一些代码/示例.在代码示例中,您可以看到我们的测试脚本如何调用Kudu Rest apis将zip部署到Function App.

函数的文件夹结构是每个文件夹的函数.您需要./site/wwwroot在Function App上部署Function文件夹.如果在更新之间添加任何新绑定,还需要添加可能包含您的机密的任何应用程序设置.

PowerShell代码看起来像是这样的:

    $apiUrl = $config.scmEndpoint + "/api/zip/"
    if ($destinationPath)
    {
        $apiUrl = $apiUrl + $destinationPath
    }

    $response = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $config.authInfo)} -Method PUT -InFile $zipFilePath -ContentType "multipart/form-data"
Run Code Online (Sandbox Code Playgroud)


rag*_*710 8

以防万一像我这样的人需要一步一步的解决方案.以下是使用powershell部署azure函数的过程(非ARM方式)

  1. 使用以下结构创建azure函数

    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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建myFunctionName文件夹的zip 文件 - 让我们为它命名my.zip.压缩my.zip包含myFunctionName文件夹及其所有内容后确保

  3. 寻找你的发布配置文件的用户名和密码作为描述在这里,即

    $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
Run Code Online (Sandbox Code Playgroud)

然后使用powershell调用Kudu REST API,如下所示

    $username = '<publish username>' #IMPORTANT: use single quotes as username may contain $
    $password = "<publish password>"
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

    $apiUrl = "https://<yourFunctionApp>.scm.azurewebsites.net/api/zip/site/wwwroot"
    $filePath = "<yourFunctionName>.zip"
    Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $filePath -ContentType "multipart/form-data"
Run Code Online (Sandbox Code Playgroud)
  1. 去吧<yourFunctionApp>.scm.azurewebsites.net -> Debug menu at the top -> CMD.在显示的页面中,导航至site -> wwwroot.您应该看到在那里提取的zip文件的内容,您还可以验证您的azure功能在azure门户中是否可用.

参考

  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


Dav*_*bbo 6

除了Chris描述的内容之外,还有一个可用于部署函数的第一类ARM API.以下是PowerShell中的内容:

Function DeployHttpTriggerFunction($ResourceGroupName, $SiteName, $FunctionName, $CodeFile, $TestData)
{
    $FileContent = "$(Get-Content -Path $CodeFile -Raw)"

    $props = @{
        config = @{
            bindings = @(
                @{
                    type = "httpTrigger"
                    direction = "in"
                    webHookType = ""
                    name = "req"
                }
                @{
                    type = "http"
                    direction = "out"
                    name = "res"
                }
            )
        }
        files = @{
            "index.js" = $FileContent
        }
        test_data = $TestData
    }

    New-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/functions -ResourceName $SiteName/$FunctionName -PropertyObject $props -ApiVersion 2015-08-01 -Force
}
Run Code Online (Sandbox Code Playgroud)

有关底层API的信息,请参阅https://github.com/projectkudu/kudu/wiki/Functions-API.


Cha*_*ani 5

您还可以使用Azure CLI 2.0 + Azure Function CLI 从命令行/powershell 部署 Azure 函数

Azure CLI API 可用于预配函数应用,使用命令

az functionapp create --name $functionappName --resource-group $resourceGroup --storage-account $storageAccountName --consumption-plan-location $consumptionPlanLocation
Run Code Online (Sandbox Code Playgroud)

并应用应用程序设置

az functionapp config appsettings set --name $functionappName --resource-group $resourceGroup --settings "test=value"
Run Code Online (Sandbox Code Playgroud)

并且 Azure Function CLI api 可用于部署您拥有的功能

func azure functionapp publish <azurefunctionapp>
Run Code Online (Sandbox Code Playgroud)

好用的工具!