Skip to content

Commit 470a374

Browse files
committed
build: move build metadata preparation to a standalone script
- Extracted `build_info` preparation logic to `script/prepare-build-info`. - Updated Dockerfile and GitHub workflows to use the new script. - Simplified metadata injection process for better reusability.
1 parent 69b809a commit 470a374

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@ jobs:
2929
run: script/test
3030

3131
- name: Prepare build_info files
32-
shell: bash
33-
run: |
34-
BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
35-
mkdir -p cmd/github-mcp-server/build_info
36-
echo "${{ github.sha }}" > cmd/github-mcp-server/build_info/commit.txt
37-
echo "$BUILD_DATE" > cmd/github-mcp-server/build_info/date.txt
38-
if [ "${{ github.event_name }}" = "push" ]; then
39-
echo "${{ github.ref_name }}" > cmd/github-mcp-server/build_info/version.txt
40-
else
41-
echo "pr-${{ github.event.pull_request.number }}" > cmd/github-mcp-server/build_info/version.txt
42-
fi
32+
run: script/prepare-build-info
33+
4334
- name: Build
4435
run: go build -v ./cmd/github-mcp-server

Dockerfile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
FROM golang:1.24.4-alpine AS build
2-
ARG VERSION="dev"
32

43
# Set the working directory
54
WORKDIR /build
@@ -8,18 +7,12 @@ WORKDIR /build
87
RUN --mount=type=cache,target=/var/cache/apk \
98
apk add git
109

11-
# Prepare build_info files
12-
RUN --mount=type=bind,target=. \
13-
mkdir -p cmd/github-mcp-server/build_info && \
14-
git rev-parse HEAD > cmd/github-mcp-server/build_info/commit.txt && \
15-
date -u +%Y-%m-%dT%H:%M:%SZ > cmd/github-mcp-server/build_info/date.txt && \
16-
echo "${VERSION}" > cmd/github-mcp-server/build_info/version.txt
17-
1810
# Build the server
1911
# go build automatically download required module dependencies to /go/pkg/mod
2012
RUN --mount=type=cache,target=/go/pkg/mod \
2113
--mount=type=cache,target=/root/.cache/go-build \
2214
--mount=type=bind,target=. \
15+
script/prepare-build-info && \
2316
CGO_ENABLED=0 go build -ldflags="-s -w" \
2417
-o /bin/github-mcp-server cmd/github-mcp-server/main.go
2518

script/prepare-build-info

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#!/bin/sh
3+
set -eu
4+
5+
mkdir -p cmd/github-mcp-server/build_info
6+
7+
BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
8+
9+
if [ -n "${GITHUB_ACTIONS:-}" ]; then
10+
# GitHub Actions context
11+
COMMIT="${GITHUB_SHA}"
12+
13+
if [ "${GITHUB_EVENT_NAME}" = "push" ] && [ "${GITHUB_REF_TYPE}" = "tag" ]; then
14+
VERSION="${GITHUB_REF_NAME}"
15+
elif [ "${GITHUB_EVENT_NAME}" = "push" ]; then
16+
VERSION="${GITHUB_REF_NAME}-${GITHUB_SHA}"
17+
else
18+
VERSION="pr-${GITHUB_EVENT_PULL_REQUEST_NUMBER}"
19+
fi
20+
else
21+
# Local/Docker context
22+
COMMIT=$(git rev-parse HEAD 2>/dev/null || echo 'unknown')
23+
VERSION="dev"
24+
fi
25+
26+
# Write build info files
27+
echo "${COMMIT}" > cmd/github-mcp-server/build_info/commit.txt
28+
echo "${BUILD_DATE}" > cmd/github-mcp-server/build_info/date.txt
29+
echo "${VERSION}" > cmd/github-mcp-server/build_info/version.txt

0 commit comments

Comments
 (0)