-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
100 lines (77 loc) · 3.7 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
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as fs from "fs";
import * as gulp from "gulp";
import * as shell from "gulp-shell";
import * as del from "del";
import * as merge from "merge-stream";
const compileTypescript = `tsc -p tsconfig.prod.json`;
gulp.task("clean", () => del("dist/**", {force: true}));
gulp.task("compile", gulp.series("clean", shell.task([compileTypescript])));
gulp.task("build", gulp.series("compile", moveTask));
///
// Build and tag the actual version, mark as latest, and also tag it with just the major and minor versions.
///
const [tagTask, pushTask] = createShellTasks("./package.json");
gulp.task("docker-build", gulp.series("build", tagTask));
gulp.task("docker-push", gulp.series("docker-build", pushTask));
gulp.task("release", gulp.series("docker-push"));
gulp.task("default", gulp.series("docker-build"));
function versionMajorMinor(version: string) {
const parts = version.split(".");
if (parts.length === 3) {
return [`${parts[0]}`, `${parts[0]}.${parts[1]}`];
}
return [null, null];
}
function moveTask() {
return merge(
[
gulp.src("package.json").pipe(gulp.dest("dist")),
gulp.src("yarn.lock").pipe(gulp.dest("dist")),
gulp.src("LICENSE").pipe(gulp.dest("dist")),
gulp.src("docker-entry.sh").pipe(gulp.dest("dist"))
]
);
}
function createShellTasks(sourceFile: string) {
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}`;
///
// The objective here is to build and tag the actual version, mark it as latest, and also tag it with just the major
// version and major.minor as the "latest" within those scopes. Iterations of deploy-services should generally use
// major.minor in the Compose file rather that just latest. This facilitates side-by-side deployments of current and
// next version systems where pulling "latest" doesn't affect the older system on subsequent up commands.
///
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/release
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([
buildCommand,
tagMajorCommand,
tagMajMinCommand,
tagLatestCommand
]),
shell.task([
pushCommand,
pushMajorCommand,
pushMajMinCommand,
pushLatestCommand
])
];
}