-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
87 lines (67 loc) · 2.98 KB
/
gulpfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import * as fs from "fs";
import * as gulp from "gulp";
import * as shell from "gulp-shell";
///
// Build and tag the actual version, mark as latest, and also tag it with just the major and minor versions.
///
const [buildTask, tagTask, pushTask] = createShellTasks("./package.json");
gulp.task("default", ["docker-build"]);
gulp.task("build", buildTask);
gulp.task("docker-build", ["build"], tagTask);
gulp.task("docker-push", ["docker-build"], pushTask);
gulp.task("release", ["docker-push"]);
function versionMajorMinor(version: string) {
const parts = version.split(".");
if (parts.length === 3) {
return [`${parts[0]}`, `${parts[0]}.${parts[1]}`];
}
return [null, null];
}
function createShellTasks(sourceFile: string) {
// Clean and build
const cleanCommand = `rm -rf dist`;
const compileTypescript = `tsc -p tsconfig.prod.json`;
const moveFiles = `cp ./{package.json,yarn.lock,LICENSE,docker-entry.sh} dist`;
const webpack = `webpack`;
const contents = fs.readFileSync(sourceFile).toString();
const npmPackage = JSON.parse(contents);
const version = npmPackage.version;
const [versionMajor, versionMajMin] = versionMajorMinor(version);
const repo = npmPackage.dockerRepository;
const imageName = npmPackage.dockerImageName || npmPackage.name;
const dockerRepoImage = `${repo}/${imageName}`;
const imageWithVersion = `${dockerRepoImage}:${version}`;
const imageWithVersionMajor = versionMajor ? `${dockerRepoImage}:${versionMajor}` : null;
const imageWithVersionMajMin = versionMajMin ? `${dockerRepoImage}:${versionMajMin}` : null;
const imageAsLatest = `${dockerRepoImage}:latest`;
// Docker build/tag
const buildCommand = `docker build --tag ${imageWithVersion} .`;
const tagMajorCommand = imageWithVersionMajor ? `docker tag ${imageWithVersion} ${imageWithVersionMajor}` : `echo "could not tag with major version"`;
const tagMajMinCommand = imageWithVersionMajMin ? `docker tag ${imageWithVersion} ${imageWithVersionMajMin}` : `echo "could not tag with major.minor version"`;
const tagLatestCommand = `docker tag ${imageWithVersion} ${imageAsLatest}`;
// Docker push
const pushCommand = `docker push ${imageWithVersion}`;
const pushMajorCommand = imageWithVersionMajor ? `docker push ${imageWithVersionMajor}` : `echo "could not push major version"`;
const pushMajMinCommand = imageWithVersionMajMin ? `docker push ${imageWithVersionMajMin}` : `echo "could not push major.minor version"`;
const pushLatestCommand = `docker push ${imageAsLatest}`;
return [
shell.task([
cleanCommand,
compileTypescript,
moveFiles,
webpack
]),
shell.task([
buildCommand,
tagMajorCommand,
tagMajMinCommand,
tagLatestCommand
]),
shell.task([
pushCommand,
pushMajorCommand,
pushMajMinCommand,
pushLatestCommand
])
];
}