Skip to content

Commit 1372e49

Browse files
committed
build script improvements
1 parent 2aad3ab commit 1372e49

File tree

7 files changed

+230
-36
lines changed

7 files changed

+230
-36
lines changed

.github/workflows/docker-build.yml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
branches:
55
# todo - change with main
66
- feat/newjitsu/self-hosted-revamped
7-
tags:
8-
- jitsu2-*
97

108
jobs:
119
build:
@@ -33,7 +31,6 @@ jobs:
3331
run: |
3432
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
3533
36-
3734
- uses: actions/cache@v3
3835
name: Setup pnpm cache
3936
with:
@@ -45,15 +42,5 @@ jobs:
4542
- name: Install dependencies with pnpm
4643
run: pnpm install --no-frozen-lockfile
4744

48-
- name: Extract version from tag
49-
if: startsWith(github.ref, 'refs/tags/jitsu2-')
50-
id: version
51-
run: echo "::set-output name=version::${GITHUB_REF#refs/tags/jitsu2-}"
52-
53-
- name: Build Beta Images
54-
if: github.ref == 'refs/heads/feat/newjitsu/self-hosted-revamped'
55-
run: pnpm build-scripts docker --tag beta --push-git-tag --push-docker
56-
57-
- name: Build Release Images
58-
if: startsWith(github.ref, 'refs/tags/jitsu2-')
59-
run: pnpm build-scripts docker --tag latest --version ${{ steps.version.outputs.version }} --push-git-tag --push-docker --dry-run
45+
- name: Build Beta Images for AMD64
46+
run: pnpm build-scripts docker --platform linux/amd64 --tag beta --push-git-tag --push-docker --logs

cli/build-scripts/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
"exec": "ts-node src/index.ts"
1414
},
1515
"dependencies": {
16-
"figlet": "^1.6.0",
16+
"boxen": "^7.1.1",
17+
"colorette": "^2.0.20",
1718
"isolated-vm": "^4.6.0",
1819
"jest-cli": "^29.7.0",
1920
"semver": "^7.5.4",
2021
"simple-git": "^3.22.0",
22+
"string-width": "^7.0.0",
2123
"tslib": "^2.6.2"
2224
},
2325
"devDependencies": {

cli/build-scripts/src/box.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { color } from "./colors";
2+
const chars = {
3+
topLeft: "┌",
4+
topRight: "┐",
5+
bottomRight: "┘",
6+
bottomLeft: "└",
7+
vertical: "│",
8+
horizontal: "─",
9+
};
10+
11+
function stringWidth(str: string): number {
12+
let width = 0;
13+
14+
for (const char of Array.from(str)) {
15+
const codePoint = char.codePointAt(0);
16+
17+
// Ignore control characters and other non-printable characters
18+
if (codePoint && (codePoint <= 31 || codePoint === 127)) {
19+
continue;
20+
}
21+
22+
// Basic check for surrogate pairs (common in emojis)
23+
if (codePoint && codePoint > 0xffff) {
24+
width += 2; // Assuming emoji width as 2
25+
} else {
26+
width += 1;
27+
}
28+
}
29+
30+
return width;
31+
}
32+
export function drawBox({ content }: { content: string | string[] }): string {
33+
const contentLines = typeof content === "string" ? content.split("\n") : content;
34+
const maxWidth = Math.max(...contentLines.map(stringWidth));
35+
36+
const resLines: string[] = [];
37+
const px = 2;
38+
const mx = 0;
39+
resLines.push(
40+
" ".repeat(mx) + color.gray(chars.topLeft + chars.horizontal.repeat(maxWidth + px * 2) + chars.topRight)
41+
);
42+
43+
resLines.push(
44+
...contentLines.map(
45+
line =>
46+
" ".repeat(mx) +
47+
color.gray(chars.vertical) +
48+
" ".repeat(px) +
49+
line.padEnd(maxWidth, " ") +
50+
" ".repeat(px) +
51+
color.gray(chars.vertical)
52+
)
53+
);
54+
55+
resLines.push(
56+
" ".repeat(mx) + color.gray(chars.bottomLeft + chars.horizontal.repeat(maxWidth + px * 2) + chars.bottomRight)
57+
);
58+
59+
return resLines.join("\n");
60+
}

cli/build-scripts/src/colors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createColors } from "colorette";
2+
3+
export const color = createColors();

cli/build-scripts/src/docker.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import simpleGit from "simple-git";
44
import * as fs from "fs";
55
import { compare as semverCompare, parse as semverParse, SemVer } from "semver";
66
import * as child_process from "child_process";
7+
import { color } from "./colors";
8+
import { drawBox } from "./box";
79

810
type ReleaseStream = "beta" | "latest";
911

