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
mainbranch. - Variables: The Node.js version is set as a variable.
- Stages: The pipeline is divided into two stages:
BuildandDeploy. - Build Stage:
- The code is checked out from the repository.
- Node.js is installed.
- Dependencies are installed using
npm install. - Linting is done via
npm run lint. - Tests are executed using
npm test. - The application is built using
npm run build.
- Deploy Stage:
- The code is checked out again (this could be optimized based on your actual needs).
- Node.js is installed.
- Only production dependencies are installed.
- 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.