在天蓝色的devops管道中执行gulp脚本

问题描述 投票:0回答:1

在我的项目中,我有一个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-latest'

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中丢失了一些软件包,但是为什么这在本地可行?

azure npm gulp
1个回答
0
投票

我相信这是因为您不在项目目录中的管道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
© www.soinside.com 2019 - 2024. All rights reserved.