node.js - Azure DevOps Pipeline 全局包安装最佳实践

标签 node.js npm azure-devops azure-pipelines

对于我们的管道,我们在同一台计算机上安装了两个 Windows 自托管代理。下面列出了我们主要的前端管道 .yml。这工作正常,除了由于某些原因 npm install 没有得到 nx 或 jest。要解决此问题,我们只需在每个代理的管道中运行一次 npm install -g nxnpm install -g jest。第一次运行后一切正常,我们可以删除额外的安装以加快执行速度。但是,当发布新版本的 nx 或 jest 时,它不会更新,这绝对不是最佳做法。

我猜问题是这些需要全局安装才能工作,所以常规的 npm install 无法实现。我包含了一个稍微修改过的 package.json 版本,它显示包中包含 nx (@nrwl) 和 jest。有谁知道安装 nx 和 jest 的更好方法,而无需在每次管道构建时重新安装它们,并且在使用新代理首次运行后无需删除安装语句?预先感谢您提出任何建议,如果我可以提供任何其他信息,请告诉我。


trigger:
- none

pool: 'myPool'
variables:
  npm_config_cache: $(Pipeline.Workspace)/.npm

steps:
- checkout: self
  clean: true

- task: NodeTool@0
  inputs:
    versionSpec: '12.x'
  displayName: 'Install Node.js'

# Install stuff
- task: Npm@1
  timeoutInMinutes: 10
  inputs:
    command: 'install'
    workingDir: '$(System.DefaultWorkingDirectory)/ID_CLIENT'
  displayName: 'npm install'

# Test stuff
- script: nx affected:lint --base=origin/master --skip-nx-cache=true --parallel
  timeoutInMinutes: 10
  workingDirectory: '$(System.DefaultWorkingDirectory)/ID_CLIENT'
  displayName: 'run lint'

- script: 'jest --ci --reporters=default --reporters=jest-junit'
  timeoutInMinutes: 10
  workingDirectory: '$(System.DefaultWorkingDirectory)/ID_CLIENT'
  displayName: 'Run Tests'
  continueOnError: 'true'

# Build stuff
- script: nx affected:build --base=origin/master --skip-nx-cache=true --parallel
  timeoutInMinutes: 10
  workingDirectory: '$(System.DefaultWorkingDirectory)/ID_CLIENT'
  displayName: 'prod build'

# Publish test results to Azure Pipelines
- task: PublishTestResults@2
  timeoutInMinutes: 10
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '**/junit.xml' 
    searchFolder: '$(System.DefaultWorkingDirectory)'
    failTaskOnFailedTests: true
{
  "name": "project",
  "version": "1.0.0",
  "scripts": {
    "install:hard": "rimraf \"node_modules/!(rimraf)\" && npm cache clear --force && npm install",
    "install:globals": "npm install -g jest-cli @nrwl/cli",
    "postinstall": "ngcc"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.1.4",
    "@angular/common": "~9.1.4",
    "@angular/compiler": "~9.1.4",
    "@angular/core": "~9.1.4",
    "@angular/forms": "~9.1.4",
    "@angular/localize": "^9.1.6",
    "@angular/platform-browser": "~9.1.4",
    "@angular/platform-browser-dynamic": "~9.1.4",
    "@angular/router": "~9.1.4",
    "@ng-bootstrap/ng-bootstrap": "^6.1.0",
    "@ngneat/until-destroy": "^7.3.0",
    "angular-datatables": "^9.0.1",
    "angulartics2": "^9.1.0",
    "bootstrap": "^4.4.1",
    "core-js": "^3.1.4",
    "jest-junit": "^12.0.0",
  },
  "devDependencies": {
    "@nrwl/angular": "^10.3.0",
    "@nrwl/jest": "^10.3.0",
    "@nrwl/workspace": "^10.3.0",
    "jest": "^26.4.2",
    "jest-canvas-mock": "^2.3.0",
    "jest-cli": "^26.4.2",
    "jest-preset-angular": "^8.3.1",
    "jest-skipped-reporter": "0.0.5",
    "ts-jest": "^26.4.1",
    "typescript": "^3.8.3",
  }
}

编辑:我尝试添加 "nx": "^10.3.0""@nrwl/cli": "^10.3.0" 到开发依赖项,然后在安装后运行 npm run nx test。它给了我 npm ERR!缺少脚本:nx。我只是没有正确添加它吗?

最佳答案

好吧,如果您想让您的全局工具保持最新并避免在您的管道运行时每次都运行这些命令,请创建一个新的计划管道以每天运行一次并更新您的全局工具。

这里有 docs关于预定触发器。

但它可能看起来像这样:

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - master
  always: true

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '12.x'
  displayName: 'Install Node.js'
- script: npm install -g jest-cli @nrwl/cli

关于node.js - Azure DevOps Pipeline 全局包安装最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64827348/

相关文章:

npm 发布天蓝色工件

xamarin - Xamarin iOS 构建的 DevOps CI 错误 在钥匙串(keychain)中找不到有效的 iOS 代码签名 key

azure-devops - 如何构建用于测试和发布环境的 Azure DevOps Pipelines?

angularjs - 如何从 Angular cli 调用 Passportjs(node) API?

javascript - 在javascript中将对象展平为数组

node.js - Node http-server 在 Windows 10 Ubuntu Bash 上不起作用

node.js - Node Mongoose 如何获取模式的完整列表(文档和子文档)

node.js - 在 Heroku 上运行时找不到模块 './mergeConfig'

npm - 模块构建失败 : Error: `sass-loader` requires `node-sass` >=4. 请安装兼容版本

c# - 获取Azure Servicebus队列错误 "The argument namespaceConnectionString is null or white space.\r\nParameter name: namespaceConnectionString"