From 66876a2c850ef5652b9139fe062f66695a7f2b50 Mon Sep 17 00:00:00 2001 From: Akis Kesoglou Date: Wed, 10 May 2017 22:02:52 +0300 Subject: [PATCH] Add ability to specify built-time variables for local build Change-Type: patch --- CHANGELOG.md | 1 + build/utils/docker.js | 31 ++++++++++++++++++++++++++++++- lib/utils/docker.coffee | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9175446f..1bfa21c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Add uploading of build logs when present with resin deploy - Highlight cache usage in a local build - Show a progress bar for upload progress +- Add ability to specify build-time variables for local builds ### Fixed diff --git a/build/utils/docker.js b/build/utils/docker.js index 92443602..8e5fd78a 100644 --- a/build/utils/docker.js +++ b/build/utils/docker.js @@ -1,5 +1,5 @@ // Generated by CoffeeScript 1.12.5 -var cacheHighlightStream, generateConnectOpts, tarDirectory; +var cacheHighlightStream, generateConnectOpts, parseBuildArgs, tarDirectory; exports.appendOptions = function(opts) { return opts.concat([ @@ -35,6 +35,11 @@ exports.appendOptions = function(opts) { parameter: 'tag', description: 'The alias to the generated image', alias: 't' + }, { + signature: 'arg', + parameter: 'arg', + description: 'Set a build-time variable (eg. "-e \'ARG=value\'"). Can be specified multiple times.', + alias: 'e' }, { signature: 'nocache', description: "Don't use docker layer caching when building", @@ -122,6 +127,25 @@ cacheHighlightStream = function() { }); }; +parseBuildArgs = function(args, onError) { + var _, obj; + _ = require('lodash'); + if (!_.isArray(args)) { + args = [args]; + } + obj = {}; + args.forEach(function(str) { + var pair; + pair = /^([^\s]+?)=(.*)$/.exec(str); + if (pair != null) { + return obj[pair[1]] = pair[2]; + } else { + return onError(str); + } + }); + return obj; +}; + exports.runBuild = function(params, options, getBundleInfo, logStreams) { var Promise, dockerBuild, doodles, es, logging, logs, resolver; Promise = require('bluebird'); @@ -187,6 +211,11 @@ exports.runBuild = function(params, options, getBundleInfo, logStreams) { if (options.nocache != null) { opts['nocache'] = true; } + if (options.arg != null) { + opts['buildargs'] = parseBuildArgs(options.arg, function(arg) { + return logging.logWarn(logStreams, "Could not parse variable: '" + arg + "'"); + }); + } return builder.createBuildStream(opts, hooks, reject); }); }); diff --git a/lib/utils/docker.coffee b/lib/utils/docker.coffee index b1fe9804..ffd19b91 100644 --- a/lib/utils/docker.coffee +++ b/lib/utils/docker.coffee @@ -47,6 +47,12 @@ exports.appendOptions = (opts) -> description: 'The alias to the generated image' alias: 't' }, + { + signature: 'arg' + parameter: 'arg' + description: 'Set a build-time variable (eg. "-e \'ARG=value\'"). Can be specified multiple times.' + alias: 'e' + }, { signature: 'nocache' description: "Don't use docker layer caching when building" @@ -128,6 +134,19 @@ cacheHighlightStream = -> data = colors.bgGreen.black(msg) return data + EOL +parseBuildArgs = (args, onError) -> + _ = require('lodash') + if not _.isArray(args) + args = [ args ] + obj = {} + args.forEach (str) -> + pair = /^([^\s]+?)=(.*)$/.exec(str) + if pair? + obj[pair[1]] = pair[2] + else + onError(str) + return obj + # Pass in the command line parameters and options and also # a function which will return the information about the bundle exports.runBuild = (params, options, getBundleInfo, logStreams) -> @@ -206,6 +225,9 @@ exports.runBuild = (params, options, getBundleInfo, logStreams) -> opts['t'] = options.tag if options.nocache? opts['nocache'] = true + if options.arg? + opts['buildargs'] = parseBuildArgs options.arg, (arg) -> + logging.logWarn(logStreams, "Could not parse variable: '#{arg}'") builder.createBuildStream(opts, hooks, reject)