Search code examples
gulpappveyor

How to send fail signal to Appveyor if gulp task fails?


I have created a very complex build process for front-end of one web app, which is being tested on Appveyor. If some parts of the app are not being built correctly with gulp, if some gulp tasks fail, how do I signal the Appveyor that the build has failed in its entirety?


Solution

  • To solve this problem, I have used instructions from this article. I had a need to separate build process into two similar parts: one for development environment and another one for production environment. Main difference was that production environment should always break if error is found in some of the tasks. Feodor Fitsner suggested that process should exit with non-zero error code.

    Combining these two solutions, I created this small JS module that should be used as a wrapper for gulp tasks:

    const msg = require('bit-message-box')
    const chalk = require('chalk')
    
    module.exports = (taskFn, production = false) => function(done) {
    	let onSuccess = () => {
    		done()
    	}
    
    	let onError = (err) => {
    		
    		if (production) {
    			// If build process is initiated in production env, it should always break
    			// on error with exit code higher than zero. This is especially important
    			// for Appveyor CI
    			msg.error(`ERROR! BUILD PROCESS ABORTED!`)
    			console.error(chalk.bgRed.white(err))
    			process.exit(1)
    		}
    		else { done() }
    	}
    
    	let outStream = taskFn(onSuccess, onError);
    
    	if (outStream && typeof outStream.on === 'function') {
    		outStream.on('end', onSuccess);
    	}
    }

    Then in gulp itself, you can import this module and use it in a following way:

    const gulp = require('gulp')
    const handleCI = require('./handleCI')
    const sass = require('gulp-sass')
    
    const PRODUCTION = true // use your own system to decide if this is true or false
    
    gulp.task('styles', handleCI((success, error) => {
      return gulp.src('./scss/style.scss')
        .pipe(
          sass()
            .on('error', error) // Add this to handle errors
        )
        .pipe(
          gulp.dest('./styles/')
            .on('error', error)
        )
    }, PRODUCTION))