Gebruik GitHub Actions om uw werkstroom te automatiseren en vanuit GitHub te implementeren in Azure App Service .
Vereisten
- Een Azure-account met een actief abonnement.
Gratis een account maken
- Een GitHub-account. Als u geen account hebt, kunt u zich registreren voor een gratis account.
Implementatie van GitHub Actions instellen bij het maken van een app
GitHub Actions-implementatie is geïntegreerd in het standaardproces Web-app maken. Stel continue implementatie in op Inschakelen op het tabblad Implementatie en configureer uw gekozen organisatie, opslagplaats en vertakking.
Wanneer u continue implementatie inschakelt, kiest het proces Web-app maken automatisch de verificatiemethode op basis van de basisverificatieselectie en configureert u uw app en uw GitHub-opslagplaats dienovereenkomstig:
Notitie
Wanneer u een app maakt, wordt er mogelijk een foutbericht weergegeven waarin wordt aangegeven dat uw Azure-account niet over bepaalde machtigingen beschikt. Uw account heeft mogelijk de vereiste machtigingen nodig om de door de gebruiker toegewezen identiteit te maken en te configureren. Zie de volgende sectie voor een alternatief.
Implementatie van GitHub Actions instellen vanuit Het Implementatiecentrum
Voor een bestaande app kunt u snel aan de slag met GitHub Actions met behulp van Deployment Center in App Service. Met deze kant-en-klare methode wordt een Werkstroombestand voor GitHub Actions gegenereerd op basis van uw toepassingsstack en wordt dit doorgevoerd in uw GitHub-opslagplaats.
Met behulp van Deployment Center kunt u ook eenvoudig de veiligere OpenID Connect-verificatie configureren met een door de gebruiker toegewezen identiteit. Zie de optie door de gebruiker toegewezen identiteit voor meer informatie.
Als uw Azure-account over de benodigde machtigingen beschikt, kunt u een door de gebruiker toegewezen identiteit maken. Anders kunt u een bestaande door de gebruiker toegewezen beheerde identiteit selecteren in het vervolgkeuzemenu Identiteit . U kunt samenwerken met uw Azure-beheerder om een door de gebruiker toegewezen beheerde identiteit te maken met de rol Inzender voor websites.
Zie Continue implementatie naar Azure-app Service voor meer informatie.
Handmatig een GitHub Actions-werkstroom instellen
U kunt een werkstroom implementeren zonder Implementatiecentrum te gebruiken. Voer deze drie stappen uit:
-
Implementatiereferenties genereren.
-
Configureer het GitHub-geheim.
-
Voeg het werkstroombestand toe aan uw GitHub-opslagplaats.
Genereer implementatiereferenties
U wordt aangeraden OpenID Connect te gebruiken om te verifiëren met Azure App Service voor GitHub Actions. Deze verificatiemethode maakt gebruik van kortdurende tokens. Het instellen van OpenID Connect met GitHub Actions is complexer, maar biedt beperkte beveiliging.
U kunt zich ook verifiëren met een door de gebruiker toegewezen beheerde identiteit, een service-principal of een publicatieprofiel.
In de volgende procedure worden de stappen beschreven voor het maken van een Microsoft Entra-toepassing, service-principal en federatieve referenties met behulp van Azure CLI-instructies. Zie Connect GitHub en Azure voor meer informatie over het maken van een Microsoft Entra-applicatie, service-principal en federatieve referenties in de Azure-portal.
Als u geen bestaande toepassing hebt, registreert u een nieuwe Microsoft Entra-toepassing en service-principal die toegang heeft tot resources. Maak de Microsoft Entra-toepassing.
az ad app create --display-name myApp
Met deze opdracht wordt een JSON-uitvoer geretourneerd met een appId die uw client-id is. Sla de waarde op die u later wilt gebruiken als het AZURE_CLIENT_ID GitHub-geheim.
U gebruikt de objectId waarde bij het maken van federated credentials met Graph API en verwijst ernaar als de APPLICATION-OBJECT-ID.
Een service-principal maken. Vervang de $appID door de appId uit je JSON-uitvoer.
Met deze opdracht wordt een JSON-uitvoer gegenereerd met een ander objectId die u in de volgende stap kunt gebruiken. Het nieuwe objectId is de assignee-object-id.
Kopieer het appOwnerTenantId bestand naar later gebruik als gitHub-geheim voor AZURE_TENANT_ID.
az ad sp create --id $appId
Maak een nieuwe roltoewijzing per abonnement en object. De roltoewijzing is standaard gekoppeld aan uw standaardabonnement. Vervang $subscriptionId door uw abonnements-id, $resourceGroupName door de naam van uw resourcegroep, $webappName door de naam van uw web-app en $assigneeObjectId door de gegenereerde idnaam. Meer informatie over het beheren van Azure-abonnementen met de Azure CLI.
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
Voer de volgende opdracht uit om een nieuwe federatieve identiteitsreferentie te maken voor uw Microsoft Entra-app.
Vervang APPLICATION-OBJECT-ID door de appId die u hebt gegenereerd tijdens het aanmaken van de app voor uw Active Directory-toepassing.
Stel een waarde in om CREDENTIAL-NAME later te verwijzen.
Stel de subject in. GitHub definieert de waarde ervan, afhankelijk van uw werkstroom:
- Gebruik voor taken in uw GitHub Actions-omgeving:
repo:< Organization/Repository >:environment:< Name >
- Voor taken die niet aan een omgeving zijn gekoppeld, moet u het ref-pad voor branch/tag opnemen op basis van het ref-pad dat wordt gebruikt voor het activeren van de werkstroom:
repo:< Organization/Repository >:ref:< ref path>. Bijvoorbeeld repo:n-username/ node_express:ref:refs/heads/my-branch of repo:n-username/ node_express:ref:refs/tags/my-tag.
- Voor werkstromen die worden geactiveerd door een pull-aanvraaggebeurtenis, gebruikt u:
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"
]
}
Notitie
Als u een publicatieprofiel wilt gebruiken, moet u basisverificatie inschakelen.
Een publicatieprofiel is een referentie op app-niveau. Stel uw publicatieprofiel in als een GitHub-geheim.
Ga naar App Service in Azure Portal.
Selecteer Op de pagina Overzicht het publicatieprofiel downloaden.
Sla het gedownloade bestand op. Gebruik de inhoud van het bestand om een GitHub-geheim te maken.
Notitie
Vanaf oktober 2020 hebben Linux-web-apps de app-instelling WEBSITE_WEBDEPLOY_USE_SCM ingesteld op truevoordat het publicatieprofiel wordt gedownload.
U kunt een service-principal maken met de az ad sp create-for-rbac opdracht in de Azure CLI. Voer deze opdracht uit met behulp van Azure Cloud Shell in Azure Portal of door Open Cloud Shell te selecteren.
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
Vervang in het vorige voorbeeld de tijdelijke aanduidingen door uw abonnements-id, resourcegroepnaam en app-naam. De uitvoer is een JSON-object met de roltoewijzingsreferenties die toegang bieden tot uw App Service-app. De uitvoer moet er ongeveer uitzien als het volgende JSON-fragment. Kopieer dit JSON-object voor later gebruik.
{
"clientId": "<GUID>",
"clientSecret": "<GUID>",
"subscriptionId": "<GUID>",
"tenantId": "<GUID>",
(...)
}
Belangrijk
U wordt aangeraden minimale toegang te verlenen. Het bereik in het vorige voorbeeld is beperkt tot de specifieke App Service-app en niet de hele resourcegroep.
U moet de client-id, tenant-id en abonnements-id van uw toepassing opgeven voor de Azure/login actie. Deze waarden kunnen rechtstreeks in de werkstroom worden opgegeven of kunnen worden opgeslagen in GitHub-geheimen en waarnaar wordt verwezen in uw werkstroom. Het opslaan van de waarden als GitHub-geheimen is de veiligere optie.
Open uw GitHub-opslagplaats en ga naar Beveiligingsgeheimen>>>en variabelen Acties>nieuwe opslagplaats.
Geheimen maken voor AZURE_CLIENT_ID, AZURE_TENANT_IDen AZURE_SUBSCRIPTION_ID. Gebruik deze waarden uit uw Active Directory-toepassing voor uw GitHub-geheimen:
| GitHub-geheim |
Active Directory-toepassing |
AZURE_CLIENT_ID |
Client-id van toepassing |
AZURE_TENANT_ID |
Id van directory (tenant) |
AZURE_SUBSCRIPTION_ID |
Abonnements-id |
Selecteer Geheim toevoegen om elk geheim op te slaan.
Blader in GitHub naar uw opslagplaats. Selecteer Beveiligingsgeheimen>en variabelen Instellingen Voor nieuwe>
Als u de referenties op app-niveau wilt gebruiken die u in de vorige sectie hebt gemaakt, plakt u de inhoud van het gedownloade publicatieprofielbestand in het waardeveld van het geheim. Geef het geheim AZURE_WEBAPP_PUBLISH_PROFILEeen naam.
Wanneer u het GitHub-werkstroombestand later configureert, gebruikt u de AZURE_WEBAPP_PUBLISH_PROFILE actie Azure-web-app implementeren . Voorbeeld:
- uses: azure/webapps-deploy@v2
with:
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
Blader in GitHub naar uw opslagplaats. Selecteer Beveiligingsgeheimen>en variabelen Instellingen Voor nieuwe>
Als u de referenties op gebruikersniveau wilt gebruiken die u in de vorige sectie hebt gemaakt, plakt u de volledige JSON-uitvoer van de Azure CLI-opdracht in het waardeveld van het geheim. Geef het geheim AZURE_CREDENTIALSeen naam.
Wanneer u het GitHub-werkstroombestand later configureert, gebruikt u het geheim voor de invoer creds van Azure/login. Voorbeeld:
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
Het werkstroombestand toevoegen aan uw GitHub-opslagplaats
Een YAML-bestand (.yml) in het /.github/workflows/ pad in uw GitHub-opslagplaats definieert een werkstroom. Deze definitie bevat de verschillende stappen en parameters die deel uitmaken van de werkstroom.
Het werkstroombestand heeft minimaal de volgende afzonderlijke stappen:
- Verifieer met App Service met behulp van het GitHub-geheim dat u hebt gemaakt.
- Bouw de web-app.
- Implementeer de web-app.
Als u uw code wilt implementeren in een App Service-app, gebruikt u de azure/webapps-deploy@v3 actie. Voor de actie is de naam van uw webapp vereist in app-name, en afhankelijk van uw taalstack, het pad van een *.zip, *.war, *.jar of map waarin geïmplementeerd moet worden in package. Zie azure/webapps-deploy@v3 voor een volledige lijst met mogelijke invoer voor de actie.
In de volgende voorbeelden ziet u het deel van de werkstroom waarmee de web-app wordt gebouwd, in verschillende ondersteunde talen.
Als u wilt implementeren met OpenID Connect met behulp van de beheerde identiteit die u hebt geconfigureerd, gebruikt u de azure/login@v2 actie met de client-id, tenant-iden subscription-id sleutels. Verwijs naar de GitHub-geheimen die u eerder hebt gemaakt.
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
Bouw en implementeer een ASP.NET MVC-app (model-view-controller) in Azure met behulp van een Azure-service-principal. In het voorbeeld worden GitHub-geheimen gebruikt voor de client-id, tenant-iden subscription-id waarden. U kunt deze waarden ook rechtstreeks doorgeven in de aanmeldingsactie.
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
Bouw en implementeer een Java Spring Boot-app in Azure met behulp van een Azure-service-principal. In het voorbeeld worden GitHub-geheimen gebruikt voor de client-id, tenant-iden subscription-id waarden. U kunt deze waarden ook rechtstreeks doorgeven in de aanmeldingsactie.
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'
Hier volgt een volledig voorbeeld waarin meerdere taken worden gebruikt voor het bouwen en implementeren.
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
De publish-profile invoer moet verwijzen naar het AZURE_WEBAPP_PUBLISH_PROFILE GitHub-geheim dat u eerder hebt gemaakt.
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'
Bouw en implementeer een ASP.NET MVC-app (model-view-controller) die gebruikmaakt van NuGet en publish-profile voor verificatie.
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/'
Bouw en implementeer een Java Spring Boot-app in Azure met behulp van een Azure-publicatieprofiel. De publish-profile invoer verwijst naar het AZURE_WEBAPP_PUBLISH_PROFILE geheim dat u eerder hebt gemaakt.
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
Als u een war in plaats van een jarwilt implementeren, wijzigt u de package waarde.
- 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
Bouw en implementeer een Tomcat-app in Azure met behulp van een Azure-publicatieprofiel. De publish-profile invoer verwijst naar het AZURE_WEBAPP_PUBLISH_PROFILE geheim dat u eerder hebt gemaakt.
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'
Hier volgt een volledig voorbeeld waarin meerdere taken worden gebruikt voor het bouwen en implementeren.
Bouw en implementeer een Node.js-app in Azure met behulp van het publicatieprofiel van de app. De publish-profile invoer verwijst naar het AZURE_WEBAPP_PUBLISH_PROFILE geheim dat u eerder hebt gemaakt.
# 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 }}
Bouw en implementeer een Python-app in Azure met behulp van het publicatieprofiel van de app. Let op hoe de publish-profile invoer verwijst naar het AZURE_WEBAPP_PUBLISH_PROFILE geheim dat u eerder hebt gemaakt.
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 }}
Als u wilt implementeren met de service-principal die u hebt geconfigureerd, gebruikt u de azure/login@v2 actie met de creds sleutel en verwijst u naar het AZURE_CREDENTIALS geheim dat u eerder hebt gemaakt.
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
Bouw en implementeer een ASP.NET MVC-app (model-view-controller) in Azure met behulp van een Azure-service-principal. De creds invoer verwijst naar het AZURE_CREDENTIALS geheim dat u eerder hebt gemaakt.
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
Bouw en implementeer een Java Spring Boot-app in Azure met behulp van een Azure-service-principal. De creds invoer verwijst naar het AZURE_CREDENTIALS geheim dat u eerder hebt gemaakt.
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
Bouw en implementeer een Tomcat-app in Azure met behulp van een Azure-service-principal. De creds invoer verwijst naar het AZURE_CREDENTIALS geheim dat u eerder hebt gemaakt.
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'
Hier volgt een volledig voorbeeld waarin meerdere taken worden gebruikt voor het bouwen en implementeren.
Bouw en implementeer een Node.js-app in Azure met behulp van een Azure-service-principal. De creds invoer verwijst naar het AZURE_CREDENTIALS geheim dat u eerder hebt gemaakt.
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
Bouw en implementeer een Python-app in Azure met behulp van een Azure-service-principal. De creds invoer verwijst naar het AZURE_CREDENTIALS geheim dat u eerder hebt gemaakt.
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
Veelgestelde vragen
Hoe implementeer ik een WAR-bestand via de Maven-invoegtoepassing?
Als u uw Java Tomcat-project hebt geconfigureerd met de Maven-invoegtoepassing, kunt u ook implementeren in Azure App Service via deze invoegtoepassing. Als u de Azure CLI GitHub-actie gebruikt, worden uw Azure-referenties gebruikt.
- name: Azure CLI script file
uses: azure/cli@v2
with:
inlineScript: |
mvn package azure-webapp:deploy
Zie de Wiki van de Maven-invoegtoepassing voor Azure App Service voor meer informatie over het gebruik en configureren van de Maven-invoegtoepassing.
Hoe implementeer ik een WAR-bestand via de Azure CLI?
Als u de Azure CLI liever wilt gebruiken om te implementeren in App Service, kunt u de GitHub Action voor de Azure CLI gebruiken.
- 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
Zie de Azure CLI GitHub-actie voor meer informatie over het gebruik en configureren van de GitHub-actie voor Azure CLI.
Zie de documentatie voor meer informatie over de az webapp deploy opdracht, waaronder het gebruik ervan en de parameterdetailsaz webapp deploy.
Hoe kan ik een opstartbestand implementeren?
Gebruik de GitHub Action voor de Azure CLI. Voorbeeld:
- 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
Hoe kan ik implementeren in een container?
Met de Azure Web Deploy-actie kunt u uw werkstroom automatiseren om aangepaste containers te implementeren in App Service met behulp van GitHub Actions. Zie Implementeren in een container voor meer informatie.
Hoe kan ik de Tomcat-configuratie bijwerken na de implementatie?
Als u de instellingen van uw web-apps na de implementatie wilt bijwerken, kunt u de actie App Service-instellingen gebruiken.
- 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
Zie de opslagplaats voor App Service-instellingen voor meer informatie over het gebruik en configureren van deze actie.
Gerelateerde inhoud
Bekijk de volgende verwijzingen in Azure GitHub Actions en werkstromen: