144 lines
4.9 KiB
YAML
144 lines
4.9 KiB
YAML
name: cd
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
workflow_dispatch:
|
|
|
|
env:
|
|
REGISTRY: registry.juwan.xhttp.zip
|
|
REPO: juwan
|
|
|
|
jobs:
|
|
discover:
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
targets: ${{ steps.list.outputs.targets }}
|
|
short_sha: ${{ steps.list.outputs.short_sha }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- id: list
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
echo "short_sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
|
|
|
|
python3 - <<'PY' >> "$GITHUB_OUTPUT"
|
|
import json, os
|
|
NAME_OVERRIDE = {
|
|
"users": ("users", "user"),
|
|
"user_verifications": ("user_verifications", "user-verifications"),
|
|
}
|
|
STATEFULSETS = {"snowflake-rpc": "snowflake"}
|
|
targets = []
|
|
for svc in sorted(os.listdir("app")):
|
|
svc_dir = f"app/{svc}"
|
|
if not os.path.isdir(svc_dir):
|
|
continue
|
|
for sub in sorted(os.listdir(svc_dir)):
|
|
d = f"{svc_dir}/{sub}"
|
|
if not os.path.isdir(d) or sub not in ("api","rpc","mq","adapter"):
|
|
continue
|
|
img_pre, wl_pre = NAME_OVERRIDE.get(svc, (svc, svc))
|
|
image = f"{img_pre}-{sub}"
|
|
workload = STATEFULSETS.get(image, f"{wl_pre}-{sub}")
|
|
targets.append({"image": image, "dir": d, "workload": workload})
|
|
print("targets=" + json.dumps(targets))
|
|
PY
|
|
|
|
build:
|
|
needs: discover
|
|
runs-on: ubuntu-latest
|
|
strategy:
|
|
fail-fast: false
|
|
max-parallel: 1
|
|
matrix:
|
|
target: ${{ fromJson(needs.discover.outputs.targets) }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Setup Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Login registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ secrets.REGISTRY_USERNAME }}
|
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
|
|
|
- name: Generate Dockerfile
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
dir='${{ matrix.target.dir }}'
|
|
entry=$(grep -l "package main" "$dir"/*.go | head -n1)
|
|
cfg=$(basename "$(find "$dir/etc" -maxdepth 1 -name '*.yaml' | head -n1)" 2>/dev/null || echo config.yaml)
|
|
cat > Dockerfile.build <<EOF
|
|
FROM golang:1.25-alpine AS builder
|
|
WORKDIR /build
|
|
ENV CGO_ENABLED=0 GOOS=linux
|
|
COPY go.mod go.sum ./
|
|
RUN --mount=type=cache,target=/go/pkg/mod go mod download
|
|
COPY . .
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
go build -ldflags="-s -w" -o /app/main $entry
|
|
|
|
FROM alpine:3.21
|
|
RUN apk add --no-cache ca-certificates tzdata
|
|
ENV TZ=Asia/Shanghai
|
|
WORKDIR /app
|
|
COPY --from=builder /app/main /app/main
|
|
COPY $dir/etc /app/etc
|
|
CMD ["./main", "-f", "etc/$cfg"]
|
|
EOF
|
|
|
|
- name: Build and push
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
file: Dockerfile.build
|
|
push: true
|
|
tags: |
|
|
${{ env.REGISTRY }}/${{ env.REPO }}/${{ matrix.target.image }}:${{ needs.discover.outputs.short_sha }}
|
|
${{ env.REGISTRY }}/${{ env.REPO }}/${{ matrix.target.image }}:latest
|
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.REPO }}/buildcache:${{ matrix.target.image }}
|
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.REPO }}/buildcache:${{ matrix.target.image }},mode=max
|
|
|
|
rollout:
|
|
needs: [discover, build]
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Install kubectl
|
|
run: |
|
|
curl -sLo /usr/local/bin/kubectl \
|
|
"https://dl.k8s.io/release/$(curl -sL https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
|
|
chmod +x /usr/local/bin/kubectl
|
|
|
|
- name: Rollout k01
|
|
env:
|
|
KUBECONFIG_B64: ${{ secrets.K01_KUBECONFIG }}
|
|
SHA_TAG: ${{ needs.discover.outputs.short_sha }}
|
|
TARGETS: ${{ needs.discover.outputs.targets }}
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
mkdir -p ~/.kube
|
|
echo "$KUBECONFIG_B64" | base64 -d > ~/.kube/config
|
|
chmod 600 ~/.kube/config
|
|
|
|
python3 <<'PY'
|
|
import json, subprocess, os
|
|
reg = os.environ["REGISTRY"] + "/" + os.environ["REPO"]
|
|
for t in json.loads(os.environ["TARGETS"]):
|
|
img = t["image"]
|
|
wl = t["workload"]
|
|
kind = "statefulset" if wl == "snowflake" else "deployment"
|
|
ref = f"{reg}/{img}:{os.environ['SHA_TAG']}"
|
|
cmd = ["kubectl","-n","juwan","set","image",f"{kind}/{wl}",f"{img}={ref}"]
|
|
print(" ".join(cmd))
|
|
subprocess.run(cmd, check=False)
|
|
PY
|