azure - 在azure devops管道中执行gulp脚本

标签 azure npm gulp

在我的项目中,我有一个 gulpfile.js ,它应该将一些文件从一个目录复制到另一个目录。从 Visual Studio 中的 Task Runner Explorer 本地运行它是可行的。

当我想在 Azure DevOps 中执行这个 gulp 脚本时,它提示缺少包。

我的 gulpfile.js 看起来像这样:

/// <binding AfterBuild='moduleCopy, copyModuleDependencies' />

var gulp = require('gulp');
var newer = require('gulp-newer');
var rename = require('gulp-rename');


gulp.task('copyModuleDependencies', function (cd) {
    gulp.src('../*Module/bin/Release/netcoreapp3.0/*.dll')
        .pipe(rename({ dirname: '' }))
        .pipe(gulp.dest("bin/Release/netcoreapp3.0/Modules"))
});

gulp.task('moduleCopy', function (cb) {
    gulp.src('../*Module/bin/Release/netcoreapp3.0/*.Module.dll')
        .pipe(newer("bin/Release/netcoreapp3.0/Modules/"))
        .pipe(rename({ dirname: '' }))
        .pipe(gulp.dest("bin/Release/netcoreapp3.0/Modules/"))
});

我的 package.json 文件如下所示:

{

  "name": "MyHost",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "dependencies": {
    "gulp": "^4.0.2",
    "gulp-newer": "^1.4.0",
    "gulp-rename": "^1.4.0",
    "upath": "^1.2.0"
  },
  "devDependencies": {}
}

还有我的 azure-pipelines.yml 文件:

触发: - 功能/混淆

池: vmImage:'ubuntu-最新'

variables:
  buildConfiguration: 'Release'

steps:
- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'
- task: Npm@1
  inputs:
    command: 'install'
    workingDir: 'obfuscated.Host'
- task: Gulp@1
  inputs:
    gulpFile: 'obfuscated.Host/gulpfile.js'
    targets: 'moduleCopy copyModuleDependencies'
    enableCodeCoverage: false

当触发构建时,npm 似乎能够安装 package.json 文件中指定的包,但是当它应该执行 gulpfile.js 时,会发生这种情况:

内部/模块/cjs/loader.js:638 抛出错误; ^

Error: Cannot find module 'upath'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/vsts/work/1/s/obfuscated.Host/node_modules/chokidar/index.js:13:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
##[error]Gulp failed with error: /usr/local/bin/gulp failed with return code: 1
##[section]Finishing: Gulp

显然它似乎缺少packages.json中的一些包,但是为什么这在本地有效?

最佳答案

我相信这是因为您没有在项目目录中的管道 YAML 文件中运行 gulp 任务,而是在解决方案目录中运行。因为您在正确的路径中指定了“gulpfile.js”,所以它能够找到它并发现您的任务,但它随后会从您尚未设置的当前工作目录执行 gulp,因此它默认为解决方案目录。

尝试从 gulp 任务中删除 gulpFile 输入(因为它默认为“gulpfile.js”)并添加 workingDirectory 输入,设置为您的项目目录“obfuscated.Host”,例如:

variables:
  buildConfiguration: 'Release'

steps:
- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'
- task: Npm@1
  inputs:
    command: 'install'
    workingDir: 'obfuscated.Host'
- task: Gulp@1
  inputs:
    workingDirectory: 'obfuscated.Host'
    targets: 'moduleCopy copyModuleDependencies'
    enableCodeCoverage: false

关于azure - 在azure devops管道中执行gulp脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58323010/

相关文章:

node.js - Gulp/Node : error while loading shared libraries: cannot allocate memory in static TLS block

azure - 基于sharepoint的ADF事件触发

azure - 如何将完整的 TFS 2012 代码以及历史记录迁移到 Azure 中的 VSTS

asp.net-mvc - Azure 和 MVC2 Web 角色模板

node.js - Laradock 上的 Node+NPM

node.js - 使用 Bower 和 Gulp 的命名约定和文件放置

azure - 在 Dockerfile 中安装 Azure CLI 无法找到包 azure-cli

javascript - es6导入three.js

javascript - 将 fullcalendar 与 webpack 结合使用

javascript - 慢哈巴狗编译成 HTML