yzvbvw
Last Updated: February 25, 2016
·
3.089K
· coma

Defining and running Gulp tasks for each environment on the fly

On a recent project we were deploying to parse.com using Gulp and I found this idea to be very useful:

var gulp     = require('gulp');
var path     = require('path');
var sequence = require('run-sequence');

var environments = ['mine', 'beta', 'prod', 'test'];

var deploy = function (environment, done) {

    var dir = path.join(__dirname, '..', 'environments', environment);

    gulp.task('config', function () {

        // The config stuff
    });

    gulp.task('minify', function () {

        // The minify stuff
    });

    sequence('clean', 'config', 'minify', 'deploy', done);
};

environments.forEach(function (environment) {

    gulp.task('deploy:' + environment, function (done) {

        deploy(environment, done);
    });
});

So basically the tasks are not there till you run one "main" task like deploy:beta.

What do you think?

6 Responses
Add your response

16310

good thinking. we actually use this method as well to deploy at NewsCred

over 1 year ago ·
16325

@riyadhalnur, thank you for the feedback!

over 1 year ago ·
16437

Excellent idea! This works great.

over 1 year ago ·
16796

@azanebrain, thank you!

over 1 year ago ·
21517

How do you single out a task if you were to run in alone for a specific env?

over 1 year ago ·
21522

hello @afhammad!,

The scoped tasks are isolated and can't be reached without running their parent task, check this https://gist.github.com/coma/55692304b951945c4c49

as you can see there, common tasks like clean and index are not wrapped as others like deploy > script

cheers

over 1 year ago ·