使用 GitHub Actions 將工作流程自動化,並從 GitHub 部署至 Azure App Service 。
先決條件
- 具有有效訂用帳戶的 Azure 帳戶。
免費建立帳戶。
- GitHub 帳戶。 如果您沒有帳戶,請免費註冊。
建立應用程式時設定 GitHub Actions 部署
GitHub 動作部署已整合至預設的建立 Web 應用程式處理序。 在 [部署] 索引標籤中將 [持續部署] 設定為 [啟用],然後設定您選擇的組織、存放庫和分支。
當您啟用持續部署時, 建立 Web 應用程式 程式會根據基本驗證選項自動挑選驗證方法,並據以設定您的應用程式和 GitHub 存放庫:
透過部署中心設定 GitHub Actions 部署
針對現有的應用程式,您可以使用 App Service 中的 部署中心 來快速開始使用 GitHub Actions。 此統包方法會根據您的應用程式堆疊產生 GitHub Actions 工作流程檔案,並將其提交至您的 GitHub 儲存庫。
透過使用 部署中心,您也可以使用 使用者指派的身分識別,輕鬆地配置更安全的 OpenID Connect 驗證。 如需詳細資訊,請參閱使用者指派的身分識別選項。
如果 Azure 帳戶具有所需的權限,您可以建立使用者指派的身分識別。 否則,您可以在 [身分識別] 下拉式功能表中選取現有使用者指派的受控識別。 您可以與 Azure 管理員合作,使用網站參與者角色建立使用者指派的受控識別。
如需詳細資訊,請參閱持續部署至 Azure App Service。
手動設定 GitHub Actions 工作流程
您可以在不使用 部署中心的情況下部署工作流程。 執行以下三個步驟:
-
產生部署認證。
-
設定 GitHub 密碼。
- 將工作流程檔案新增至 GitHub 存放庫。
產生部署認證
建議您使用 OpenID Connect 向 Azure App Service for GitHub Actions 進行驗證。 此驗證方法使用短期權杖。 使用 GitHub Actions 設定 OpenID Connect 是更複雜的作法,但安全性更高。
您也可以使用使用者指派的受控身分識別、服務主體或發佈設定文件進行驗證。
下列程序說明使用 Azure CLI 陳述式建立 Microsoft Entra 應用程式、服務主體和同盟認證的步驟。 若要瞭解如何在 Azure 入口網站中建立 Microsoft Entra 應用程式、服務主體和同盟認證,請參閱 連線 GitHub 和 Azure。
若您沒有現有的應用程式,請註冊可存取資源的新 Microsoft Entra 應用程式與服務主體。 建立 Microsoft Entra 應用程式。
az ad app create --display-name myApp
此命令會傳回 JSON 輸出,其中包含 appId,即為您的 client-id。 儲存該值,以便稍後作為 AZURE_CLIENT_ID GitHub 祕密。
當您使用 Graph API 建立同盟認證時,您會使用該 objectId 值,並將它參考為 APPLICATION-OBJECT-ID。
建立服務主體。 請將 JSON 輸出中的 $appID 替換為 appId。
此命令會產生 JSON 輸出,其中包含不同的 objectId 以便在下一步中使用。 新的 objectId 是 assignee-object-id。
複製 appOwnerTenantId 稍後用作 AZURE_TENANT_ID 的 GitHub 秘密。
az ad sp create --id $appId
依訂閱與物件建立新的角色指派。 根據預設,角色指派將會繫結至預設訂用帳戶。 以您的訂用帳戶識別碼取代 $subscriptionId,以您的資源群組名稱取代 $resourceGroupName,以您的 Web 應用程式名稱取代 $webappName,並以產生的 $assigneeObjectId 取代 id。 了解如何使用 Azure CLI 管理 Azure 訂閱。
az role assignment create --role "Website Contributor" --subscription $subscriptionId --assignee-object-id $assigneeObjectId --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Web/sites/$webappName --assignee-principal-type ServicePrincipal
執行下列命令,為您的 Microsoft Entra 應用程式 建立新的同盟身分識別認證 。
以您在建立應用程式期間為 Active Directory 應用程式產生的 APPLICATION-OBJECT-ID 取代 appId。
將 CREDENTIAL-NAME 的值設定為稍後參考。
設定 subject。 GitHub 會根據您的工作流程定義其值:
- 針對 GitHub Actions 環境中的作業,請使用:
repo:< Organization/Repository >:environment:< Name >
- 針對未繫結至環境的工作,請根據用來觸發工作流程的參考路徑,包含分支/標記的參考路徑:
repo:< Organization/Repository >:ref:< ref path>。 例如,repo:n-username/ node_express:ref:refs/heads/my-branch 或 repo:n-username/ node_express:ref:refs/tags/my-tag。
- 對於提取要求事件所觸發的工作流程,請使用:
repo:< Organization/Repository >:pull_request。
az ad app federated-credential create --id <APPLICATION-OBJECT-ID> --parameters credential.json
("credential.json" contains the following content)
{
"name": "<CREDENTIAL-NAME>",
"issuer": "https://token.actions.githubusercontent.com",
"subject": "repo:organization/repository:ref:refs/heads/main",
"description": "Testing",
"audiences": [
"api://AzureADTokenExchange"
]
}
發行設定檔是應用程式層級認證。 將發行設定檔設定為 GitHub 祕密。
移至 Azure 入口網站中的 App Service。
在 [概觀] 頁面上,選取 [下載發行設定檔]。
儲存下載的檔案。 使用檔案的內容來建立 GitHub 祕密。
附註
自 2020 年 10 月起,Linux Web 應用程式需要將應用程式設定 WEBSITE_WEBDEPLOY_USE_SCM 設為 true,才能下載發行設定檔。
您可以使用 Azure CLI 中的命令來az ad sp create-for-rbac建立服務主體。 在 Azure 入口網站中使用 Azure Cloud Shell 或選取 [開啟 Cloud Shell] 來執行此命令。
az ad sp create-for-rbac --name "myApp" --role "Website Contributor" \
--scopes /subscriptions/<subscription-id>/resourceGroups/<group-name>/providers/Microsoft.Web/sites/<app-name> \
--json-auth
在上述範例中,將預留位置取代為訂用帳戶識別碼、資源群組名稱與應用程式名稱。 輸出是具有角色指派認證的 JSON 物件,可提供 App Service 應用程式的存取權。 輸出看起來應該類似下列 JSON 程式碼片段。 複製此 JSON 物件以供後續使用。
{
"clientId": "<GUID>",
"clientSecret": "<GUID>",
"subscriptionId": "<GUID>",
"tenantId": "<GUID>",
(...)
}
重要事項
建議您授與最低存取權。 上一個範例中的範圍限制為特定 App Service 應用程式,而不是整個資源群組。
您必須提供應用程式的用戶端識別碼、租用戶識別碼和訂用帳戶識別碼Azure/login給動作。 這些值可以直接在工作流程中提供,也可以儲存於 GitHub 祕密中,並在您的工作流程中參考。 將值儲存為 GitHub 祕密是較安全的選項。
開啟您的 GitHub 存放庫,並移至 [設定]> [安全性]> [祕密和變數]> [動作]> [新存放庫袐密]。
建立 AZURE_CLIENT_ID、AZURE_TENANT_ID 與 AZURE_SUBSCRIPTION_ID 的祕密。 使用 Active Directory 應用程式中的這些值來取得 GitHub 祕密:
| GitHub 祕密 |
Active Directory 應用程式 |
AZURE_CLIENT_ID |
應用程式 (用戶端) 識別碼 |
AZURE_TENANT_ID |
目錄 (租用戶) 識別碼 |
AZURE_SUBSCRIPTION_ID |
訂用帳戶識別碼 |
選取 [ 新增密碼 ] 以儲存每個密碼。
在 GitHub (英文) 中瀏覽您的存放庫。 選取 [設定]> [安全性]> [祕密和變數]> [動作]> [新存放庫祕密]。
若要使用您在上一節中建立的應用程式層級認證,請將下載的發佈設定檔檔案的內容貼到密碼的值欄位中。 將祕密命名為 AZURE_WEBAPP_PUBLISH_PROFILE。
當您稍後設定 GitHub 工作流程檔案時,請使用 AZURE_WEBAPP_PUBLISH_PROFILE 在 部署 Azure Web 應用程式 的動作。 例如:
- uses: azure/webapps-deploy@v2
with:
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
在 GitHub (英文) 中瀏覽您的存放庫。 選取 [設定]> [安全性]> [祕密和變數]> [動作]> [新存放庫祕密]。
若要使用您在上一節中建立的使用者層級認證,請將 Azure CLI 命令的整個 JSON 輸出貼到秘密的值欄位中。 將祕密命名為 AZURE_CREDENTIALS。
當您稍後設定 GitHub 工作流程檔案時,會將祕密用於 creds 的輸入 Azure/login。 例如:
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
將工作流程檔案新增至 GitHub 存放庫
GitHub 存放庫的 /.github/workflows/ 路徑中的 YAML (. yml) 檔案會定義工作流程。 此定義包含組成工作流程的各種步驟與參數。
工作流程檔案至少具有下列不同的步驟:
- 使用您建立的 GitHub 秘密,向 App Service 進行驗證。
- 建置 Web 應用程式。
- 部署 Web 應用程式。
若要將程式碼部署至 App Service 應用程式,請使用 動作 azure/webapps-deploy@v3 。 此動作需要提供您的 Web 應用程式app-name的名稱,並且(視您的語言堆疊而定),也需提供要部署的 *.zip、*.war、*.jar 或資料夾的路徑package。 如需 azure/webapps-deploy@v3 動作可能輸入的完整清單,請參閱 action.yml。
下列範例顯示工作流程的一部分,這部分會以不同支援的語言建置 Web 應用程式。
若要使用您設定的受控身分識別來部署 OpenID Connect,請使用 azure/login@v2 動作搭配 client-id、tenant-id 和 subscription-id 金鑰。 參考您稍早建立的 GitHub 祕密。
name: .NET Core
on: [push]
permissions:
id-token: write
contents: read
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
DOTNET_VERSION: '6.0.x' # Set this to the dot net version to use
jobs:
build:
runs-on: ubuntu-latest
steps:
# Check out the repo
- uses: actions/checkout@main
- uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# Setup .NET Core SDK
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
# Run dotnet build and publish
- name: dotnet build and publish
run: |
dotnet restore
dotnet build --configuration Release
dotnet publish -c Release --property:PublishDir='${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp'
# Deploy to Azure Web apps
- name: 'Run Azure webapp deploy action using publish profile credentials'
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp'
- name: logout
run: |
az logout
使用 Azure 服務主體建置 ASP.NET 模型檢視控制器 (MVC) 應用程式並將其部署至 Azure。 此範例會針對 client-id、tenant-id 和 subscription-id 值使用 GitHub 祕密。 您也可以直接將這些值傳至登入動作中。
name: Deploy ASP.NET MVC App deploy to Azure Web App
on: [push]
permissions:
id-token: write
contents: read
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
NUGET_VERSION: '5.3.x' # Set this to the dot net version to use
jobs:
build-and-deploy:
runs-on: windows-latest
steps:
# Check out the repo
- uses: actions/checkout@main
- uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Install Nuget
uses: nuget/setup-nuget@v1
with:
nuget-version: ${{ env.NUGET_VERSION}}
- name: NuGet to restore dependencies as well as project-specific tools that are specified in the project file
run: nuget restore
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
- name: Run MSBuild
run: msbuild .\SampleWebApplication.sln
- name: 'Run Azure webapp deploy action using publish profile credentials'
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/SampleWebApplication/'
# Azure logout
- name: logout
run: |
az logout
使用 Azure 服務主體建置 Java Spring Boot 應用程式並將其部署至 Azure。 此範例會針對 client-id、tenant-id 和 subscription-id 值使用 GitHub 祕密。 您也可以直接將這些值傳至登入動作中。
name: Java CI with Maven
on: [push]
permissions:
id-token: write
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Set up JDK 1.8
uses: actions/setup-java@v3
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml
working-directory: complete
- name: Azure WebApp
uses: Azure/webapps-deploy@v3
with:
app-name: my-app-name
package: my/target/*.jar
# Azure logout
- name: logout
run: |
az logout
name: Build and deploy WAR app to Azure Web App using OpenID Connect
env:
JAVA_VERSION: '11' # Set this to the Java version to use
DISTRIBUTION: microsoft # Set this to the Java distribution
AZURE_WEBAPP_NAME: sampleapp # Set this to the name of your web app
on: [push]
permissions:
id-token: write
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Java version
uses: actions/setup-java@v3.0.0
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.DISTRIBUTION }}
cache: 'maven'
- name: Build with Maven
run: mvn clean install
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: '*.war'
以下是使用多個作業進行建置和部署的完整 範例 。
重要事項
如果你要用 TypeScript 或其他編譯語言 azure/webapps-deploy@v3部署 Node.js 應用程式,請先在 GitHub Actions 建置你的應用程式,然後部署編譯好的輸出資料夾(例如 dist/ 或 build/)。 如果你想改用 App Service 建置自動化,請 將 SCM_DO_BUILD_DURING_DEPLOYMENT 應用程式設定 設為 true。
name: JavaScript CI
on: [push]
permissions:
id-token: write
contents: read
name: Node.js
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: 'my-app-path' # Set this to the path to your web app project, defaults to the repository root
NODE_VERSION: '24.x' # Set this to the node version to use
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Check out the repo
- name: 'Checkout GitHub Action'
uses: actions/checkout@main
- uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Setup Node ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: 'npm install, build, and test'
run: |
npm install
npm run build --if-present
npm run test --if-present
working-directory: my-app-path
# Deploy web app by using Azure credentials
- uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
# Azure logout
- name: logout
run: |
az logout
name: Python application
on:
[push]
permissions:
id-token: write
contents: read
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: 3.x
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Deploy web App using GH Action azure/webapps-deploy
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
- name: logout
run: |
az logout
publish-profile 輸入應該參考您稍早建立的 AZURE_WEBAPP_PUBLISH_PROFILE GitHub 祕密。
name: .NET Core CI
on: [push]
env:
AZURE_WEBAPP_NAME: my-app-name # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
DOTNET_VERSION: '6.0.x' # Set this to the dot net version to use
jobs:
build:
runs-on: ubuntu-latest
steps:
# Check out the repo
- uses: actions/checkout@main
# Setup .NET Core SDK
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
# Run dotnet build and publish
- name: dotnet build and publish
run: |
dotnet restore
dotnet build --configuration Release
dotnet publish -c Release --property:PublishDir='${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp'
# Deploy to Azure Web apps
- name: 'Run Azure webapp deploy action using publish profile credentials'
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} # Define secret variable in repository settings as per action documentation
package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp'
建置和部署使用 NuGet 和 publish-profile 進行驗證的 ASP.NET 模型檢視控制器 (MVC) 應用程式。
name: Deploy ASP.NET MVC App deploy to Azure Web App
on: [push]
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
NUGET_VERSION: '5.3.x' # Set this to the dot net version to use
jobs:
build-and-deploy:
runs-on: windows-latest
steps:
- uses: actions/checkout@main
- name: Install Nuget
uses: nuget/setup-nuget@v1
with:
nuget-version: ${{ env.NUGET_VERSION}}
- name: NuGet to restore dependencies as well as project-specific tools that are specified in the project file
run: nuget restore
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
- name: Run MSBuild
run: msbuild .\SampleWebApplication.sln
- name: 'Run Azure webapp deploy action using publish profile credentials'
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} # Define secret variable in repository settings as per action documentation
package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/SampleWebApplication/'
使用 Azure 發佈設定檔,建置 Java Spring Boot 應用程式並將其部署至 Azure。 輸入 publish-profile 會參考您稍早建立的 AZURE_WEBAPP_PUBLISH_PROFILE 祕密。
name: Java CI with Maven
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 1.8
uses: actions/setup-java@v3
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml
working-directory: my-app-path
- name: Azure WebApp
uses: Azure/webapps-deploy@v3
with:
app-name: my-app-name
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: my/target/*.jar
若要部署 war 而不是 jar,請變更 package 值。
- name: Azure WebApp
uses: Azure/webapps-deploy@v3
with:
app-name: my-app-name
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: my/target/*.war
使用 Azure 發佈設定檔建置 Tomcat 應用程式並將其部署至 Azure。 輸入 publish-profile 會參考您稍早建立的 AZURE_WEBAPP_PUBLISH_PROFILE 祕密。
name: Build and deploy WAR app to Azure Web App using publish profile
env:
JAVA_VERSION: '11' # Set this to the Java version to use
DISTRIBUTION: microsoft # Set this to the Java distribution
AZURE_WEBAPP_NAME: sampleapp # Set this to the name of your web app
on: [push]
permissions:
id-token: write
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Java version
uses: actions/setup-java@v3.0.0
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.DISTRIBUTION }}
cache: 'maven'
- name: Build with Maven
run: mvn clean install
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: '*.war'
以下是使用多個作業進行建置和部署的完整 範例 。
重要事項
如果你要用 TypeScript 或其他編譯語言 azure/webapps-deploy@v3部署 Node.js 應用程式,請先在 GitHub Actions 建置你的應用程式,然後部署編譯好的輸出資料夾(例如 dist/ 或 build/)。 如果你想改用 App Service 建置自動化,請 將 SCM_DO_BUILD_DURING_DEPLOYMENT 應用程式設定 設為 true。
使用應用程式的發佈設定檔,建置 Node.js 應用程式並將其部署至 Azure。 輸入 publish-profile 會參考您稍早建立的 AZURE_WEBAPP_PUBLISH_PROFILE 祕密。
# File: .github/workflows/workflow.yml
name: JavaScript CI
on: [push]
env:
AZURE_WEBAPP_NAME: my-app-name # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: 'my-app-path' # Set this to the path to your web app project, defaults to the repository root
NODE_VERSION: '24.x' # Set this to the node version to use
jobs:
build-and-deploy:
name: Build and Deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: npm install, build, and test
run: |
# Build and test the project, then
# deploy to Azure Web App.
npm install
npm run build --if-present
npm run test --if-present
working-directory: my-app-path
- name: 'Deploy to Azure WebApp'
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
使用應用程式的發佈設定檔,建置 Python 應用程式並將其部署至 Azure。 請注意輸入 publish-profile 如何參考您稍早建立的 AZURE_WEBAPP_PUBLISH_PROFILE 祕密。
name: Python CI
on:
[push]
env:
AZURE_WEBAPP_NAME: my-web-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: 3.x
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Building web app
uses: azure/appservice-build@v2
- name: Deploy web App using GH Action azure/webapps-deploy
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
若要使用您設定的服務主體進行部署,請使用 azure/login@v2 動作搭配 creds 金鑰,並參考您稍早所建立的 AZURE_CREDENTIALS 祕密。
name: .NET Core
on: [push]
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
DOTNET_VERSION: '6.0.x' # Set this to the dot net version to use
jobs:
build:
runs-on: ubuntu-latest
steps:
# Check out the repo
- uses: actions/checkout@main
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# Set up .NET Core SDK
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
# Run dotnet build and publish
- name: dotnet build and publish
run: |
dotnet restore
dotnet build --configuration Release
dotnet publish -c Release --property:PublishDir='${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp'
# Deploy to Azure Web apps
- name: 'Run Azure webapp deploy action using Azure Credentials'
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp'
- name: logout
run: |
az logout
使用 Azure 服務主體建置 ASP.NET 模型檢視控制器 (MVC) 應用程式並將其部署至 Azure。 輸入 creds 會參考您稍早建立的 AZURE_CREDENTIALS 祕密。
name: Deploy ASP.NET MVC App deploy to Azure Web App
on: [push]
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
NUGET_VERSION: '5.3.x' # Set this to the dot net version to use
jobs:
build-and-deploy:
runs-on: windows-latest
steps:
# Check out the repo
- uses: actions/checkout@main
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Install Nuget
uses: nuget/setup-nuget@v1
with:
nuget-version: ${{ env.NUGET_VERSION}}
- name: NuGet to restore dependencies as well as project-specific tools that are specified in the project file
run: nuget restore
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.0.2
- name: Run MSBuild
run: msbuild .\SampleWebApplication.sln
- name: 'Run Azure webapp deploy action using Azure Credentials'
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/SampleWebApplication/'
# Azure logout
- name: logout
run: |
az logout
使用 Azure 服務主體建置 Java Spring Boot 應用程式並將其部署至 Azure。 輸入 creds 會參考您稍早建立的 AZURE_CREDENTIALS 祕密。
name: Java CI with Maven
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Set up JDK 1.8
uses: actions/setup-java@v3
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml
working-directory: complete
- name: Azure WebApp
uses: Azure/webapps-deploy@v3
with:
app-name: my-app-name
package: my/target/*.jar
# Azure logout
- name: logout
run: |
az logout
使用 Azure 服務主體建置 Tomcat 應用程式並將其部署至 Azure。 輸入 creds 會參考您稍早建立的 AZURE_CREDENTIALS 祕密。
name: Build and deploy WAR app to Azure Web App using Service Principal Connect
env:
JAVA_VERSION: '11' # Set this to the Java version to use
DISTRIBUTION: microsoft # Set this to the Java distribution
AZURE_WEBAPP_NAME: sampleapp # Set this to the name of your web app
on: [push]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Java version
uses: actions/setup-java@v3.0.0
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.DISTRIBUTION }}
cache: 'maven'
- name: Build with Maven
run: mvn clean install
- name: Login to Azure
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: '*.war'
以下是使用多個作業進行建置和部署的完整 範例 。
重要事項
如果你要用 TypeScript 或其他編譯語言 azure/webapps-deploy@v3部署 Node.js 應用程式,請先在 GitHub Actions 建置你的應用程式,然後部署編譯好的輸出資料夾(例如 dist/ 或 build/)。 如果你想改用 App Service 建置自動化,請 將 SCM_DO_BUILD_DURING_DEPLOYMENT 應用程式設定 設為 true。
使用 Azure 服務主體建置 Node.js 應用程式並將其部署至 Azure。 輸入 creds 會參考您稍早建立的 AZURE_CREDENTIALS 祕密。
name: JavaScript CI
on: [push]
name: Node.js
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: 'my-app-path' # Set this to the path to your web app project, defaults to the repository root
NODE_VERSION: '24.x' # Set this to the node version to use
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Check out the repo
- name: 'Checkout GitHub Action'
uses: actions/checkout@main
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Setup Node ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: 'npm install, build, and test'
run: |
npm install
npm run build --if-present
npm run test --if-present
working-directory: my-app-path
# Deploy web app using Azure credentials
- uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
# Azure logout
- name: logout
run: |
az logout
使用 Azure 服務主體建置 Python 應用程式並將其部署至 Azure。 輸入 creds 會參考您稍早建立的 AZURE_CREDENTIALS 祕密。
name: Python application
on:
[push]
env:
AZURE_WEBAPP_NAME: my-app # Set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: 3.x
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Deploy web App using GH Action azure/webapps-deploy
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
- name: logout
run: |
az logout
常見問題
如何透過 Maven 外掛程式部署 WAR 檔案?
如果您使用 Maven 外掛程式設定 Java Tomcat 專案,您也可以透過此外掛程式部署至 Azure App Service。 如果您使用 Azure CLI GitHub 動作,它會使用您的 Azure 認證。
- name: Azure CLI script file
uses: azure/cli@v2
with:
inlineScript: |
mvn package azure-webapp:deploy
如需如何使用和設定 Maven 外掛程式的詳細資訊,請參閱適用於 Azure App Service 的 Maven 外掛程式 Wiki。
如何透過 Azure CLI 部署 WAR 檔案?
如果您想要使用 Azure CLI 部署至 App Service,您可以使用適用於 Azure CLI 的 GitHub Action。
- name: Azure CLI script
uses: azure/cli@v2
with:
inlineScript: |
az webapp deploy --src-path '${{ github.workspace }}/target/yourpackage.war' --name ${{ env.AZURE_WEBAPP_NAME }} --resource-group ${{ env.RESOURCE_GROUP }} --async true --type war
如需如何使用和設定 Azure CLI 的 GitHub 動作的詳細資訊,請參閱 Azure CLI GitHub 動作。
如需命令的 az webapp deploy 詳細資訊,包括如何使用它和參數詳細資料,請參閱 az webapp deploy 文件。
如何部署啟動檔案?
使用 Azure CLI 的 GitHub 動作。 例如:
- name: Deploy startup script
uses: azure/cli@v2
with:
inlineScript: |
az webapp deploy --src-path ${{ github.workspace }}/src/main/azure/createPasswordlessDataSource.sh --name ${{ env.AZURE_WEBAPP_NAME }} --resource-group ${{ env.RESOURCE_GROUP }} --type startup --track-status false
如何將應用程式部署到容器中?
透過 Azure Web Deploy 動作,您可以自動化工作流程,使用 GitHub Actions 將自訂容器部署至 App Service。 如需詳細資訊,請參閱 部署至容器。
如何將部署佈署至部署插槽?
您可以使用 slot-name 參數,將部署佈署至部署插槽,而非生產插槽,方法是使用 azure/webapps-deploy@v3 動作。 若要佈署至插槽,請在您的工作流程中將 slot-name 參數新增至部署步驟:
- name: Deploy to Azure Web App
uses: azure/webapps-deploy@v3
with:
app-name: 'my-app-name'
slot-name: 'staging' # Deploy to the 'staging' slot instead of production
package: './output'
附註
使用 OpenID Connect 或服務主體認證時,請確保該身份在應用程式和部署欄位上都具備 網站貢獻者 角色。 關於發佈設定檔認證,請從 Azure 入口網站下載該特定插槽的發佈設定檔(>部署插槽>選擇插槽>下載發佈設定檔)。
如何在部署之後更新 Tomcat 設定?
如果您想要在部署之後更新任何 Web 應用程式設定,您可以使用 App Service 設定 動作。
- uses: azure/appservice-settings@v1
with:
app-name: 'my-app'
slot-name: 'staging' # Optional and needed only if the settings have to be configured on the specific deployment slot
app-settings-json: '[{ "name": "CATALINA_OPTS", "value": "-Dfoo=bar" }]'
connection-strings-json: '${{ secrets.CONNECTION_STRINGS }}'
general-settings-json: '{"alwaysOn": "false", "webSocketsEnabled": "true"}' #'General configuration settings as Key Value pairs'
id: settings
如需如何使用和設定此動作的詳細資訊,請參閱 App Service 設定 存放庫。
相關內容
請參閱 Azure GitHub 動作和工作流程上的下列參考: