Azure Functions 的 ZIP 部署Zip deployment for Azure Functions

本文會說明如何從 .zip (壓縮) 檔案將函式應用程式的專案檔部署至 Azure。This article describes how to deploy your function app project files to Azure from a .zip (compressed) file. 您將了解如何藉由使用 Azure CLI 和使用 REST API 來進行推送部署。You learn how to do a push deployment, both by using Azure CLI and by using the REST APIs. Azure Functions Core Tools 也會在將本機專案發佈至 Azure 時,使用這些部署 API。Azure Functions Core Tools also uses these deployment APIs when publishing a local project to Azure.

Azure Functions 擁有 Azure App Service 所提供的全套持續部署與整合選項。Azure Functions has the full range of continuous deployment and integration options that are provided by Azure App Service. 如需詳細資訊,請參閱 Azure Functions 的持續部署For more information, see Continuous deployment for Azure Functions.

為了加快開發速度,您可能會發現直接從 .zip 檔案部署函式應用程式的專案檔會比較容易。To speed development, you may find it easier to deploy your function app project files directly from a .zip file. .zip 部署 API 會取得 .zip 檔案的內容,並將內容擷取至函式應用程式的 wwwroot 資料夾。The .zip deployment API takes the contents of a .zip file and extracts the contents into the wwwroot folder of your function app. 此 .zip 檔案部署所使用的是支援持續整合式部署的同一個 Kudu 服務,包括:This .zip file deployment uses the same Kudu service that powers continuous integration-based deployments, including:

  • 刪除較舊部署所留下的檔案。Deletion of files that were left over from earlier deployments.
  • 部署自訂,包括執行中的部署指令碼。Deployment customization, including running deployment scripts.
  • 部署記錄。Deployment logs.
  • 同步處理取用方案函式應用程式中的函式觸發程序。Syncing function triggers in a Consumption plan function app.

如需詳細資訊,請參閱 .zip 部署參考For more information, see the .zip deployment reference.

.zip 部署檔案需求Deployment .zip file requirements

您用於推送部署的 .zip 檔案必須包含執行函式所需的所有檔案。The .zip file that you use for push deployment must contain all of the files needed to run your function.

重要

當您使用 .zip 部署時,.zip 檔案中找不到的現有部署檔案,都會從函式應用程式中刪除。When you use .zip deployment, any files from an existing deployment that aren't found in the .zip file are deleted from your function app.

特定函式應用程式中所有函式的程式碼,都位於包含主機設定檔和一或多個子資料夾的根專案資料夾中。The code for all the functions in a specific function app is located in a root project folder that contains a host configuration file and one or more subfolders. 每個子資料夾都包含個別函數的程式碼。Each subfolder contains the code for a separate function. 下列的表示法,顯示資料夾結構:The folder structure is shown in the following representation:

FunctionApp
 | - host.json
 | - Myfirstfunction
 | | - function.json
 | | - ...  
 | - mysecondfunction
 | | - function.json
 | | - ...  
 | - SharedCode
 | - bin

在版本 2.x 的 Functions 執行階段,函式應用程式中的所有函式必須共用相同的語言堆疊。In version 2.x of the Functions runtime, all functions in the function app must share the same language stack.

Host.json檔案包含執行階段特定組態,並在函數應用程式根資料夾中。The host.json file contains runtime-specific configurations and is in the root folder of the function app. A bin資料夾包含封裝和其他函式應用程式所需的程式庫檔案。A bin folder contains packages and other library files that the function app requires. 請參閱函式應用程式專案以了解特定語言的需求:See the language-specific requirements for a function app project:

函式應用程式包含 wwwroot 目錄中所有檔案和資料夾。A function app includes all of the files and folders in the wwwroot directory. .zip 檔案部署包含 wwwroot 目錄的內容,但不是目錄本身。A .zip file deployment includes the contents of the wwwroot directory, but not the directory itself. 在部署 C# 類別庫專案時,您必須將已編譯的類別庫檔案和相依性納入到 .zip 套件中的 bin 子資料夾。When deploying a C# class library project, you must include the compiled library files and dependencies in a bin subfolder in your .zip package.

下載函式應用程式檔案Download your function app files

在本機電腦上進行開發時,您不用費多少功夫就能在開發電腦上建立函式應用程式專案資料夾的 .zip 檔案。When you are developing on a local computer, it's easy to create a .zip file of the function app project folder on your development computer.

