Skip to content

Commit 599c97b

Browse files
committed
fix(ci): build server binary for alinux compatibility
1 parent addbcc3 commit 599c97b

3 files changed

Lines changed: 63 additions & 13 deletions

File tree

.github/workflows/deploy-server.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ jobs:
3030
with:
3131
go-version-file: clipSync-server/go.mod
3232

33+
- name: Install musl toolchain
34+
run: |
35+
sudo apt-get update
36+
sudo apt-get install -y musl-tools
37+
3338
- name: Run server tests
3439
working-directory: clipSync-server
3540
run: go test ./... -v -count=1
@@ -40,7 +45,12 @@ jobs:
4045
GOOS: linux
4146
GOARCH: amd64
4247
CGO_ENABLED: "1"
43-
run: go build -o bin/clipsync-server-linux ./cmd/server
48+
CC: x86_64-linux-musl-gcc
49+
run: |
50+
go build \
51+
-ldflags='-linkmode external -extldflags "-static"' \
52+
-o bin/clipsync-server-linux \
53+
./cmd/server
4454
4555
- name: Assemble release bundle
4656
env:
@@ -87,6 +97,8 @@ jobs:
8797
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }}
8898
DEPLOY_SERVICE_NAME: ${{ secrets.DEPLOY_SERVICE_NAME }}
8999
DEPLOY_JWT_SECRET: ${{ secrets.DEPLOY_JWT_SECRET }}
100+
DEPLOY_BINARY_RELATIVE_PATH: ${{ secrets.DEPLOY_BINARY_RELATIVE_PATH }}
101+
DEPLOY_CONFIG_RELATIVE_PATH: ${{ secrets.DEPLOY_CONFIG_RELATIVE_PATH }}
90102
run: |
91103
ssh_opts=(
92104
-o BatchMode=yes
@@ -101,6 +113,8 @@ jobs:
101113
printf 'export DEPLOY_PATH=%q\n' "$DEPLOY_PATH"
102114
printf 'export DEPLOY_SERVICE_NAME=%q\n' "$DEPLOY_SERVICE_NAME"
103115
printf 'export DEPLOY_JWT_SECRET=%q\n' "$DEPLOY_JWT_SECRET"
116+
printf 'export DEPLOY_BINARY_RELATIVE_PATH=%q\n' "$DEPLOY_BINARY_RELATIVE_PATH"
117+
printf 'export DEPLOY_CONFIG_RELATIVE_PATH=%q\n' "$DEPLOY_CONFIG_RELATIVE_PATH"
104118
cat scripts/deploy/server-release.sh
105119
} | ssh "${ssh_opts[@]}" "${DEPLOY_USER}@${DEPLOY_HOST}" "bash -s"
106120

