Skip to main content

Deploying Node.js to Azure App Service

Learn how to deploy a Node.js project to Azure App Service as part of your continuous deployment (CD) workflows.

Prerequisites

Before creating your GitHub Actions workflow, you will first need to complete the following setup steps:

  1. 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
    

    Dans la commande ci-dessus, remplacez MY_RESOURCE_GROUP par votre groupe de ressources Azure préexistant et MY_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 :

  2. 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"
    

    In the command above, replace the parameters with your own values, where MY_WEBAPP_NAME is a new name for the web app.

  3. 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.

  4. Optionally, configure a deployment environment. Les environnements sont utilisĂ©s pour dĂ©crire une cible de dĂ©ploiement gĂ©nĂ©ral comme production, staging ou development. 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.

YAML
# 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