npmci/dist/npmci.build.docker.js

135 lines
16 KiB
JavaScript
Raw Normal View History

2016-06-04 21:20:39 +00:00
"use strict";
var plugins = require("./npmci.plugins");
2016-06-05 02:48:39 +00:00
var NpmciEnv = require("./npmci.env");
2016-06-04 21:20:39 +00:00
exports.build = function () {
var done = plugins.q.defer();
2016-06-05 05:41:14 +00:00
plugins.gulp.src("./Dockerfile*")
2016-06-05 05:17:02 +00:00
.pipe(readDockerfiles())
2016-06-05 04:20:05 +00:00
.pipe(plugins.gulpFunction(function () {
sortDockerfiles()
.then(buildDockerfiles)
.then(done.resolve);
}, "atEnd"));
2016-06-04 21:20:39 +00:00
return done.promise;
};
2016-06-05 02:48:39 +00:00
var readDockerfiles = function () {
2016-06-05 05:50:04 +00:00
return plugins.through2.obj(function (file, enc, cb) {
var myDockerfile = new Dockerfile({
2016-06-05 02:48:39 +00:00
filePath: file.path,
read: true
});
NpmciEnv.dockerFiles.push(myDockerfile);
cb(null, file);
2016-06-05 05:17:02 +00:00
});
};
2016-06-05 04:20:05 +00:00
var sortDockerfiles = function () {
var done = plugins.q.defer();
var redoSort;
var sortFunction = function () {
redoSort = false;
var notYetBuiltImages = [];
NpmciEnv.dockerFiles.forEach(function (dockerFileArg) {
notYetBuiltImages.push(dockerFileArg.cleanTag);
});
NpmciEnv.dockerFiles.sort(function (a, b) {
plugins.beautylog.log("sort build order for Dockerimages");
var aIndex = notYetBuiltImages.indexOf(a.cleanTag);
if (aIndex != -1) {
notYetBuiltImages.splice(aIndex, 1);
}
if (notYetBuiltImages.indexOf(b.cleanTag) != -1) {
redoSort = true;
return -1;
}
else {
return 0;
}
});
if (redoSort) {
sortFunction();
}
else {
done.resolve();
}
2016-06-05 02:48:39 +00:00
};
2016-06-05 04:20:05 +00:00
sortFunction();
return done.promise;
};
var buildDockerfiles = function () {
var done = plugins.q.defer();
NpmciEnv.dockerFiles.forEach(function (dockerfileArg) {
dockerfileArg.build();
});
done.resolve();
return done.promise;
2016-06-05 02:48:39 +00:00
};
var Dockerfile = (function () {
function Dockerfile(options) {
this.filePath = options.filePath;
this.repo = NpmciEnv.repo.user + "/" + NpmciEnv.repo.repo;
this.version = dockerFileVersion(plugins.path.parse(options.filePath).base);
2016-06-05 04:20:05 +00:00
this.cleanTag = this.repo + ":" + this.version;
2016-06-05 02:48:39 +00:00
if (options.filePath && options.read) {
this.content = plugins.smartfile.local.toStringSync(plugins.path.resolve(options.filePath));
}
;
this.baseImage = dockerBaseImage(this.content);
}
;
Dockerfile.prototype.build = function () {
if (!this.buildTag) {
var tag = exports.dockerTag(this.repo, this.version);
plugins.shelljs.exec("docker build -t " + tag + " -f " + this.filePath + " .");
this.buildTag = tag;
NpmciEnv.dockerFilesBuilt.push(this);
}
else {
plugins.beautylog.error("This Dockerfile already has been built!");
}
2016-06-05 02:48:39 +00:00
};
;
Dockerfile.prototype.push = function () {
if (this.buildTag) {
plugins.shelljs.exec("docker push " + this.buildTag);
}
else {
plugins.beautylog.error("Dockerfile hasn't been built yet!");
}
};
return Dockerfile;
}());
exports.Dockerfile = Dockerfile;
var dockerFileVersion = function (dockerfileNameArg) {
var versionString;
var versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
var regexResultArray = versionRegex.exec(dockerfileNameArg);
if (regexResultArray.length = 2) {
versionString = regexResultArray[1];
}
else {
versionString = "latest";
}
return versionString;
};
var dockerBaseImage = function (dockerfileContentArg) {
var baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n/;
var regexResultArray = baseImageRegex.exec(dockerfileContentArg);
return regexResultArray[1];
};
exports.dockerTag = function (repoArg, versionArg) {
var tagString;
var registry = NpmciEnv.dockerRegistry;
if (process.env.CI_BUILD_STAGE == "test") {
registry = "registry.gitlab.com";
}
var repo = repoArg;
var version = versionArg;
if (process.env.CI_BUILD_STAGE == "test" || process.env.CI_BUILD_STAGE == "build") {
version = version + "_test";
}
tagString = registry + "/" + repo + ":" + version;
return tagString;
};
2016-06-04 21:20:39 +00:00
2016-06-05 05:50:04 +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