Last Updated: February 25, 2016
· 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


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

over 1 year ago ·

@riyadhalnur, thank you for the feedback!

over 1 year ago ·

Excellent idea! This works great.

over 1 year ago ·

@azanebrain, thank you!

over 1 year ago ·

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

over 1 year ago ·

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


over 1 year ago ·