@@ -88,43 +90,84 @@ export async function docker(args: minimist.ParsedArgs): Promise<void> {
8890
let version = args.version;
8991
const tag: ReleaseStream = args.tag || "beta";
9092
const tagPrefix = args.taxPrefix || "jitsu2";
91-
const dockerPlatforms = args.dockerPlatforms;
9293
const dryRun = !!(args["dry-run"] || false);
94+
console.log(
95+
drawBox({
96+
content: [`Hi, I'm Jitsu Docker Builder!`, ``, `I'm going to build docker images for \`${tag}\` release stream`],
97+
})
98+
);
9399
if (!version) {
94100
version = await getAutomaticVersion(tag, tagPrefix);
95-
console.info("--version is not specified, using automatic version: " + version);
101+
console.info(
102+
"💁🏻‍ Version (--version param) is not specified, using automatic version: " + color.bold(color.cyan(version))
103+
);
96104
}
97105
const gitTag = `${tagPrefix}-${version}`;
98106
if ((await git.tags()).all.includes(gitTag)) {
99107
throw new Error(`Tag ${gitTag} for next version ${version} already exists. Aborting`);
100108
}
109+
const logsDir = args["logs"] ? path.resolve(__dirname, "../../../", args["logs"]) : undefined;
110+
if (logsDir) {
111+
fs.mkdirSync(logsDir, { recursive: true });
112+
}
101113

102114
for (const dockerTarget of ["console", "rotor"]) {
103-
console.log(`Building jitsucom/${dockerTarget}:${version}...`);
115+
console.log(
116+
`🚀 Building ${color.cyan(dockerTarget)} docker with tags: ${color.cyan(
117+
`jitsucom/${dockerTarget}:${version}`
118+
)} and ${color.cyan(`jitsucom/${dockerTarget}:${tag}`)}...`
119+
);
104120
const dockerImageName = `jitsucom/${dockerTarget}`;
105121
const dockerArgs = [
106122
"buildx build",
107123
`--target ${dockerTarget}`,
108-
"--progress plain",
109-
args["docker-platforms"] && `--platform ${dockerPlatforms}`,
124+
"--progress=plain",
125+
args["platform"] && `--platform ${args["platform"]}`,
110126
`-t ${dockerImageName}:${tag}`,
111127
`-t ${dockerImageName}:${version}`,
112128
`-f all.Dockerfile`,
113129
args["push-docker"] ? "--push" : "--load",
114130
".",
115131
].filter(Boolean);
132+
const qt = `${color.gray(color.bold("`"))}`;
133+
console.log(
134+
`🎻 Docker command\n\n\t${qt}${color.cyan(
135+
`docker ${dockerArgs.filter(args => !args.startsWith("--progress")).join(" ")}`
136+
)}${qt}\n\n\t`
137+
);
116138
if (dryRun) {
117-
console.log("Dry run: docker " + dockerArgs.join(" "));
139+
console.log(`🏃🏻 Skipping actual build because of ${color.cyan("--dry-run")} flag`);
118140
} else {
141+
const logPath = logsDir
142+
? path.join(logsDir, `${dockerTarget}-docker-build-${new Date().toISOString()}.log`)
143+
: undefined;
144+
const stream = logsDir ? fs.createWriteStream(logPath!) : undefined;
145+
if (stream) {
146+
console.log(`📝 Writing logs to ${color.cyan(logPath!)}`);
147+
stream.write(`Building ${dockerImageName}:${tag}\n`);
148+
stream.write(`Command:\n\tdocker ${dockerArgs.join(" ")}\n`);
149+
stream.write("=".repeat(80));
150+
stream.write("\n\n");
151+
}
119152
const exitCode = await runCommand("docker", {
120153
args: [...dockerArgs],
121154
outputHandler: (data, opts) => {
122155
const dataStr = data.toString();
123156
dataStr.split("\n").forEach(line => {
124-
process.stdout.write(`${dockerTarget}: ${line}\n`);
157+
if (!logsDir) {
158+
process.stdout.write(`${color.green(dockerTarget)}: ${line}\n`);
159+
}
160+
if (stream) {
161+
stream.write(`${line}\n`);
162+
}
125163
});
126164
},
127165
});
166+
if (stream) {
167+
stream.write("=".repeat(80));
168+
stream.write("\n\n");
169+
stream.end();
170+
}
128171
if (exitCode != 0) {
129172
throw new Error(`Docker build failed with exit code ${exitCode}`);
130173
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"private": false,
55
"scripts": {
6-
"build-scripts": "cd cli/build-scripts && pnpm run exec",
6+
"build-scripts": "pnpm --filter ./cli/build-scripts run exec",
77
"format:check": "prettier --ignore-unknown --check --config ./.prettierrc.json --ignore-path ./.prettierignore $(git diff --name-only --diff-filter d | xargs)",
88
"format:check:all": "prettier --check --config ./.prettierrc.json --ignore-path ./.prettierignore .",
99
"format": "prettier --ignore-unknown --write --config ./.prettierrc.json --ignore-path ./.prettierignore $(git diff --name-only --diff-filter d | xargs)",

0 commit comments

Comments
 (0)