npmci/ts/npmci.build.docker.ts
2016-06-05 04:45:46 +02:00

90 lines
2.7 KiB
TypeScript

import * as plugins from "./npmci.plugins"
import * as NpmciEnv from "./npmci.env";
export let build = function(){
let done = plugins.q.defer();
done.resolve();
return done.promise;
}
let readDockerfiles = function(){
plugins.gulp.dest("./Dockerfile*")
.pipe(makeDockerfiles);
};
let makeDockerfiles = function(){
return function(file,enc,cb){
NpmciEnv.dockerFiles.push(
new Dockerfile({
filePath:file.path,
read:true
})
);
cb();
};
}
export class Dockerfile {
filePath:string;
buildTag:string;
repo:string;
version:string;
content:string;
baseImage:string;
constructor(options:{filePath?:string,fileContents?:string|Buffer,read?:boolean}){
this.filePath = options.filePath;
this.repo = NpmciEnv.repo.user + "/" + NpmciEnv.repo.repo;
this.version = dockerFileVersion(plugins.path.parse(options.filePath).base);
if(options.filePath && options.read){
this.content = plugins.smartfile.local.toStringSync(plugins.path.resolve(options.filePath));
};
this.baseImage = dockerBaseImage(this.content);
};
build(){
let tag = dockerTag(this.repo,this.version);
plugins.shelljs.exec("docker build -t " + tag + " -f " + this.filePath + " .");
this.buildTag = tag;
};
push(){
if(this.buildTag){
plugins.shelljs.exec("docker push " + this.buildTag);
} else {
plugins.beautylog.error("Dockerfile hasn't been built yet!");
}
}
}
let dockerFileVersion = function(dockerfileNameArg:string):string{
let versionString:string;
let versionRegex = /Dockerfile_([a-zA-Z0-9\.]*)$/;
let regexResultArray = versionRegex.exec(dockerfileNameArg);
if(regexResultArray.length = 2){
versionString = regexResultArray[1];
} else {
versionString = "latest";
}
return versionString;
}
let dockerBaseImage = function(dockerfileContentArg:string){
let baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n/
let regexResultArray = baseImageRegex.exec(dockerfileContentArg)
return regexResultArray[1];
}
export let dockerTag = function(repoArg:string,versionArg:string):string{
let tagString:string;
let registry = NpmciEnv.dockerRegistry;
if(process.env.CI_BUILD_STAGE == "test"){
registry = "registry.gitlab.com";
}
let repo = repoArg;
let version = versionArg;
if(process.env.CI_BUILD_STAGE == "test" || process.env.CI_BUILD_STAGE == "build"){
version = version + "_test";
}
tagString = registry + "/" + repo + ":" + version;
return tagString;
};