spauny
spauny

Reputation: 5096

Grunt - how to run common external tasks from external projects

I just started using Grunt and I want to have a common project where I can define common tasks. After that, each team can create their own project and be able to add my common project as a dependency and run the common tasks.

Also helpful when you want to add some predefined tasks to your FE build pipeline. When someone pushes that project to github and tries to build it using, let's say, Jenkins, then I want to also run some other tasks by default (like jslint, uglify, compress, nexus deploy etc).

I tried doing this by creating a grunt project with all those common tasks and then add it as a dependency to other projects. The problem is I can't run the common tasks from the external project. Is there another way to achieve this?

Upvotes: 1

Views: 230

Answers (1)

hereandnow78
hereandnow78

Reputation: 14434

not a huge fan of of trying to put all your tasks into one repository and pull them in, but it is possible.

i haven't done that before (i have done loadTasks from directory, and it just works), and i don't like it. but here are 2 possibilities i see:

1. Using submodules

you could use git submodules to pull your tasks-repo into a folder of your projects repo, and load your tasks from their.

Loading Tasks from a Folder is done this way (loads all files in this folder!):

grunt.loadTasks "./your-git-submodule-folder"

2. Using 1 Git-Repo and installing it via NPM

Create a Repository with as much tasks as you want. one task should reside in 1 file (it works with only 1 file, but i wouldn't recommend it)

// task1.js
grunt.registerMultiTask(task1", "run task1", function() {
  // do stuff here      
});

// task2.js
grunt.registerMultiTask(task2", "run task2", function() {

});

then you can install the git-repo as a simple-dev-dependency in your project(s):

$ npm install git+ssh://your-tasks-repo.git --save-dev

for this to work, your Gruntfile needs to have targets configured for both tasks


What i would do

I for myself would create a single git-repo for every single task, and install every single task separatly into projects where i need them (npm install from git-repo).

publish tasks which could be useful for the community to the npm-registry. use grunt-init gruntplugin for creating new tasks.


Regarding your Build-Process:

just add an alias-task to your gruntfile with all the tasks you want to be run on a new commit:

grunt.registerTask('jenkins', ['jslint', 'compress', ...]);

and configure your jenkins to run this task (you need to add a git-hook und Source-Code-Management in your Projects-Config), and configure a shell Script as Build-Step:

npm install && grunt jenkins

or even better, add a scripts-section to your package.json and use a test-command.

i created a much better answer on this topic, which unfortunatly was deleted (i don't know why, it had a lot of upvotes...):

https://stackoverflow.com/questions/14722649/continuous-integration-in-nodejs/15657588#15657588

here is another answer which is about custom-task development:

Grunt: custom task development how-to

Upvotes: 1

Related Questions