首页 > 解决方案 > Azure DevOps 自定义任务扩展:powershell.exe / node.exe 退出代码:5

问题描述

我将 PowerShell 脚本作为构建管道中的自定义任务。这些任务的步骤是:

  1. 签出存储库分支
  2. 在这些分支中编译解决方案
  3. 将结帐和编译结果复制到网络内的服务器

(由于一些遗留的东西我不能直接在服务器上编译解决方案)

为了使这些任务更美观,我将此 PowerShell 脚本包装在自定义构建任务中。

index.ts看起来像:

import tl = require('azure-pipelines-task-lib/task');

async function run() {
    try {
        //PARAMETERS
        var params: string[] = ['Major', 'Minor', 'Work', 'Share', 'RepositoryUri', 'Branch', 'ProjectConfig', 'Include', 'Exclude'];
        var required: boolean[] = [true, true, true, true, false, false, false, true, false];
        var paramList: string[] = [];

        //LOOP ALL PARAMETERS
        for (let i = 0; i < params.length; i++) {
            var item: string = tl.getInput(params[i], required[i]) || '';
            if (item != null && item != '') paramList.push('-' + params[i] + ' ' + item.replace(/(?:\r\n|\r|\n)/g, ','));
        }

        //START CHILD PROCESS
        var spawn = require('child_process').spawn, child;
        console.log('##[command][js] call: powershell.exe ' + __dirname + '/DeployGit.ps1 ' + paramList.join(' '))
        child = spawn('powershell.exe', [__dirname + '/DeployGit.ps1 ' + paramList.join(' ')]);

        //REDIRECT CONSOLE OUTPUT
        child.stdout.on('data', function (data: string) { console.log(data.toString()); });
        child.stderr.on('data', function (data: string) { console.log(data.toString()); });
        child.on('exit', function (code: number) { console.log('##[command][js] powershell exit code:', code); process.exit(code) });
        child.stdin.end(); //END INPUT
    }
    catch (err) { tl.setResult(tl.TaskResult.Failed, err.message); process.exit(-1) }
}

run();

所以这个自定义任务的唯一工作就是调用 PowerShell 脚本。


问题

如果我使用 PowerShell Buildpipeline 任务执行 PowerShell 脚本,一切都很好。任务大约需要20 分钟,但一切正常。

如果我执行包装的自定义任务,该任务在任务的3. 阶段约 11-12 分钟后抛出错误(将结帐和编译结果复制到网络内的服务器

在此处输入图像描述


错误信息

[ps1] copy items from 'D:\AzureDevOpsData\DeployGit\Folder' to '\\my-server\DeployGit' # <- LAST EXECUTET COMMAND [Copy-Item $Work -Destination $Share -Recurse -Force]

##[command][js] powershell exit code: 5
##[error]Der Exitcode 5 wurde vom Prozess zurückgegeben: Dateiname "D:\AzureDevOpsData\AgentA\externals\node\bin\node.exe", Argumente ""D:\AzureDevOpsData\AgentA\_work\_tasks\DeployGit_ff191cd0-69d5-402d-aa18-9566fb6c511c\1.0.6\index.js"".
##[debug]Microsoft.VisualStudio.Services.Agent.Util.ProcessExitCodeException: Der Exitcode 5 wurde vom Prozess zurückgegeben: Dateiname "D:\AzureDevOpsData\AgentA\externals\node\bin\node.exe", Argumente ""D:\AzureDevOpsData\AgentA\_work\_tasks\DeployGit_ff191cd0-69d5-402d-aa18-9566fb6c511c\1.0.6\index.js"".
   at Microsoft.VisualStudio.Services.Agent.Util.ProcessInvoker.ExecuteAsync(String workingDirectory, String fileName, String arguments, IDictionary`2 environment, Boolean requireExitCodeZero, Encoding outputEncoding, Boolean killProcessOnCancel, IList`1 contentsToStandardIn, Boolean inheritConsoleHandler, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Services.Agent.ProcessInvokerWrapper.ExecuteAsync(String workingDirectory, String fileName, String arguments, IDictionary`2 environment, Boolean requireExitCodeZero, Encoding outputEncoding, Boolean killProcessOnCancel, IList`1 contentsToStandardIn, Boolean inheritConsoleHandler, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Services.Agent.Worker.Handlers.DefaultStepHost.ExecuteAsync(String workingDirectory, String fileName, String arguments, IDictionary`2 environment, Boolean requireExitCodeZero, Encoding outputEncoding, Boolean killProcessOnCancel, Boolean inheritConsoleHandler, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Services.Agent.Worker.Handlers.NodeHandler.RunAsync()
   at Microsoft.VisualStudio.Services.Agent.Worker.TaskRunner.RunAsync()
   at Microsoft.VisualStudio.Services.Agent.Worker.StepsRunner.RunStepAsync(IStep step, CancellationToken jobCancellationToken)
##[section]Abschließen: Task: DeployGit.ps1

我对错误消息的解释是node.exe使用退出代码 5 引发错误。

本文中,Windows 使用错误代码 5 表示Access is denied. 但它更像是node.exe出于任何原因无法处理长复制过程。


结论

我在很多情况下都使用了自定义包装的任务,这是我第一次遇到问题,可能与执行时间长有关?

对于长期且非常具体的问题,我很抱歉,我只希望其他一些开发人员遇到类似的情况,因为我不知道这里发生了什么。

标签: node.jsazure-devopsazure-pipelines

解决方案


代替使用 TypeScript 包装 PowerShell 脚本,您可以直接在自定义构建任务中使用 PS 脚本。

task.json您需要以这种方式配置它:

"execution": {
    "PowerShell3": {
        "target": "your-script.ps1",
         "workingDirectory": "$(currentDirectory)"
    }
 }

您可以在repo 中查看如何处理 PowerShell 脚本中的输入。


推荐阅读