问题
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?
回答1:
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:
- An Azure Web App service to deploy to
- An Azure subscription
- A Node.js project on Azure DevOps with a working build pipeline
- You have been able to deploy your app through other means, such as ftp or Git
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.
- Generate an artifact of your repo directory in your build pipeline. If your build pipeline is done through a YAML script, add this to the YAML file (more info):
- task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(System.DefaultWorkingDirectory)'
- Run the build pipeline and wait for it to finish. Confirm that an artifact was generated.
- Go to the Releases tab and create a new release pipeline.
- In the templates menu, find "Deploy a Node.js app to Azure App Service" and click Apply.
- Open Stage 1 by clicking the link that says "1 job, 1 task".
- In the "Azure subscription" field, click the + New button. The "Add an Azure Resource Manager service connection" modal pops up.
- This part was incredibly frusturating, but this Medium article helped out a lot. At first it didn't work, but that was because I didn't read everything. Make sure to read every sentence, and it should work. When the connection shows "verified", click OK.
- There is the option to "use the automated version of the service connection dialog", but that interface was not working for me.
- What makes this step hard is the fact that DevOps and Portal use different terms for each variable. Azure services, please come together and agree on a single naming system.
- Select the app type.
- Find your App service name.
- Go to the Deploy Azure App Service task. Most settings don't need to be changed, but you will need to specify the build artifact to use. This is done under "Package or folder". Click on "..." and find your build artifact. If there are no artifacts showing, your build pipeline isn't working.
- Save the release pipeline.
- Open your latest CI build and click the Release button.
- All default settings in the "Create a new release" modal should be fine. Hit "Create". Now you can open your Release pipeline and watch the progress. If it fails due to a connection problem, edit your Release pipeline and confirm that your Azure Resource Manager connection is verified and the correct app type and app service is selected.
- Go to your site and confirm that your app has successfully deployed.
回答2:
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 task, which I was not a fan of because release pipelines can't be added to source control. Deploying through your build pipeline is much better and I highly recommend it. It allows you to write all CI/CD tasks in a single file, and add this file to your project source.
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.
First, you're going to need a service connection. See this question.
Assuming you have a service connection for your web app and you have a Node.js project in DevOps, make package.json
and main.js
in 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}!`))
Now you need a YAML file for the pipeline. Name this file azure-pipelines.yaml
. The YAML schema documentation is here.
trigger: # Runs pipeline every time you push commits and when you create a tag
- '*'
- 'refs/tags/*'
jobs:
- job: test
pool:
vmImage: ubuntu-16.04
steps:
- script: npm install
displayName: npm install
- script: npm run test
displayName: npm run test
- job: deploy
condition: contains(variables['Build.SourceBranch'], 'refs/tags') # Run deploy job only if triggered by tag
dependsOn: test
pool:
vmImage: ubuntu-18.04
steps:
- script: npm install
displayName: npm install
# - script: npm run build # If you are using TypeScript
# displayName: npm run build
- task: AzureWebApp@1 # https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-rm-web-app?view=azure-devops
inputs:
azureSubscription: <service connection name>
appName: test-project
package: $(Build.SourcesDirectory)
customWebConfig: -Handler iisnode -NodeStartFile main.js -appType node # https://docs.microsoft.com/en-us/azure/devops/pipelines/targets/webapp?view=azure-devops&tabs=yaml
Push this to your repo, then create a build pipeline. The setup is easy. If your azure-pipelines.yaml
is in your repo, the setup should detect this file and allow you to run it. On the first run, it may say that the service connection is unauthorized. Clicking "Authorize resources" then running the build again manually with the Queue button will resolve this.
The reason why I do npm install
twice in the build is because the workspace is cleaned after each job. I was not able to figure out how to preserve files from the previous job.
After the build finishes, you can deploy your code by creating a tag. In the sidebar of your repo, there is a Tags page. There, you can Create Tag. For some reason it requires a tag description, so I just copy the tag name. Now if you go back to the builds list, you will see your deployment job running.
When the build finishes, go to your site, and you should see the hello world message. If your site displays an application error message, you can check the error log by going to your web app in the Azure portal, then Log stream page in the sidebar. Note that the app container is started only after someone visits the web page. Therefore, to test app initialization, you must first visit the site.
来源:https://stackoverflow.com/questions/52785852/how-do-i-create-a-release-pipeline-for-a-node-js-web-app-in-azure-devops