0
votes

I'm trying to run grunt tasks through a batch script and am calling grunt as follows:

call npm install
call npm install grunt

However, if this returns an error, then the VSTS build on the hosted build agent still shows up as successful (even with a logged error in the script). Does anyone have any good examples of how to get it to return an error to the build?

I've been looking at using powershell, but without any luck so far, with code as follows:

In gruntfile.js:

grunt.initConfig({
    shell: {
        ps: {
            options: {
                stdout: true
            },
            command: 'powershell ../../errors.ps1'
        }
    }
});

grunt.registerTask('default',  function() {
    try {
                    grunt.task.run([
                    'less:desktop',
                    'less:tablet',
                    'less:smartphone',
                    'less:homepage_desktop',
                    'less:homepage_tablet']);
    } catch(e) {
                    grunt.task.run([
                    'shell:ps']);
                    throw e;

    }
}); 

In errors.ps1:

$URL_Format_Error = [string]"Error found in running grunt. Please investigate grunt logs"
    Write-Error $URL_Format_Error -ErrorAction:Stop
    return

The code run in the exception handler never gets called, and a warning is output with a compilation error in the .less file, but the powershell is never run. Is there a way I can hook into the warning perhaps, and run my powershell then?

As an alternative, when I try to add a grunt task to the VSTS build definition after a batch script to run the NPM install, I just keep getting the following error (even after seeing a successful NPM installation in the batch script):

Fatal error: Unable to find local grunt

Hence, I'm not sure if I can run the grunt task in a separate task the VSTS build definition, if I'm using a hosted build agent. I'm inclined to think that would only work if I had my own build server.

1

1 Answers

0
votes

Just try to use Write-Error in combination with an exit 1 in your script.

 Write-Error ("Some error")
 exit 1

Reference this thread : How to fail the build from a PowerShell task in TFS 2015