Azure DevOps Pipeline 全局包安装最佳实践

And*_*rew 5 node.js npm azure-devops azure-pipelines

对于我们的管道,我们在同一台计算机上安装了两个 Windows 自托管代理。下面列出了我们的主要前端管道 .yml。这工作得很好,除了由于某种原因npm install没有得到 nx 或玩笑。为了解决这个问题,我们只需在每个代理的管道中运行一次npm install -g nx即可。npm 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
Run Code Online (Sandbox Code Playgroud)
{
  "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",
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我尝试将"nx": "^10.3.0"和添加"@nrwl/cli": "^10.3.0"到开发依赖项中,然后在安装后运行npm run nx test. 它给了我npm ERR! missing script: nx。是我刚刚没有添加正确吗?

And*_*rew 0

我最终采用了马克斯·伊万诺夫的解决方案。我们刚刚将 nx 和别名添加到 package.json 并将管道脚本切换为使用npm run nx ...。

这对我们来说非常有效,但我要感谢 Krzysztof Madej 和 julie-ng 的回答。两者都是非常合理的,并且能够达到我们想要的效果。我最终没有选择他们,因为它们也不适合我们的特定用例。如果有人正在浏览这篇文章,我建议您查看所有答案并选择最适合您的答案。