配合 GitHub Actions 開始將工作流程自動化,並從 GitHub 部署至 Azure App Service。
必要條件
- 具有有效訂用帳戶的 Azure 帳戶。
免費建立帳戶。
- GitHub 帳戶。 如果您沒有 Microsoft 帳戶,請免費註冊。
建立應用程式時設定 GitHub Actions 部署
GitHub Actions 部署已整合至預設 的建立 Web 應用程式程式。 在 [部署] 索引標籤中將 [持續部署] 設定為 [啟用],然後設定您想要的組織、存放庫和分支。
當您啟用持續部署時,應用程式建立會根據基本身份驗證選取專案自動挑選驗證方法,並據以設定您的應用程式和 GitHub 存放庫:
透過部署中心設定 GitHub Actions 部署
若是現有的應用程式,您可以使用 App Service 部署中心,迅速地開始使用 GitHub Actions。 這個周全方法會根據您的應用程式堆疊產生 GitHub Actions 工作流程檔案,並將其認可至 GitHub 存放庫。
部署中心也可讓您使用使用者指派的身分識別,輕鬆地設定更安全的 OpenID Connect 驗證。 如需詳細資訊,請參閱 使用者指派的身分識別選項。
如果您的 Azure 帳戶具有 所需的許可權,您可以建立使用者指派的身分識別。 否則,您可以在 [身分識別] 下拉功能表中選取現有的使用者指派受控識別。 您可以與 Azure 管理員合作,使用網站參與者角色建立使用者指派的受控識別。
如需詳細資訊,請參閱持續部署至 Azure App Service。
手動設定 GitHub Actions 工作流程
您可以在不使用部署中心的情況下部署工作流程。 在此情況下,您需要執行三個步驟:
-
產生部署認證
-
設定 GitHub 祕密
-
將工作流程檔案新增至 GitHub 存放庫
產生部署認證
使用 GitHub Actions Azure App 服務 進行驗證的建議方式是使用 OpenID Connect。 此方法是使用短期令牌的驗證方法。 使用 GitHub Actions 設定 OpenID Connect 是更複雜的作法,但安全性更高。
或者,您可以使用使用者指派的受控識別、服務主體或發行配置文件進行驗證。
下列程序說明使用 Azure CLI 語句建立Microsoft Entra 應用程式、服務主體和同盟認證的步驟。 若要瞭解如何在 Azure 入口網站 中建立Microsoft Entra 應用程式、服務主體和同盟認證,請參閱 Connect GitHub 和 Azure。
如果您沒有現有的應用程式,請註冊 可存取資源的新Microsoft Entra 應用程式和服務主體。 建立 Microsoft Entra 應用程式。
az ad app create --display-name myApp
這個指令會傳回 JSON,其中包含 appId
您的 client-id
。 儲存值以稍後作為 AZURE_CLIENT_ID
GitHub 秘密。
使用圖形 API 建立同盟認證時,您使用 objectId
值,並將其作為 APPLICATION-OBJECT-ID
加以參考。
建立服務主體。 將 $appID
取代為您 JSON 輸出中的 appId。
這個指令會產生 JSON 輸出,並在 objectId
下一個步驟中使用不同的 。 新的 objectId
是 assignee-object-id
。
複製 appOwnerTenantId
以供稍後作為 AZURE_TENANT_ID
的 GitHub 秘密使用。
az ad sp create --id $appId
依訂用帳戶和物件建立新的角色指派。 根據預設,角色指派將會繫結至預設訂用帳戶。
$subscriptionId
取代為訂用帳戶識別碼、$resourceGroupName
資源群組名稱、 $webappName
Web 應用程式名稱,以及$assigneeObjectId
產生的 id
。 瞭解如何使用 Azure CLI 來管理 Azure 訂用帳戶。
az role assignment create --role contributor --subscription $subscriptionId --assignee-object-id $assigneeObjectId --scope /subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Web/sites/$webappName --assignee-principal-type ServicePrincipal
執行下列命令,為您的 Entra 應用程式建立新的同盟身分識別認證 。
將取代APPLICATION-OBJECT-ID
為 Active Directory 應用程式的 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 應用程式需要設定true
為 的應用程式設定WEBSITE_WEBDEPLOY_USE_SCM
,才能下載發行配置檔。 未來將移除此需求。
您可以使用 Azure CLI 中的 az ad sp create-for-rbac 命令來建立服務主體。 在 Azure 入口網站 中使用 Azure Cloud Shell 執行此命令,或選取 [開啟 Cloud Shell]。
az ad sp create-for-rbac --name "myApp" --role 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/登入動作。 這些值可以直接在工作流程中提供,也可以儲存在 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/登入。 例如:
- 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 動作。 此動作需要 app-name
中的 Web 應用程式名稱,而且根據語言堆疊,在 package
中輸入要部署的 *.zip、*.war、*.jar 或資料夾路徑。 如需動作可能輸入 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:
# Checkout 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:
# checkout 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'
您可以使用多個作業進行建置和部署,找到此 完整範例 。
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: '18.x' # set this to the node version to use
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# checkout 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 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:
# Checkout 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'
您可以使用多個作業進行建置和部署,找到此 完整範例 。
使用該應用程式發行設定檔,建置 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: '18.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:
# Checkout the repo
- uses: actions/checkout@main
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# 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 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:
# checkout 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'
您可以使用多個作業進行建置和部署,找到此 完整範例 。
使用 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: '18.x' # set this to the node version to use
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# checkout 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 服務。 如果您使用 Azure CLI GitHub 動作,它會使用您的 Azure 認證。
- name: Azure CLI script file
uses: azure/cli@v2
with:
inlineScript: |
mvn package azure-webapp:deploy
如需 Maven 外掛程式及其使用方式及設定的詳細資訊,請參閱 maven 外掛程式 wiki for Azure App 服務。
如何? 透過 Az 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
如需 CLI 的 GitHub Action 以及如何使用和設定的詳細資訊,請參閱 Azure CLI GitHub 動作。
如需 az webapp deploy 命令、如何使用命令和參數詳細數據的詳細資訊,請參閱 az webapp deploy 檔。
如何? 部署啟動檔案嗎?
使用適用於 CLI 的 GitHub Action。 例如:
- 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 部署動作,您可以使用 GitHub Actions 將工作流程自動化,以將自訂容器部署至 App Service。 如需使用 GitHub Actions 部署步驟的詳細資訊,請參閱 部署至容器。
如何? 部署後更新 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 Actions 和工作流程的參考: