Azure DevOps for Node.js

Creating a DevOps pipeline with Azure DevOps for a Node.js application involves defining steps for tasks such as checking out code, installing dependencies, running tests, and deploying the application. Below is a sample YAML file that demonstrates these tasks.

Create a new file in your repository named azure-pipelines.yml and populate it as follows:

# Node.js application Azure DevOps pipeline

trigger:
- main  # this pipeline will trigger on push to main branch

variables:
  nodeVersion: '18.x'  # set Node.js version

stages:
- stage: Build
  displayName: 'Build Stage'
  jobs:
  - job: BuildJob
    pool:
      vmImage: 'ubuntu-latest'  # set VM image
    steps:
    - checkout: self  # checkout code from repository
    - task: UseNode@1  # use Node.js in pipeline
      inputs:
        version: $(nodeVersion)

    # Install dependencies
    - script: npm install
      displayName: 'npm install'

    # Run lint
    - script: npm run lint
      displayName: 'Run lint'

    # Run tests
    - script: npm test
      displayName: 'Run tests'

    # Build application
    - script: npm run build
      displayName: 'Build application'

- stage: Deploy
  displayName: 'Deploy Stage'
  jobs:
  - deployment: DeployJob
    environment: 'Production'
    pool:
      vmImage: 'ubuntu-latest'
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self
          - task: UseNode@1
            inputs:
              version: $(nodeVersion)

          # Install production dependencies only
          - script: npm install --production
            displayName: 'Install production dependencies'

          # Actual deployment steps
          # Here you can add tasks to deploy to your hosting environment
          # e.g., Azure Web App, container registry, etc.

          - script: echo "Deployment Step Placeholder"
            displayName: 'Deploy application'Code language: PHP (php)

Explanation:

  • Trigger: The pipeline will be triggered when there are changes to the main branch.
  • Variables: The Node.js version is set as a variable.
  • Stages: The pipeline is divided into two stages: Build and Deploy.
  • Build Stage:
    1. The code is checked out from the repository.
    2. Node.js is installed.
    3. Dependencies are installed using npm install.
    4. Linting is done via npm run lint.
    5. Tests are executed using npm test.
    6. The application is built using npm run build.
  • Deploy Stage:
    1. The code is checked out again (this could be optimized based on your actual needs).
    2. Node.js is installed.
    3. Only production dependencies are installed.
    4. Actual deployment steps (this is a placeholder, you’ll need to fill in the details based on where and how you’re deploying your application).

This is a basic template, and depending on your project’s needs, you may need to add or modify steps. This could include steps for database migrations, setting environment variables, caching dependencies, etc.

Leave a Reply

Scroll to Top

Discover more from DevOps AI/ML

Subscribe now to keep reading and get access to the full archive.

Continue reading