yzvbvw
Last Updated: February 25, 2016
·
2.887K
· coma
258c0521bb1b413d55346b74baf9d7ee

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?

Say Thanks
Respond

6 Responses
Add your response

16310
10309058 10152457564452360 4465802230248535815 n

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

over 1 year ago ·
16325
258c0521bb1b413d55346b74baf9d7ee

@riyadhalnur, thank you for the feedback!

over 1 year ago ·
16437
B59f1b9ee892c9503057b98123fd70a2

Excellent idea! This works great.

over 1 year ago ·
16796
258c0521bb1b413d55346b74baf9d7ee

@azanebrain, thank you!

over 1 year ago ·
21517
F3b55195d5f885c4987868ab730fe6cd

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

over 1 year ago ·
21522
258c0521bb1b413d55346b74baf9d7ee

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 ·
Filed Under