From f2bc69c4978051fac3951722960e9c60592971d4 Mon Sep 17 00:00:00 2001 From: mrorigo Date: Mon, 18 Jan 2016 04:17:54 +0100 Subject: [PATCH 1/5] Added buildArgs option --- tasks/docker_io.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tasks/docker_io.js b/tasks/docker_io.js index 9a4848e..ccb5d29 100644 --- a/tasks/docker_io.js +++ b/tasks/docker_io.js @@ -93,6 +93,12 @@ module.exports = function(grunt) { var buildName = getBase(); buildOpts.push('-t') buildOpts.push(buildName + ':' + opts.tag[0]) + if(opts.buildArgs) { + opts.buildArgs.forEach(function(ba) { + buildOpts.push('--build-arg'); + buildOpts.push(ba); + }); + } buildOpts.push(opts.dockerFileLocation) console.log(buildOpts.join(' ')) var dockerBuild = spawn('docker', buildOpts) From 9a6a05ac39521da3fe17e051eb65aa84d245a5be Mon Sep 17 00:00:00 2001 From: origo Date: Mon, 18 Jan 2016 21:36:37 +0100 Subject: [PATCH 2/5] Update README.md Added example buildArgs to usage example --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ed62ef8..abd4330 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ grunt.initConfig({ options: { dockerFileLocation: '.', buildName: 'dockerIO', + buildArgs: ['version=' + process.env.VERSION] tag: 'latest', pushLocation: 'https://hub.docker.io', username: 'drgrove', From ad1aee5ab6e25cc0532ae7290f3a2da10a89c87b Mon Sep 17 00:00:00 2001 From: origo Date: Mon, 18 Jan 2016 21:40:54 +0100 Subject: [PATCH 3/5] Fixed indentation fail --- tasks/docker_io.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tasks/docker_io.js b/tasks/docker_io.js index ccb5d29..61efe2c 100644 --- a/tasks/docker_io.js +++ b/tasks/docker_io.js @@ -93,12 +93,12 @@ module.exports = function(grunt) { var buildName = getBase(); buildOpts.push('-t') buildOpts.push(buildName + ':' + opts.tag[0]) - if(opts.buildArgs) { - opts.buildArgs.forEach(function(ba) { - buildOpts.push('--build-arg'); - buildOpts.push(ba); - }); - } + if(opts.buildArgs) { + opts.buildArgs.forEach(function(ba) { + buildOpts.push('--build-arg'); + buildOpts.push(ba); + }); + } buildOpts.push(opts.dockerFileLocation) console.log(buildOpts.join(' ')) var dockerBuild = spawn('docker', buildOpts) From 227841236ae0689fe33d41344355acf1b726eeac Mon Sep 17 00:00:00 2001 From: Samuel Olsson Date: Mon, 25 Jan 2016 13:19:28 +0100 Subject: [PATCH 4/5] Build once, tag multiple times --- tasks/docker_io.js | 175 ++++++++++++++++++++++++++------------------- 1 file changed, 103 insertions(+), 72 deletions(-) diff --git a/tasks/docker_io.js b/tasks/docker_io.js index 61efe2c..37c2f7f 100644 --- a/tasks/docker_io.js +++ b/tasks/docker_io.js @@ -8,8 +8,8 @@ 'use strict'; -var spawn = require('child_process').spawn -var fs = require('fs') +var spawn = require('child_process').spawn; +var fs = require('fs'); module.exports = function(grunt) { @@ -18,8 +18,8 @@ module.exports = function(grunt) { grunt.registerMultiTask('docker_io', 'Build and Push Docker Images', function() { // Merge task-specific and/or target-specific options with these defaults. - var DOCKER_HUB_URL = "http://hub.docker.io" - var REQUIRES_LOGIN = "Please login prior to push:" + var DOCKER_HUB_URL = "http://hub.docker.io"; + var REQUIRES_LOGIN = "Please login prior to push:"; var opts = this.options({ dockerFileLocation: '.', buildName: '', @@ -34,113 +34,144 @@ module.exports = function(grunt) { var queue = []; var next = function() { if(!queue.length) { - return done() + return done(); } queue.shift()(); - } + }; var runIf = function(condition, behavior){ if(condition) { - queue.push(behavior) + queue.push(behavior); } - } + }; var getBase = function(){ - var buildName + var buildName; if(opts.pushLocation === DOCKER_HUB_URL) { - buildName = opts.username + '/' + opts.buildName + buildName = opts.username + '/' + opts.buildName; } else { - buildName = opts.pushLocation + '/' + opts.buildName + buildName = opts.pushLocation + '/' + opts.buildName; } return buildName; - } + }; // Check that user is logged in runIf(true, function(){ - var loginOpts = ['login'] + var loginOpts = ['login']; if(opts.pushLocation !== DOCKER_HUB_URL) { - loginOpts.push(opts.pushLocation) + loginOpts.push(opts.pushLocation); } - var dockerLogin = spawn('docker', loginOpts) + var dockerLogin = spawn('docker', loginOpts); dockerLogin.stdout.on('data', function(data){ - data = data || '' - var usernameRegex = /\(.*\)/ + data = data || ''; + var usernameRegex = /\(.*\)/; if(usernameRegex.exec(data) && usernameRegex.exec(data).length > 0) { if(usernameRegex.exec(data)[0] !== '(' + opts.username + ')'){ - grunt.fatal('Please Login First') + grunt.fatal('Please Login First'); } - next() + next(); } else { - grunt.fatal('Please login to the docker registry - ' + opts.pushLocation) + grunt.fatal('Please login to the docker registry - ' + opts.pushLocation); } - }) - }) + }); + }); + + if(typeof opts.tag === 'string') { + opts.tag = opts.tag.split(','); + } - if( typeof opts.tag === 'string') - opts.tag = opts.tag.split(',') if(opts.tag === '' || opts.tag === [] || opts.tag === 'latest') { opts.tag = []; - opts.tag.push('latest') + opts.tag.push('latest'); } - var tagCount = opts.tag.length; - for(var i = 0; i < tagCount; i++) { - runIf - ( opts.dockerFileLocation !== '' - && opts.buildName !== '' - , function(i){ - var buildOpts = ['build'] - var buildName = getBase(); - buildOpts.push('-t') - buildOpts.push(buildName + ':' + opts.tag[0]) - if(opts.buildArgs) { - opts.buildArgs.forEach(function(ba) { - buildOpts.push('--build-arg'); - buildOpts.push(ba); - }); + + runIf( + opts.dockerFileLocation !== '' && + opts.buildName !== '', + function() { + var buildOpts = ['build']; + var buildName = getBase(); + + buildOpts.push('-t'); + buildOpts.push(buildName + ':' + opts.tag[0]); + if(opts.buildArgs) { + opts.buildArgs.forEach(function(ba) { + buildOpts.push('--build-arg'); + buildOpts.push(ba); + }); + } + buildOpts.push(opts.dockerFileLocation); + console.log(buildOpts.join(' ')); + var dockerBuild = spawn('docker', buildOpts); + dockerBuild.stdout.on('data', function(data){ + grunt.log.ok(data); + }); + dockerBuild.stderr.on('data', function(data){ + grunt.fatal('Could not build image - ' + data); + }); + dockerBuild.on('exit', function(code){ + if(code === 0) { + next(); + } else { + grunt.fatal('Error Building image'); } - buildOpts.push(opts.dockerFileLocation) - console.log(buildOpts.join(' ')) - var dockerBuild = spawn('docker', buildOpts) - dockerBuild.stdout.on('data', function(data){ - grunt.log.ok(data) - }) - dockerBuild.stderr.on('data', function(data){ - grunt.fatal('Could not build image - ' + data) - }) - dockerBuild.on('exit', function(code){ - if(code === 0) { - opts.tag.shift() - next() - } else { - grunt.fatal('Error Building image') + }); + } + ); + + for (var i = 1 ; i < opts.tag.length ; i++) { + runIf(true, (function(primaryTag, newTag) { + return function() { + var tagOpts = ['tag']; + var buildName = getBase(); + + tagOpts.push('-f'); + tagOpts.push(buildName + ':' + primaryTag); + tagOpts.push(buildName + ':' + newTag); + + console.log(tagOpts.join(' ')); + var dockerTag = spawn('docker', tagOpts); + dockerTag.stdout.on('data', function(data) { + grunt.log.ok(data); + }); + dockerTag.stderr.on('data', function(data) { + grunt.fatal('Could not tag image - ' + data); + }); + dockerTag.on('exit', function(code) { + if (code === 0) { + next(); } - }) - } - ) + else { + grunt.fatal('Error Tagging image'); + } + }); + }; + })(opts.tag[0], opts.tag[i])); } + runIf(opts.push, function(){ - var pushOpts = [] - pushOpts.push('push') - pushOpts.push(getBase()) - var dockerPush = spawn('docker', pushOpts) + var pushOpts = []; + pushOpts.push('push'); + pushOpts.push(getBase()); + var dockerPush = spawn('docker', pushOpts); dockerPush.stdout.on('data', function(data){ if(data === REQUIRES_LOGIN) { - grunt.fatal('You must first login ') + grunt.fatal('You must first login '); } - grunt.log.ok(data) - }) + grunt.log.ok(data); + }); dockerPush.stderr.on('data', function(data){ - grunt.log.error(data) - }) + grunt.log.error(data); + }); dockerPush.on('exit', function(code){ if(code !== 0) { - grunt.fatal('Could not push docker image ' + opts.buildName) + grunt.fatal('Could not push docker image ' + opts.buildName); } - next() - }) - }) + next(); + }); + }); - next() + next(); }); }; From 56e5780a5452b4afe6a7b3d8f3defed09471198a Mon Sep 17 00:00:00 2001 From: mrorigo Date: Thu, 11 Feb 2016 02:12:11 +0100 Subject: [PATCH 5/5] Removed deprecated -f flag from tag command --- tasks/docker_io.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tasks/docker_io.js b/tasks/docker_io.js index 37c2f7f..3939daf 100644 --- a/tasks/docker_io.js +++ b/tasks/docker_io.js @@ -126,7 +126,6 @@ module.exports = function(grunt) { var tagOpts = ['tag']; var buildName = getBase(); - tagOpts.push('-f'); tagOpts.push(buildName + ':' + primaryTag); tagOpts.push(buildName + ':' + newTag);