npmci/dist/npmci.build.docker.js

124 lines
16 KiB
JavaScript

"use strict";
var plugins = require("./npmci.plugins");
var NpmciEnv = require("./npmci.env");
exports.build = function () {
var done = plugins.q.defer();
plugins.gulp.src("./Dockerfile*")
.pipe(readDockerfiles())
.pipe(plugins.gulpFunction(function () {
sortDockerfiles()
.then(buildDockerfiles)
.then(done.resolve);
}, "atEnd"));
return done.promise;
};
var readDockerfiles = function () {
return plugins.through2.obj(function (file, enc, cb) {
var myDockerfile = new Dockerfile({
filePath: file.path,
read: true
});
NpmciEnv.dockerFiles.push(myDockerfile);
cb(null, file);
});
};
var sortDockerfiles = function () {
var done = plugins.q.defer();
NpmciEnv.dockerFiles.sort(function (a, b) {
var cleanTags = [];
NpmciEnv.dockerFiles.forEach(function (dockerfileArg) {
cleanTags.push(dockerfileArg.cleanTag);
});
var aIndex = cleanTags.indexOf(a.baseImage);
var bIndex = cleanTags.indexOf(b.cleanTag);
console.log(cleanTags);
console.log(a.baseImage, aIndex);
console.log(b.cleanTag, bIndex);
if (aIndex < bIndex && bIndex != -1) {
return 1;
}
else {
return 0;
}
});
done.resolve();
return done.promise;
};
var buildDockerfiles = function () {
var done = plugins.q.defer();
NpmciEnv.dockerFiles.forEach(function (dockerfileArg) {
dockerfileArg.build();
});
done.resolve();
return done.promise;
};
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);
this.cleanTag = this.repo + ":" + this.version;
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!");
}
};
;
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 && 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 == "build" || process.env.CI_BUILD_STAGE == "test") {
registry = "registry.gitlab.com";
}
var repo = repoArg;
var version = versionArg;
if (process.env.CI_BUILD_STAGE == "build" || process.env.CI_BUILD_STAGE == "test") {
version = version + "_test";
}
tagString = registry + "/" + repo + ":" + version;
return tagString;
};
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5wbWNpLmJ1aWxkLmRvY2tlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsSUFBWSxPQUFPLFdBQU0saUJBQ3pCLENBQUMsQ0FEeUM7QUFDMUMsSUFBWSxRQUFRLFdBQU0sYUFBYSxDQUFDLENBQUE7QUFHN0IsYUFBSyxHQUFHO0lBQ2YsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixPQUFPLENBQUMsSUFBSSxDQUFDLEdBQUcsQ0FBQyxlQUFlLENBQUM7U0FDNUIsSUFBSSxDQUFDLGVBQWUsRUFBRSxDQ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