如何在Azure DevOps中为Node.js Web应用程序创建发布管道?

如何在Azure DevOps中为Node.js Web应用程序创建发布管道?

问题描述:

我想通过创建发布管道在Node.js应用程序的Azure DevOps上启用连续部署.我如何做到这一点?

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?

当我写了一年前的上一个答案时,Azure DevOps没有用于构建管道的Web应用程序部署任务,因此必须完成使用发布任务,我不喜欢发布任务,因为发布管道无法添加到源代码管理中.通过您的构建管道进行部署要好得多,我强烈建议您这样做.它允许您将所有CI/CD任务写在一个文件中,然后将此文件添加到项目源中.

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.

因此,此答案用于通过构建管道部署Web应用程序.如果您想使用发布管道,请参阅我的较早答案.

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.

假设您的Web应用程序具有服务连接,并且在DevOps中有一个Node.js项目,则在项目中创建package.jsonmain.js.在本地运行此程序,以确保它可以在您的计算机上正常工作.

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}!`))

现在,您需要一个YAML文件用于管道.将此文件命名为azure-pipelines.yaml. YAML架构文档为这里.

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

将其推送到您的仓库中,然后创建一个构建管道.设置很容易.如果您的azure-pipelines.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.

之所以在构建中执行两次npm install是因为在每次作业后都会清理工作区.我无法弄清楚如何保留上一份工作中的文件.

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.

构建完成后,请转到您的站点,您应该会看到hello world消息.如果您的站点显示应用程序错误消息,则可以通过以下方法检查错误日志:转到Azure门户中的Web应用程序,然后转到侧边栏中的日志流"页面.请注意,只有在有人访问网页后,应用容器才会启动.因此,要测试应用程序的初始化,必须首先访问该网站.

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.