不過,您可能已在 Azure 入口網站中使用編輯器建立好您的函式。However, you might have created your functions by using the editor in the Azure portal. 您可以使用下列其中一個方式來下載現有函式應用程式專案:You can download an existing function app project in one of these ways:

  • 從 Azure 入口網站:From the Azure portal:

    1. 登入 Azure 入口網站,然後移至您的函式應用程式。Sign in to the Azure portal, and then go to your function app.

    2. 在 [概觀] 索引標籤上,選取 [下載應用程式內容] 。On the Overview tab, select Download app content. 選取下載選項,然後選取 [下載] 。Select your download options, and then select Download.

      下載函式應用程式專案

      所下載的 .zip 檔案會採用正確格式,以便您可以使用 .zip 推送部署加以重新發佈至函式應用程式。The downloaded .zip file is in the correct format to be republished to your function app by using .zip push deployment. 入口網站下載也可新增直接在 Visual Studio 中開啟您函式應用程式所需的檔案。The portal download can also add the files needed to open your function app directly in Visual Studio.

  • 使用 REST API:Using REST APIs:

    使用下列部署 GET API 從您的 <function_app> 專案下載檔案:Use the following deployment GET API to download the files from your <function_app> project:

      https://<function_app>.scm.azurewebsites.net/api/zip/site/wwwroot/
    

    包括 /site/wwwroot/ 可確保您的 zip 檔案僅包含函式應用程式專案檔案,而不是整個站台。Including /site/wwwroot/ makes sure your zip file includes only the function app project files and not the entire site. 如果您尚未登入 Azure,系統會要求您登入。If you are not already signed in to Azure, you will be asked to do so.

您也可以從 GitHub 存放庫下載 .zip 檔案。You can also download a .zip file from a GitHub repository. 當您將 GitHub 存放庫下載為 .zip 檔案時,GitHub 會為分支新增額外的資料夾層級。When you download a GitHub repository as a .zip file, GitHub adds an extra folder level for the branch. 這個額外的資料夾層級表示,由於您是從 GitHub 下載 .zip 檔案,所以無法直接部署該檔案。This extra folder level means that you can't deploy the .zip file directly as you downloaded it from GitHub. 如果您使用 GitHub 存放庫來維護函式應用程式,請使用持續整合來部署應用程式。If you're using a GitHub repository to maintain your function app, you should use continuous integration to deploy your app.

使用 Azure CLI 進行部署Deploy by using Azure CLI

您可以使用 Azure CLI 來觸發推送部署。You can use Azure CLI to trigger a push deployment. 請使用 az functionapp deployment source config-zip 命令,將 .zip 檔案推送部署至函式應用程式。Push deploy a .zip file to your function app by using the az functionapp deployment source config-zip command. 若要使用此命令,您所使用的 Azure CLI 必須是 2.0.21 版或更新版本。To use this command, you must use Azure CLI version 2.0.21 or later. 若要了解您所使用的 Azure CLI 版本,請使用 az --version 命令。To see what Azure CLI version you are using, use the az --version command.

在下列命令中,使用 .zip 檔案的位置路徑取代 <zip_file_path> 預留位置。In the following command, replace the <zip_file_path> placeholder with the path to the location of your .zip file. 也請使用函式應用程式的唯一名稱取代 <app_name>Also, replace <app_name> with the unique name of your function app.

az functionapp deployment source config-zip  -g myResourceGroup -n \
<app_name> --src <zip_file_path>

此命令會將專案檔從所下載的 .zip 檔案部署到您在 Azure 中的函式應用程式。This command deploys project files from the downloaded .zip file to your function app in Azure. 然後重新啟動應用程式。It then restarts the app. 若要檢視此函式應用程式的部署清單,您必須使用 REST API。To view the list of deployments for this function app, you must use the REST APIs.

當您在本機電腦上使用 Azure CLI 時,<zip_file_path> 是 .zip 檔案在您電腦上的路徑。When you're using Azure CLI on your local computer, <zip_file_path> is the path to the .zip file on your computer. 您也可以在 Azure Cloud Shell 中執行 Azure CLI。You can also run Azure CLI in Azure Cloud Shell. 當您使用 Cloud Shell 時,必須先將部署 .zip 檔案上傳到與 Cloud Shell 相關聯的 Azure 檔案服務帳戶。When you use Cloud Shell, you must first upload your deployment .zip file to the Azure Files account that's associated with your Cloud Shell. 在此情況下,<zip_file_path> 會是 Cloud Shell 帳戶所使用的儲存體位置。In that case, <zip_file_path> is the storage location that your Cloud Shell account uses. 如需詳細資訊,請參閱在 Azure Cloud Shell 中保存檔案For more information, see Persist files in Azure Cloud Shell.

使用 REST API 部署 ZIP 檔案Deploy ZIP file with REST APIs

您可以使用部署服務 REST API,在 Azure 中將 .zip 檔案部署至您的應用程式。You can use the deployment service REST APIs to deploy the .zip file to your app in Azure. 若要部署,請將 POST 要求傳送至 https://<app_name>.scm.azurewebsites.net/api/zipdeploy。To deploy, send a POST request to https://<app_name>.scm.azurewebsites.net/api/zipdeploy. POST 要求必須在訊息本文中包含 .zip 檔案。The POST request must contain the .zip file in the message body. 系統會使用 HTTP 基本驗證,在要求中提供應用程式的部署認證。The deployment credentials for your app are provided in the request by using HTTP BASIC authentication. 如需詳細資訊,請參閱 .zip 推送部署參考For more information, see the .zip push deployment reference.

