使用 GitHub Actions 自动执行工作流,并从 GitHub 部署到 Azure 应用服务 。
先决条件
- 具有活动订阅的 Azure 帐户。 免费创建帐户。
- 一个 GitHub 帐户。 如果你没有帐户,可免费注册一个。
创建应用时设置 GitHub Actions 部署
GitHub Actions 部署已集成到默认的创建 Web 应用程序流程中。 在“部署”选项卡中将“持续部署”设置为“启用”,并配置所选的组织、存储库和分支。
启用持续部署时, 创建 Web 应用 过程会根据基本身份验证选择自动选取身份验证方法,并相应地配置应用和 GitHub 存储库:
从部署中心设置 GitHub Actions 部署
对于现有应用,可以在应用服务中使用 部署中心 快速开始使用 GitHub Actions。 此交钥匙方法基于应用程序堆栈生成 GitHub Actions 工作流文件,并将其提交到 GitHub 存储库。
通过使用 部署中心,还可以使用 用户分配的标识轻松配置更安全的 OpenID Connect 身份验证。 有关详细信息,请参阅用户分配的标识选项。
如果你的 Azure 帐户具有所需的权限,则你可以创建用户分配的标识。 否则,可以在“标识”下拉菜单中选择现有的用户分配的托管标识。 可以与 Azure 管理员协作,使用网站参与者角色创建用户分配的托管标识。
有关详细信息,请参阅持续部署到 Azure 应用服务。
手动设置 GitHub Actions 工作流
无需使用 部署中心即可部署工作流。 执行以下三个步骤:
- 生成部署凭据。
- 配置 GitHub 机密。
- 将工作流文件添加到 GitHub 存储库。
生成部署凭据
建议使用 OpenID Connect 通过适用于 GitHub Actions 的 Azure 应用服务进行身份验证。 此身份验证方法使用生存期较短的令牌。 使用 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
。
创建服务主体。 将 JSON 输出中的 $appID
替换为 appId
。
此命令可生成要在下一步中使用的具有不同 objectId
的 JSON 输出。 新的 objectId
是 assignee-object-id
。
复制appOwnerTenantId
以便稍后用作AZURE_TENANT_ID
的GitHub机密。
az ad sp create --id $appId
按订阅和对象创建新的角色分配。 默认情况下,角色分配将绑定到默认订阅。 将 $subscriptionId
替换为你的订阅 ID,将 $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
运行以下命令,为 Microsoft Entra 应用 创建新的联合标识凭据 。
在为 Active Directory 应用创建应用的过程中,将 APPLICATION-OBJECT-ID
替换为 appId
。
为 CREDENTIAL-NAME
设置一个值供以后引用。
设置 subject
。 GitHub 将根据工作流来定义其值:
- 在 GitHub Actions 环境中执行作业时,请使用:
repo:< Organization/Repository >:environment:< Name >
- 对于未绑定到环境的作业,请根据用于触发工作流的 ref 路径包含分支/标记的 ref 路径:
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 门户中的应用服务。
在“概述”页上,选择“下载发布配置文件”。
保存下载的文件。 使用该文件的内容来创建 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 contributor \
--scopes /subscriptions/<subscription-id>/resourceGroups/<group-name>/providers/Microsoft.Web/sites/<app-name> \
--json-auth
在之前的示例中,请将占位符替换为你的订阅 ID、资源组名称和应用名称。 输出是一个 JSON 对象,包含的角色分配凭据可提供对应用服务应用的访问权限。 输出应类似于以下 JSON 代码片段。 复制此 JSON 对象供以后使用。
{
"clientId": "<GUID>",
"clientSecret": "<GUID>",
"subscriptionId": "<GUID>",
"tenantId": "<GUID>",
(...)
}
重要
建议授予最低访问权限。 上一个示例中的范围仅限于特定的应用服务应用而不是整个资源组。
需要向登录操作提供应用程序的“客户端 ID”、“租户 ID”和“订阅 ID”。 这些值可直接在工作流中提供,或可存储在 GitHub 机密中并在工作流中引用。 将这些值保存为 GitHub 机密是更安全的选择。
打开你的 GitHub 存储库并转到“设置”>“安全”>“机密和变量”>“操作”>“新建存储库机密”。
为 AZURE_CLIENT_ID
、AZURE_TENANT_ID
和 AZURE_SUBSCRIPTION_ID
创建机密。 将 Active Directory 应用程序中的这些值用于 GitHub 机密:
GitHub 机密 |
Active Directory 应用程序 |
AZURE_CLIENT_ID |
应用程序(客户端)ID |
AZURE_TENANT_ID |
目录(租户)ID |
AZURE_SUBSCRIPTION_ID |
订阅 ID |
选择 “添加机密 ”以保存每个机密。
在 GitHub 中,浏览到存储库。 选择“设置”>“安全性”>“机密和变量”>“操作”>“新建存储库机密”。
若要使用在上一部分中创建的应用级凭据。请将下载的发布配置文件的内容粘贴到密钥值字段中。 将机密命名为 AZURE_WEBAPP_PUBLISH_PROFILE
。
稍后配置 GitHub 工作流文件时,请在“部署 Azure Web 应用”操作中使用AZURE_WEBAPP_PUBLISH_PROFILE
。 例如:
- uses: azure/webapps-deploy@v2
with:
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
在 GitHub 中,浏览到存储库。 选择“设置”>“安全性”>“机密和变量”>“操作”>“新建存储库机密”。
若要使用在上一部分中创建的用户级凭据,请将 Azure CLI 命令中的整个 JSON 输出粘贴到机密的值字段中。 将机密命名为 AZURE_CREDENTIALS
。
以后配置 GitHub 工作流文件时,请使用该机密作为 Azure/login
的输入 creds
。 例如:
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
将工作流文件添加到 GitHub 存储库
GitHub 存储库中 /.github/workflows/
路径中的 YAML (.yml) 文件定义工作流。 此定义包含组成工作流的各种步骤和参数。
工作流文件至少具有以下不同的步骤:
- 使用创建的 GitHub 机密对应用服务进行身份验证。
- 构建 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:
# 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。 该示例将 GitHub 机密用于 client-id
、tenant-id
和 subscription-id
值。 还可以直接在登录操作中传递这些值。
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。 该示例将 GitHub 机密用于 client-id
、tenant-id
和 subscription-id
值。 还可以直接在登录操作中传递这些值。
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:
# 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'
下面是使用多个作业进行生成和部署 的完整示例 。
使用应用的发布配置文件生成 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:
# 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'
下面是使用多个作业进行生成和部署 的完整示例 。
使用 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:
# 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 应用服务。 如果使用 Azure CLI GitHub 操作,它将使用你的 Azure 凭据。
- name: Azure CLI script file
uses: azure/cli@v2
with:
inlineScript: |
mvn package azure-webapp:deploy
有关如何使用和配置 Maven 插件的详细信息,请参阅 适用于 Azure 应用服务的 Maven 插件 Wiki。
如何通过 Azure CLI 部署 WAR 文件?
如果首选使用 Azure CLI 部署到应用服务,可以使用适用于 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 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 将自定义容器部署到应用服务。 有关详细信息,请参阅 “部署到容器”。
部署后如何更新 Tomcat 配置?
如果要在部署后更新任何 Web 应用设置,可以使用 应用服务设置 作。
- 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
有关如何使用和配置此作的详细信息,请参阅 应用服务设置 存储库。
相关内容
查看有关 Azure GitHub Actions 和工作流的以下参考: