Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto add tag:version (version+1) #23

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions config/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ <h2>Docker Build</h2>
<label for="tagName">Tag name</label>
<input type="text" name="tagName" ng-model="config.tag" required>

<label>
<input type="checkbox" name="autoVersion" ng-model="config.autoVersion">
Auto add image tag version?
</label>

<label>
<input type="checkbox" name="push" ng-model="config.push">
Push after successful build?
Expand Down
136 changes: 92 additions & 44 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,108 @@
'use strict';
"use strict";

const debug = require('debug')('strider-docker-build:build');
const Docker = require('dockerode');
const JSONStream = require('json-stream');
const push = require('./push');
const tar = require('tar-fs');
const debug = require("debug")("strider-docker-build:build");
const Docker = require("dockerode");
const JSONStream = require("json-stream");
const push = require("./push");
const tar = require("tar-fs");
const axios = require("axios");

function getLatestVersion(context, config, callback) {
if (config.autoVersion) {
const tags = config.tag.split("/");
const name = tags
.slice(1, tags.length - 1)
.concat(tags[tags.length - 1].split(":")[0])
.join("/");
const remote = `http://${tags[0]}/v2/${name}/tags/list`;
context.comment(`fetch: ${remote}`);
axios
.get(remote)
.then((res) => {
const vers = res.data.tags
.filter((v) => v !== "latest")
.sort((a, b) => {
const va = a.split(".").map((t) => parseInt(t));
const vb = b.split(".").map((t) => parseInt(t));
for (let i = 0; i < Math.max(va.length, vb.length); i++) {
if (va[i] > vb[i]) {
return 1;
} else if (va[i] < vb[i]) {
return -1;
}
}
return 0;
});
// context.comment(vers);
const v = vers.reverse()[0];
const vs = v ? v.split("-")[0].split(".") : "1.0".split(".");
vs[vs.length - 1] = parseInt(vs[vs.length - 1]) + 1;
const version = vs.join(".");
const tag = tags[0] + "/" + name + ":" + version;
context.comment(`tag: ${tag}`);
callback(null, tag);
})
.catch(callback);
} else {
callback(null, config.tag);
}
}

module.exports = function (config) {
return function (context, done) {
const docker = new Docker();

context.comment(`Connecting to Docker: ${process.env.DOCKER_HOST}`);

const tarStream = tar.pack(context.dataDir);
docker.buildImage(tarStream, {
t: config.tag,
q: false
}, function (err, ostream) {
if (err) {
return done(err);
}

var stream = new JSONStream();
let buildErr;

stream.on('data', function (data) {
if (data.stream) {
context.out(data.stream);
} else if (data.error) {
context.out(data.errorDetail.message);
err = new Error(data.error);
}
});
getLatestVersion(context, config, (err, tag) => {
const tarStream = tar.pack(context.dataDir);
docker.buildImage(
tarStream,
{
t: tag,
q: false,
},
function (err, ostream) {
if (err) {
return done(err);
}
knownasilya marked this conversation as resolved.
Show resolved Hide resolved

stream.on('error', function (err) {
debug(err);
buildErr = err;
});
var stream = new JSONStream();
let buildErr;

stream.on('end', function () {
if (buildErr) {
return done(buildErr);
}
stream.on("data", function (data) {
if (data.stream) {
context.out(data.stream);
} else if (data.error) {
context.out(data.errorDetail.message);
err = new Error(data.error);
}
});

if (config.push) {
push(docker, config, context, done);
} else {
done();
}
});
stream.on("error", function (err) {
debug(err);
buildErr = err;
});

ostream.pipe(stream);
stream.on("end", function () {
if (buildErr) {
return done(buildErr);
}

ostream.on('end', function () {
stream.end();
});
if (config.push) {
push(docker, config, context, done);
} else {
done();
}
});

ostream.pipe(stream);

ostream.on("end", function () {
stream.end();
});
}
);
});
};
};
Loading