Merge pull request #445 from karlseguin/capture_git_commit
Some checks are pending
e2e-test / zig build release (push) Waiting to run
e2e-test / puppeteer (push) Blocked by required conditions
wpt / web platform tests (push) Waiting to run
wpt / perf-fmt (push) Blocked by required conditions
zig-test / zig build dev (push) Waiting to run
zig-test / zig test (push) Waiting to run
zig-test / perf-fmt (push) Blocked by required conditions

Make the the short git SHA available within the program
This commit is contained in:
Pierre Tachoire
2025-02-26 14:10:24 +01:00
committed by GitHub
4 changed files with 27 additions and 8 deletions

View File

@@ -28,7 +28,7 @@ jobs:
- uses: ./.github/actions/install
- name: zig build
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8 -Dcpu=x86_64
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8 -Dcpu=x86_64 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
- name: Rename binary
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}
@@ -60,7 +60,7 @@ jobs:
arch: ${{env.ARCH}}
- name: zig build
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
- name: Rename binary
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}

View File

@@ -59,13 +59,13 @@ download-zig:
## Build in release-safe mode
build:
@printf "\e[36mBuilding (release safe)...\e[0m\n"
@$(ZIG) build -Doptimize=ReleaseSafe -Dengine=v8 || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
$(ZIG) build -Doptimize=ReleaseSafe -Dengine=v8 -Dgit_commit=$$(git rev-parse --short HEAD) || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
@printf "\e[33mBuild OK\e[0m\n"
## Build in debug mode
build-dev:
@printf "\e[36mBuilding (debug)...\e[0m\n"
@$(ZIG) build -Dengine=v8 || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
@$(ZIG) build -Dengine=v8 -Dgit_commit=$$(git rev-parse --short HEAD) || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
@printf "\e[33mBuild OK\e[0m\n"
## Run the server in debug mode

View File

@@ -58,6 +58,15 @@ pub fn build(b: *std.Build) !void {
.optimize = mode,
});
try common(b, exe, options);
{
var opt = b.addOptions();
opt.addOption(
[]const u8,
"git_commit",
b.option([]const u8, "git_commit", "Current git commit") orelse "dev",
);
exe.root_module.addImport("build_info", opt.createModule());
}
b.installArtifact(exe);
// run

View File

@@ -59,6 +59,10 @@ pub fn main() !void {
switch (args.mode) {
.help => args.printUsageAndExit(args.mode.help),
.version => {
std.debug.print("{s}\n", .{@import("build_info").git_commit});
return std.process.cleanExit();
},
.serve => |opts| {
const address = std.net.Address.parseIp4(opts.host, opts.port) catch |err| {
log.err("address (host:port) {any}\n", .{err});
@@ -125,12 +129,14 @@ const Command = struct {
help,
fetch,
serve,
version,
};
const Mode = union(ModeType) {
help: bool, // false when being printed because of an error
fetch: Fetch,
serve: Serve,
version: void,
};
const Serve = struct {
@@ -172,10 +178,13 @@ const Command = struct {
\\--timeout Inactivity timeout in seconds before disconnecting clients
\\ Defaults to 3 (seconds)
\\
\\version command
\\Displays the version of {s}
\\
\\help command
\\Displays this message
;
std.debug.print(usage, .{ self.exec_name, self.exec_name, self.exec_name });
std.debug.print(usage, .{ self.exec_name, self.exec_name, self.exec_name, self.exec_name });
if (success) {
return std.process.cleanExit();
}
@@ -210,9 +219,10 @@ fn parseArgs(allocator: Allocator) !Command {
};
cmd.mode = switch (mode) {
.help => Command.Mode{ .help = true },
.serve => Command.Mode{ .serve = parseServeArgs(allocator, &args) catch return cmd },
.fetch => Command.Mode{ .fetch = parseFetchArgs(allocator, &args) catch return cmd },
.help => .{ .help = true },
.serve => .{ .serve = parseServeArgs(allocator, &args) catch return cmd },
.fetch => .{ .fetch = parseFetchArgs(allocator, &args) catch return cmd },
.version => .{ .version = {} },
};
return cmd;
}