Reputation: 2099
I want to enable continuous deployment on Azure DevOps for a Node.js app, by creating a Release pipeline. How do I make this happen?
Upvotes: 4
Views: 5126
Reputation: 2099
See my newer answer for a better solution.
I spent several hours trying to figure out how to get CI/CD working for a Node.js app, on Azure DevOps, because there is very little documentation that I could reference. I finally got it working, so I hope these steps will help you. Note: the UI may change over time. This is written in October of 2018.
Prerequisites:
There are two ways to add a deploy step to your pipeline, and those are: through the YAML script in the build pipeline, or with a release pipeline. These steps are for creating a release pipeline. I choose this so that I can manually choose which commit to deploy, but it can also be triggered automatically.
- task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(System.DefaultWorkingDirectory)'
Upvotes: 3
Reputation: 2099
When I wrote the previous answer I made one year ago, Azure DevOps didn't have the web app deployment task for build pipelines, so it had to be done using a release pipeline. Deploying through a build pipeline is much better and I highly recommend it because it allows you to commit all your CI/CD jobs to the repo. (Build pipelines are labeled "Pipelines" in the sidebar and release pipelines are in "Releases".)
Thus, this answer is for deploying your web app through your build pipeline. See my older answer if you would like to use a release pipeline.
package.json
and main.js
for your project. Run this locally to make sure it works on your computer.{
"name": "test-project",
"version": "0.0.0",
"scripts": {
"start": "node main.js",
"test": ""
},
"dependencies": {
"express": "^4.17.1"
}
}
const express = require('express');
const app = express();
const port = process.env.PORT || 3000; // You can see your app's env variables in Kudu: https://<your app>.scm.azurewebsites.net/
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
azure-pipelines.yml
. The YAML schema documentation is here.trigger:
- '*' # Run pipeline when a commit is pushed to any branch
- 'refs/tags/*' # Run pipeline when a tag is pushed
jobs:
- job: test
pool:
vmImage: ubuntu-latest
steps:
- script: npm install
displayName: npm install
- script: npm run test
displayName: npm run test
- job: deploy
condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/') # Run deploy job only if triggered by tag
pool:
vmImage: ubuntu-latest
steps:
- script: npm install
displayName: npm install
# - script: npm run build # If you are using TypeScript
# displayName: npm run build
- task: AzureWebApp@1 # https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-rm-web-app?view=azure-devops
inputs:
azureSubscription: <service connection name> # Replace this with the service connection name
appName: test-project # Replace this with the web app name
package: $(Build.SourcesDirectory)
customWebConfig: -Handler iisnode -NodeStartFile main.js -appType node # https://learn.microsoft.com/en-us/azure/devops/pipelines/targets/webapp?view=azure-devops&tabs=yaml
AzureWebApp@1
task and push this file to your repo.azure-pipelines.yml
Upvotes: 3