針對 HTTP 基本驗證,您需要 App Service 部署的認證。For the HTTP BASIC authentication, you need your App Service deployment credentials. 若要了解如何設定部署認證,請參閱設定及重設使用者層級的認證To see how to set your deployment credentials, see Set and reset user-level credentials.

使用 cURLWith cURL

下列範例會使用 cURL 工具來部署 .zip 檔案。The following example uses the cURL tool to deploy a .zip file. 取代預留位置 <username><password><zip_file_path><app_name>Replace the placeholders <username>, <password>, <zip_file_path>, and <app_name>. 當 cURL 顯示提示時,請輸入密碼。When prompted by cURL, type in the password.

curl -X POST -u <deployment_user> --data-binary @"<zip_file_path>" https://<app_name>.scm.azurewebsites.net/api/zipdeploy

此要求會觸發從上傳的 .zip 檔案推送部署。This request triggers push deployment from the uploaded .zip file. 您可以使用 https://<app_name>.scm.azurewebsites.net/api/deployments 端點來檢閱目前和過去的部署,如下列 cURL 範例所示。You can review the current and past deployments by using the https://<app_name>.scm.azurewebsites.net/api/deployments endpoint, as shown in the following cURL example. 再次使用您的應用程式名稱取代 <app_name>,以及使用部署認證的使用者名稱取代 <deployment_user>Again, replace <app_name> with the name of your app and <deployment_user> with the username of your deployment credentials.

curl -u <deployment_user> https://<app_name>.scm.azurewebsites.net/api/deployments

透過 PowerShellWith PowerShell

下列範例會使用new-azwebapp上傳 .zip 檔案。The following example uses Publish-AzWebapp upload the .zip file. 取代預留位置 <group-name><app-name><zip-file-path>Replace the placeholders <group-name>, <app-name>, and <zip-file-path>.

Publish-AzWebapp -ResourceGroupName <group-name> -Name <app-name> -ArchivePath <zip-file-path>

此要求會觸發從上傳的 .zip 檔案推送部署。This request triggers push deployment from the uploaded .zip file.

若要檢閱目前和過去的部署,請執行下列命令。To review the current and past deployments, run the following commands. 同樣地, 取代<deployment-user><deployment-password><app-name>預留位置。Again, replace the <deployment-user>, <deployment-password>, and <app-name> placeholders.

$username = "<deployment-user>"
$password = "<deployment-password>"
$apiUrl = "https://<app-name>.scm.azurewebsites.net/api/deployments"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$userAgent = "powershell/1.0"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET

從部署套件執行函式Run functions from the deployment package

您也可以選擇直接從部署套件檔案執行函式。You can also choose to run your functions directly from the deployment package file. 這個方法會略過將套件中的檔案複製到函式應用程式 wwwroot 目錄的部署步驟。This method skips the deployment step of copying files from the package to the wwwroot directory of your function app. 相反地,套件檔案會由 Functions 執行階段掛接,而且 wwwroot 目錄的內容會變成唯讀狀態。Instead, the package file is mounted by the Functions runtime, and the contents of the wwwroot directory become read-only.

ZIP 部署會與這項功能整合,藉由將函式應用程式的設定 WEBSITE_RUN_FROM_PACKAGE 設定為值 1 即可加以啟用。Zip deployment integrates with this feature, which you can enable by setting the function app setting WEBSITE_RUN_FROM_PACKAGE to a value of 1. 如需詳細資訊,請參閱從部署套件檔案執行函式For more information, see Run your functions from a deployment package file.

部署自訂Deployment customization

部署程序假設您所推送的 .zip 檔案包含隨時可執行的應用程式。The deployment process assumes that the .zip file that you push contains a ready-to-run app. 根據預設,系統不會執行任何自訂。By default, no customizations are run. 若要啟用您在持續整合所獲得的相同建置程序,請將下列內容新增至您的應用程式設定:To enable the same build processes that you get with continuous integration, add the following to your application settings:

SCM_DO_BUILD_DURING_DEPLOYMENT=true 

當您使用 .zip 推送部署時,這項設定的預設值是 falseWhen you use .zip push deployment, this setting is false by default. 持續整合部署的預設值則是 trueThe default is true for continuous integration deployments. 設定為 true 時,系統會在部署期間使用您的部署相關設定。When set to true, your deployment-related settings are used during deployment. 您可以將這些設定設定為應用程式設定或設定於 .deployment 設定檔 (位於 .zip 檔案的根目錄) 中。You can configure these settings either as app settings or in a .deployment configuration file that's located in the root of your .zip file. 如需詳細資訊,請參閱部署參考中的存放庫和部署相關設定For more information, see Repository and deployment-related settings in the deployment reference.

後續步驟Next steps