1

I have PowerShell Script as a custom task inside a Build pipeline. The steps of these tasks are:

  1. Checkout a repositroy branch
  2. Compile a solution in these branch
  3. Copy the checkout and the compiling results to a server inside the network

(because of some legacy stuff I can't compile the solution on the server directly)

To make these task more handsome, I wrap this PowerShell Script inside a custom build task.

The index.ts looks like:

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();

So the only job of this custom task is to call the PowerShell script.


The Problem

If I execute the PowerShell script with a PowerShell Buildpipeline Task, everything is fine. The Task takes about 20 min, but every thing works.

If I execute the wrapped custom task the task throw an error after ~11-12 min in the 3. phase of the task (Copy the checkout and the compiling results to a server inside the network)

enter image description here


The Error Message

[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

My interpretation of the error message is that node.exe thorws an error with the exit code 5.

In this article windows use error code 5 for Access is denied. But it more feeling like node.exe can't handle the long copy process for any reason.


Conclusion

I used the custom wrapped tasks in many cases and it is the first time that I have a problem, maybe it is relatet to the long execution time?

I'am sorry for the long and the very specific problem, I only hoped that some other developer run into a similar situation, cause I have no idea what is going on here.

12
  • 2
    Why do use typescript in the custom task? you can use powershell, maybe in native powershell task the error will not will be.. Commented Nov 26, 2019 at 8:37
  • 1
    Check my this repo: github.com/shayki5/azure-devops-create-pr-task Commented Nov 26, 2019 at 9:00
  • 1
    Yea, I think is not found in the docs... but you can also find few tutorials about it, if it will solve the error please update me :) Commented Nov 26, 2019 at 9:25
  • 1
    How do you pass the args, can you show an example? Commented Nov 27, 2019 at 7:02
  • 1
    In the input text out $(system.task....) not $env:.... Commented Nov 27, 2019 at 9:13

1 Answer 1

2

Instaed of wrap the PowerShell script with TypeScript you can use the PS script directly in your custom build task.

In the task.json you need to configure it in this way:

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

You can check in this repo how to handle the inputs in the PowerShell script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.