docs/deployment/github-actions-server.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Pushing to `main` triggers [.github/workflows/deploy-server.yml](/C:/Users/20562
77
- Checks out the repo on `ubuntu-latest`
88
- Sets up Go from `clipSync-server/go.mod`
99
- Runs `go test ./... -v -count=1` in `clipSync-server`
10-
- Builds a Linux `amd64` server binary with `CGO_ENABLED=1`
10+
- Builds a Linux `amd64` server binary with `CGO_ENABLED=1` against musl so the release is compatible with older glibc-based servers such as Alibaba Cloud Linux 3
1111
- Packages the binary and `clipSync-server/configs/config.yaml` into `clipsync-server-release-<git-sha>.tar.gz`
1212
- Uploads the release archive to the deployment host over SSH/SCP
1313
- Pipes [`scripts/deploy/server-release.sh`](/C:/Users/20562/Desktop/桌面/clipSync/scripts/deploy/server-release.sh) to the remote host and executes it with deployment environment variables
@@ -28,13 +28,16 @@ Configure these repository secrets before enabling the workflow:
2828
| `DEPLOY_SERVICE_NAME` | `clipsync.service` | systemd service restarted during deploy and rollback |
2929
| `DEPLOY_KNOWN_HOSTS` | output of `ssh-keyscan -H 8.141.100.238` | Host key entry used for strict SSH host verification |
3030
| `DEPLOY_JWT_SECRET` | a long random secret | Injected into the deployed server config during deployment; do not store the live JWT secret in git |
31+
| `DEPLOY_BINARY_RELATIVE_PATH` | `clipsync-server` | Optional relative install path under `DEPLOY_PATH` for the live binary when the systemd service does not use `bin/clipsync-server-linux` |
32+
| `DEPLOY_CONFIG_RELATIVE_PATH` | `configs/config.yaml` | Optional relative install path under `DEPLOY_PATH` for the live config |
3133

3234
Notes:
3335

3436
- `DEPLOY_SSH_KEY` must match the public key installed for `DEPLOY_USER` on the server.
3537
- `DEPLOY_KNOWN_HOSTS` is required because the workflow uses `StrictHostKeyChecking=yes`.
3638
- Keep `DEPLOY_PATH` and `DEPLOY_SERVICE_NAME` aligned with the actual server layout and systemd unit.
3739
- `DEPLOY_JWT_SECRET` should be treated as the real production JWT signing key. The repository config now keeps only the placeholder value.
40+
- Leave `DEPLOY_BINARY_RELATIVE_PATH` and `DEPLOY_CONFIG_RELATIVE_PATH` unset when the server uses the default layout under `DEPLOY_PATH/bin` and `DEPLOY_PATH/configs`.
3841
- Optional: `DEPLOY_PUBLIC_HEALTH_URL` can override the final GitHub Actions health-check URL when the public endpoint differs from `http://<DEPLOY_HOST>:8081/api/v1/health`.
3942

4043
## Server Requirements
@@ -44,10 +47,10 @@ The workflow assumes the target server already has:
4447
- A Linux environment reachable from GitHub-hosted runners over SSH
4548
- A systemd service matching `DEPLOY_SERVICE_NAME`
4649
- `bash`, `tar`, `curl`, and `systemctl` available on the target host
47-
- A writable deployment directory such as `/opt/clipSync-server-src`
50+
- A writable deployment directory such as `/opt/clipSync-server-src` or `/opt/clipsync`
4851
- Permission for `DEPLOY_USER` to write under `DEPLOY_PATH`
4952
- Permission for `DEPLOY_USER` to restart `DEPLOY_SERVICE_NAME`
50-
- The service configured to run the deployed binary from `DEPLOY_PATH/bin/clipsync-server-linux`
53+
- The service configured to run the deployed binary from either `DEPLOY_PATH/bin/clipsync-server-linux` or `DEPLOY_PATH/<DEPLOY_BINARY_RELATIVE_PATH>`
5154
- The service configured so the server can find `configs/config.yaml` after startup
5255
- The server health endpoint available at `http://127.0.0.1:8081/api/v1/health`
5356

@@ -86,6 +89,12 @@ Example expectations:
8689
- Data directory: `/opt/clipSync-server-src/data`
8790
- Either `WorkingDirectory=/opt/clipSync-server-src` or `Environment=CLIPSYNC_CONFIG=/opt/clipSync-server-src/configs/config.yaml`
8891

92+
If your existing service uses a flat layout like `/opt/clipsync/clipsync-server`, set:
93+
94+
- `DEPLOY_PATH=/opt/clipsync`
95+
- `DEPLOY_BINARY_RELATIVE_PATH=clipsync-server`
96+
- `DEPLOY_CONFIG_RELATIVE_PATH=configs/config.yaml`
97+
8998
7. Verify the service can start and answer its local health endpoint before relying on automation.
9099

91100
```bash
@@ -102,9 +111,9 @@ The remote deployment script is intentionally narrow and opinionated:
102111

103112
- `data/` is preserved. The script creates `DEPLOY_PATH/data` if needed and never deletes or replaces it.
104113
- `configs/config.yaml` is overwritten from the repository on every deploy, then the script replaces the placeholder JWT secret with `DEPLOY_JWT_SECRET` on the server.
105-
- The live binary path is `DEPLOY_PATH/bin/clipsync-server-linux`.
106-
- The binary backup path is `DEPLOY_PATH/bin/clipsync-server-linux.prev`.
107-
- The config backup path is `DEPLOY_PATH/configs/config.yaml.prev`.
114+
- The live binary path defaults to `DEPLOY_PATH/bin/clipsync-server-linux`, but can be overridden with `DEPLOY_BINARY_RELATIVE_PATH`.
115+
- The binary backup path is the live binary path with `.prev` appended.
116+
- The config backup path is the live config path with `.prev` appended.
108117
- The uploaded release archive is stored remotely at `/tmp/clipsync-server-release-<git-sha>.tar.gz`.
109118
- The script extracts into a temporary staging directory under `/tmp/clipsync-release.XXXXXX`.
110119
- Archive contents are validated before extraction to reject absolute paths, path traversal, and unsupported entry types.
@@ -172,6 +181,7 @@ What to verify:
172181
- The deployed config in `/opt/clipSync-server-src/configs/config.yaml` contains production-safe values.
173182
- `DEPLOY_JWT_SECRET` exists in GitHub Secrets and the deployed config no longer contains the placeholder value.
174183
- The service is actually starting the binary at `/opt/clipSync-server-src/bin/clipsync-server-linux`.
184+
- If `DEPLOY_BINARY_RELATIVE_PATH` is set, verify the service path matches that effective install path instead of the default `bin/clipsync-server-linux`.
175185
- Port `8081` is listening and reachable from outside the host if the final GitHub Actions health check is using the default URL.
176186
- If `DEPLOY_PUBLIC_HEALTH_URL` is configured, verify that public endpoint and any proxy/load-balancer routing in front of it.
177187
- If rollback ran, check whether `.prev` files were restored and whether the service recovered to the previous version.

scripts/deploy/server-release.sh

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,39 @@ require_env DEPLOY_PATH
140140
require_env DEPLOY_SERVICE_NAME
141141
require_env DEPLOY_JWT_SECRET
142142

143+
DEPLOY_BINARY_RELATIVE_PATH="${DEPLOY_BINARY_RELATIVE_PATH:-bin/clipsync-server-linux}"
144+
DEPLOY_CONFIG_RELATIVE_PATH="${DEPLOY_CONFIG_RELATIVE_PATH:-configs/config.yaml}"
145+
143146
DEPLOY_HEALTH_URL="${DEPLOY_HEALTH_URL:-http://127.0.0.1:8081/api/v1/health}"
144147

148+
validate_relative_path() {
149+
local path_value="$1"
150+
local label="$2"
151+
152+
if [[ -z "$path_value" ]]; then
153+
echo "Missing relative path for $label" >&2
154+
exit 1
155+
fi
156+
157+
if [[ "$path_value" = /* ]]; then
158+
echo "$label must be relative, got absolute path: $path_value" >&2
159+
exit 1
160+
fi
161+
162+
if [[ "$path_value" == ".." || "$path_value" == ../* || "$path_value" == */../* || "$path_value" == */.. ]]; then
163+
echo "$label contains path traversal: $path_value" >&2
164+
exit 1
165+
fi
166+
}
167+
145168
if [[ ! -f "$DEPLOY_ARCHIVE" ]]; then
146169
echo "Deploy archive not found: $DEPLOY_ARCHIVE" >&2
147170
exit 1
148171
fi
149172

173+
validate_relative_path "$DEPLOY_BINARY_RELATIVE_PATH" "DEPLOY_BINARY_RELATIVE_PATH"
174+
validate_relative_path "$DEPLOY_CONFIG_RELATIVE_PATH" "DEPLOY_CONFIG_RELATIVE_PATH"
175+
150176
validate_archive_paths
151177

152178
STAGING_DIR="$(mktemp -d /tmp/clipsync-release.XXXXXX)"
@@ -157,11 +183,11 @@ tar -xzf "$DEPLOY_ARCHIVE" -C "$STAGING_DIR"
157183
RELEASE_ROOT="$(resolve_release_root)"
158184
NEW_BINARY="$RELEASE_ROOT/bin/clipsync-server-linux"
159185
NEW_CONFIG="$RELEASE_ROOT/configs/config.yaml"
160-
LIVE_BINARY="$DEPLOY_PATH/bin/clipsync-server-linux"
161-
BACKUP_BINARY="$DEPLOY_PATH/bin/clipsync-server-linux.prev"
162-
TEMP_BINARY="$DEPLOY_PATH/bin/clipsync-server-linux.new"
163-
LIVE_CONFIG="$DEPLOY_PATH/configs/config.yaml"
164-
BACKUP_CONFIG="$DEPLOY_PATH/configs/config.yaml.prev"
186+
LIVE_BINARY="$DEPLOY_PATH/$DEPLOY_BINARY_RELATIVE_PATH"
187+
BACKUP_BINARY="$LIVE_BINARY.prev"
188+
TEMP_BINARY="$LIVE_BINARY.new"
189+
LIVE_CONFIG="$DEPLOY_PATH/$DEPLOY_CONFIG_RELATIVE_PATH"
190+
BACKUP_CONFIG="$LIVE_CONFIG.prev"
165191

166192
if [[ ! -f "$NEW_BINARY" || -L "$NEW_BINARY" ]]; then
167193
echo "Release binary missing from archive: $NEW_BINARY" >&2
@@ -173,7 +199,7 @@ if [[ ! -f "$NEW_CONFIG" || -L "$NEW_CONFIG" ]]; then
173199
exit 1
174200
fi
175201

176-
install -d "$DEPLOY_PATH/bin" "$DEPLOY_PATH/configs" "$DEPLOY_PATH/data"
202+
install -d "$(dirname "$LIVE_BINARY")" "$(dirname "$LIVE_CONFIG")" "$DEPLOY_PATH/data"
177203

178204
if [[ -f "$LIVE_BINARY" ]]; then
179205
HAD_LIVE_BINARY=1

0 commit comments

Comments
 (0)