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

标签 node.js azure-devops azure-pipelines continuous-deployment

我想通过创建发布管道在 Azure DevOps 上为 Node.js 应用启用持续部署。我该如何做到这一点?

最佳答案

当我写我一年前做的上一个答案时,Azure DevOps 没有用于构建管道的 Web 应用程序部署任务,因此必须使用发布管道来完成。通过构建管道进行部署要好得多,我强烈推荐它,因为它允许您将所有 CI/CD 作业提交到 repo。 (构建管道在侧边栏中标记为“管道”,发布管道在“发布”中。)

因此,此答案适用于通过构建管道部署网络应用程序。如果您想使用发布管道,请参阅我的旧答案。

  1. Create a service connection.
  2. 假设您的网络应用程序有一个服务连接,并且您在 DevOps 中有一个 Node.js 项目,请为您的项目创建一个 package.jsonmain.js .在本地运行此程序以确保它可以在您的计算机上运行。
{
  "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}!`));
  1. 现在您需要一个用于管道的 YAML 文件。将此文件命名为 azure-pipelines.yml。 YAML 模式文档是 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
  1. 替换 AzureWebApp@1 任务中特定于项目的值,并将此文件推送到您的存储库。
  2. 转到“管道”页面并单击“新建管道”。管道选项(非常简单):
  • “你的代码在哪里?” Azure 存储库 Git
  • “选择存储库”选择您的存储库
  • “配置你的管道”现有的 Azure Pipelines YAML 文件
  • azure-pipelines.yml 的“选择现有 YAML 文件”路径
  • 点击运行
  • 第一次运行时,它可能会说服务连接未经授权。单击“授权资源”,然后使用“队列”按钮再次手动运行构建将解决此问题。
  1. 要运行构建作业,请创建一个标签并将其推送。返回构建列表,您将看到您的部署作业正在运行。
  • 要通过网络界面执行此操作:转到 Repos 下的“标签”页面,然后点击“新建标签”。出于某种原因,它需要标签描述,所以我只复制标签名称。
  1. 构建成功完成后,转到您的站点,您应该会看到 hello world 消息。
  • 如果您的站点显示应用程序错误消息,您可以通过转到 Azure 门户中的 Web 应用程序,然后转到边栏中的日志流页面来检查错误日志。请注意,只有在有人访问网页后,应用程序容器才会启动。因此,要测试应用程序初始化,您必须先访问您的网页。

关于node.js - 如何在 Azure DevOps 中为 Node.js Web 应用程序创建发布管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52785852/

相关文章:

javascript - 如何从客户端向服务器发送数据

node.js - Redis在应用程序中的使用

node.js - grunt uglify 在父目录中找不到我的文件

git - 排队时如何使用自托管代理清理构建

docker - 获取Build.Repository.LocalPath的数据并在我的DockerFile中使用它

javascript - Mongoose 保存一个空对象

git - 从 Azure DevOps 访问本地 Bitbucket 服务器

azure-devops - 如何在 Azure DevOps 中读取发布管道中工件的目录路径?

azure-sql-database - 如何在下一个任务中使用 SqlAzureDacpacDeployment@1 任务结果

.net - Azure管道: Error NETSDK1139: The target platform identifier android was not recognized