Prerequisites
Before creating your GitHub Actions workflow, you will first need to complete the following setup steps:
-
Créer un plan Azure App Service.
Par exemple, vous pouvez utiliser Azure CLI pour créer un plan App Service :
Bash az appservice plan create \ --resource-group MY_RESOURCE_GROUP \ --name MY_APP_SERVICE_PLAN \ --is-linux
az appservice plan create \ --resource-group MY_RESOURCE_GROUP \ --name MY_APP_SERVICE_PLAN \ --is-linux
Dans la commande ci-dessus, remplacez
MY_RESOURCE_GROUP
par votre groupe de ressources Azure préexistant etMY_APP_SERVICE_PLAN
par un nouveau nom pour le plan App Service.Pour plus dâinformations sur lâutilisation dâAzure CLI, consultez la documentation Azure :
- Pour lâauthentification, consultez Se connecter avec Azure CLI.
- Si vous devez créer un groupe de ressources, consultez az group.
-
Create a web app.
For example, you can use the Azure CLI to create an Azure App Service web app with a Node.js runtime:
Bash az webapp create \ --name MY_WEBAPP_NAME \ --plan MY_APP_SERVICE_PLAN \ --resource-group MY_RESOURCE_GROUP \ --runtime "NODE|14-lts"
az webapp create \ --name MY_WEBAPP_NAME \ --plan MY_APP_SERVICE_PLAN \ --resource-group MY_RESOURCE_GROUP \ --runtime "NODE|14-lts"
In the command above, replace the parameters with your own values, where
MY_WEBAPP_NAME
is a new name for the web app. -
Configurez un profil de publication Azure, et créez un secret
AZURE_WEBAPP_PUBLISH_PROFILE
.GĂ©nĂ©rez vos informations dâidentification de dĂ©ploiement Azure Ă lâaide dâun profil de publication. Pour plus dâinformations, consultez GĂ©nĂ©rer les informations dâidentification du dĂ©ploiement dans la documentation Azure.
Dans votre dépÎt GitHub, créez un secret nommé
AZURE_WEBAPP_PUBLISH_PROFILE
, qui contient le profil de publication. Pour plus dâinformations sur la crĂ©ation de secrets, consultez Using secrets in GitHub Actions. -
Optionally, configure a deployment environment. Les environnements sont utilisés pour décrire une cible de déploiement général comme
production
,staging
oudevelopment
. Quand un workflow GitHub Actions est dĂ©ployĂ© dans un environnement, lâenvironnement sâaffiche dans la page principale du dĂ©pĂŽt. Vous pouvez utiliser les environnements pour exiger lâapprobation dâun travail, restreindre les branches pouvant dĂ©clencher un flux de travail, contrĂŽler les dĂ©ploiements avec des rĂšgles personnalisĂ©es de protection des dĂ©ploiements ou limiter lâaccĂšs aux secrets. Pour plus dâinformations sur la crĂ©ation dâenvironnements, consultez Managing environments for deployment.
Creating the workflow
Once you've completed the prerequisites, you can proceed with creating the workflow.
The following example workflow demonstrates how to build, test, and deploy the Node.js project to Azure App Service when there is a push to the main
branch.
Ensure that you set AZURE_WEBAPP_NAME
in the workflow env
key to the name of the web app you created. If the path to your project is not the repository root, change AZURE_WEBAPP_PACKAGE_PATH
to your project path. If you use a version of Node.js other than 10.x
, change NODE_VERSION
to the version that you use.
Si vous avez configuré un environnement de déploiement, remplacez la valeur environment
par le nom de votre environnement. Si vous nâavez pas configurĂ© dâenvironnement ou si votre workflow se trouve dans un rĂ©fĂ©rentiel privĂ© et que vous nâutilisez pas GitHub Enterprise Cloud, supprimez la clĂ© environment
.
# Ce workflow utilise des actions qui ne sont pas certifiĂ©es par GitHub. # Elles sont fournies par un tiers et rĂ©gies par # des conditions dâutilisation du service, une politique de confidentialitĂ© et un support distincts. # documentation en ligne. # GitHub recommande dâĂ©pingler les actions Ă un SHA de commit. # Pour obtenir une version plus rĂ©cente, vous devez mettre Ă jour le SHA. # Vous pouvez Ă©galement rĂ©fĂ©rencer une balise ou une branche, mais lâaction peut changer sans avertissement. on: push: branches: - main env: AZURE_WEBAPP_NAME: MY_WEBAPP_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 NODE_VERSION: '14.x' # set this to the node version to use jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Node.js uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} cache: 'npm' - name: npm install, build, and test run: | npm install npm run build --if-present npm run test --if-present - name: Upload artifact for deployment job uses: actions/upload-artifact@v4 with: name: node-app path: . deploy: runs-on: ubuntu-latest needs: build environment: name: 'production' url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} steps: - name: Download artifact from build job uses: actions/download-artifact@v4 with: name: node-app - name: 'Deploy to Azure WebApp' id: deploy-to-webapp uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c with: app-name: ${{ env.AZURE_WEBAPP_NAME }} publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub.
# Elles sont fournies par un tiers et régies par
# des conditions dâutilisation du service, une politique de confidentialitĂ© et un support distincts.
# documentation en ligne.
# GitHub recommande dâĂ©pingler les actions Ă un SHA de commit.
# Pour obtenir une version plus récente, vous devez mettre à jour le SHA.
# Vous pouvez Ă©galement rĂ©fĂ©rencer une balise ou une branche, mais lâaction peut changer sans avertissement.
on:
push:
branches:
- main
env:
AZURE_WEBAPP_NAME: MY_WEBAPP_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
NODE_VERSION: '14.x' # set this to the node version to use
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v4
with:
name: node-app
path: .
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: node-app
- name: 'Deploy to Azure WebApp'
id: deploy-to-webapp
uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
Further reading
- For the original workflow template, see
azure-webapps-node.yml
in the GitHub Actionsstarter-workflows
repository. - The action used to deploy the web app is the official Azure
Azure/webapps-deploy
action. - For more examples of GitHub Action workflows that deploy to Azure, see the actions-workflow-samples repository.
- The Create a Node.js web app in Azure quickstart in the Azure web app documentation demonstrates using Visual Studio Code with the Azure App Service extension.