From c191974df7898747084529cb20be3dcd8a162d9a Mon Sep 17 00:00:00 2001 From: shenchangbin Date: Thu, 12 Nov 2026 09:34:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E7=BD=B2=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 4 ++ Jenkinsfile.crontab | 123 +++++++++++++++++++++++++++++++++++++++ Jenkinsfile.rpc | 123 +++++++++++++++++++++++++++++++++++++++ build/crontab/Dockerfile | 8 +-- build/srv/Dockerfile | 10 ++-- cmd/healthcheck/main.go | 38 ++++++++++++ 6 files changed, 296 insertions(+), 10 deletions(-) create mode 100644 Jenkinsfile.crontab create mode 100644 Jenkinsfile.rpc create mode 100644 cmd/healthcheck/main.go diff --git a/.dockerignore b/.dockerignore index d1eac46..b286888 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,3 +7,7 @@ logs *.local.yaml *.local.yml *.local.json +configs +deploy +release.txt +published-image.txt diff --git a/Jenkinsfile.crontab b/Jenkinsfile.crontab new file mode 100644 index 0000000..7105995 --- /dev/null +++ b/Jenkinsfile.crontab @@ -0,0 +1,123 @@ +pipeline { + agent { label 'layout-docker' } + options { + skipDefaultCheckout() + disableConcurrentBuilds() + timeout(time: 30, unit: 'MINUTES') + buildDiscarder(logRotator(numToKeepStr: '30')) + } + parameters { + choice(name: 'DEPLOY_ENV', choices: ['test', 'prod'], + description: 'test -> dev; prod -> release (build/push only, deployment disabled)') + } + environment { + SERVICE = 'micro-crontab' + IMAGE_REPOSITORY = 'harbor.vitatech.com.cn:19443/backend/micro_layout-crontab' + DOCKERFILE = 'build/crontab/Dockerfile' + DEPLOY_ROOT = '/home/SHARE/devstack-scb/apps/layout-test' + } + stages { + stage('Checkout') { + steps { + script { + if (!(params.DEPLOY_ENV in ['test', 'prod'])) { + error('Invalid deployment environment') + } + env.SOURCE_BRANCH = params.DEPLOY_ENV == 'test' ? 'dev' : 'release' + def scmResult = checkout([ + $class: 'GitSCM', + branches: [[name: '*/' + env.SOURCE_BRANCH]], + userRemoteConfigs: [[ + url: 'ssh://git@git.vitatech.com.cn:12222/leo/micro_layout.git', + credentialsId: 'gitea-layout-read' + ]], + extensions: [[$class: 'CleanBeforeCheckout']] + ]) + env.SOURCE_COMMIT = scmResult.GIT_COMMIT + env.IMAGE_TAG = env.SOURCE_BRANCH + '-' + + env.SOURCE_COMMIT.take(12) + '-' + env.BUILD_NUMBER + } + } + } + stage('Build and push') { + steps { + sshagent(credentials: ['gitea-layout-read']) { + withCredentials([ + file(credentialsId: 'gitea-known-hosts', variable: 'GIT_KNOWN_HOSTS'), + usernamePassword(credentialsId: 'harbor-backend', + usernameVariable: 'HARBOR_USER', + passwordVariable: 'HARBOR_PASSWORD') + ]) { + sh ''' + set -eu + set +x + DOCKER_CONFIG=$(mktemp -d) + export DOCKER_CONFIG + trap 'rm -rf "$DOCKER_CONFIG"' EXIT + printf '%s' "$HARBOR_PASSWORD" | + docker login harbor.vitatech.com.cn:19443 \ + --username "$HARBOR_USER" --password-stdin + image="$IMAGE_REPOSITORY:$IMAGE_TAG" + docker buildx build --builder default --platform linux/amd64 \ + --ssh default \ + --secret "id=git_known_hosts,src=$GIT_KNOWN_HOSTS" \ + --add-host git.vitatech.com.cn:192.168.18.200 \ + --file "$DOCKERFILE" --tag "$image" --load . + docker push "$image" + docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "$image" | + awk -v prefix="$IMAGE_REPOSITORY@" \ + 'index($0, prefix) == 1 { print; exit }' > published-image.txt + test -s published-image.txt + ''' + } + } + script { + env.PUBLISHED_IMAGE = readFile('published-image.txt').trim() + writeFile(file: 'release.txt', text: + 'service=' + env.SERVICE + '\nbranch=' + env.SOURCE_BRANCH + + '\ncommit=' + env.SOURCE_COMMIT + '\nbuild=' + env.BUILD_NUMBER + + '\nimage=' + env.PUBLISHED_IMAGE + '\n') + } + archiveArtifacts(artifacts: 'release.txt', fingerprint: true) + } + } + stage('Deploy test') { + when { expression { params.DEPLOY_ENV == 'test' } } + steps { + withCredentials([ + usernamePassword(credentialsId: 'harbor-backend', + usernameVariable: 'HARBOR_USER', + passwordVariable: 'HARBOR_PASSWORD') + ]) { + sh ''' + set -eu + set +x + DOCKER_CONFIG=$(mktemp -d) + export DOCKER_CONFIG + trap 'rm -rf "$DOCKER_CONFIG"' EXIT + printf '%s' "$HARBOR_PASSWORD" | + docker login harbor.vitatech.com.cn:19443 \ + --username "$HARBOR_USER" --password-stdin + bash "$DEPLOY_ROOT/deploy.sh" deploy "$SERVICE" \ + "$PUBLISHED_IMAGE" "$SOURCE_COMMIT" "$BUILD_NUMBER" + ''' + } + } + } + stage('Production deployment disabled') { + when { expression { params.DEPLOY_ENV == 'prod' } } + steps { + echo 'release image built and pushed. NOT DEPLOYED: production target is not configured.' + } + } + } + post { + success { + script { + currentBuild.description = env.SOURCE_BRANCH + ' / ' + + env.SOURCE_COMMIT.take(12) + + (params.DEPLOY_ENV == 'prod' ? ' / NOT DEPLOYED' : ' / test deployed') + } + } + } +} diff --git a/Jenkinsfile.rpc b/Jenkinsfile.rpc new file mode 100644 index 0000000..dab50c3 --- /dev/null +++ b/Jenkinsfile.rpc @@ -0,0 +1,123 @@ +pipeline { + agent { label 'layout-docker' } + options { + skipDefaultCheckout() + disableConcurrentBuilds() + timeout(time: 30, unit: 'MINUTES') + buildDiscarder(logRotator(numToKeepStr: '30')) + } + parameters { + choice(name: 'DEPLOY_ENV', choices: ['test', 'prod'], + description: 'test -> dev; prod -> release (build/push only, deployment disabled)') + } + environment { + SERVICE = 'micro-rpc' + IMAGE_REPOSITORY = 'harbor.vitatech.com.cn:19443/backend/micro_layout-rpc' + DOCKERFILE = 'build/srv/Dockerfile' + DEPLOY_ROOT = '/home/SHARE/devstack-scb/apps/layout-test' + } + stages { + stage('Checkout') { + steps { + script { + if (!(params.DEPLOY_ENV in ['test', 'prod'])) { + error('Invalid deployment environment') + } + env.SOURCE_BRANCH = params.DEPLOY_ENV == 'test' ? 'dev' : 'release' + def scmResult = checkout([ + $class: 'GitSCM', + branches: [[name: '*/' + env.SOURCE_BRANCH]], + userRemoteConfigs: [[ + url: 'ssh://git@git.vitatech.com.cn:12222/leo/micro_layout.git', + credentialsId: 'gitea-layout-read' + ]], + extensions: [[$class: 'CleanBeforeCheckout']] + ]) + env.SOURCE_COMMIT = scmResult.GIT_COMMIT + env.IMAGE_TAG = env.SOURCE_BRANCH + '-' + + env.SOURCE_COMMIT.take(12) + '-' + env.BUILD_NUMBER + } + } + } + stage('Build and push') { + steps { + sshagent(credentials: ['gitea-layout-read']) { + withCredentials([ + file(credentialsId: 'gitea-known-hosts', variable: 'GIT_KNOWN_HOSTS'), + usernamePassword(credentialsId: 'harbor-backend', + usernameVariable: 'HARBOR_USER', + passwordVariable: 'HARBOR_PASSWORD') + ]) { + sh ''' + set -eu + set +x + DOCKER_CONFIG=$(mktemp -d) + export DOCKER_CONFIG + trap 'rm -rf "$DOCKER_CONFIG"' EXIT + printf '%s' "$HARBOR_PASSWORD" | + docker login harbor.vitatech.com.cn:19443 \ + --username "$HARBOR_USER" --password-stdin + image="$IMAGE_REPOSITORY:$IMAGE_TAG" + docker buildx build --builder default --platform linux/amd64 \ + --ssh default \ + --secret "id=git_known_hosts,src=$GIT_KNOWN_HOSTS" \ + --add-host git.vitatech.com.cn:192.168.18.200 \ + --file "$DOCKERFILE" --tag "$image" --load . + docker push "$image" + docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "$image" | + awk -v prefix="$IMAGE_REPOSITORY@" \ + 'index($0, prefix) == 1 { print; exit }' > published-image.txt + test -s published-image.txt + ''' + } + } + script { + env.PUBLISHED_IMAGE = readFile('published-image.txt').trim() + writeFile(file: 'release.txt', text: + 'service=' + env.SERVICE + '\nbranch=' + env.SOURCE_BRANCH + + '\ncommit=' + env.SOURCE_COMMIT + '\nbuild=' + env.BUILD_NUMBER + + '\nimage=' + env.PUBLISHED_IMAGE + '\n') + } + archiveArtifacts(artifacts: 'release.txt', fingerprint: true) + } + } + stage('Deploy test') { + when { expression { params.DEPLOY_ENV == 'test' } } + steps { + withCredentials([ + usernamePassword(credentialsId: 'harbor-backend', + usernameVariable: 'HARBOR_USER', + passwordVariable: 'HARBOR_PASSWORD') + ]) { + sh ''' + set -eu + set +x + DOCKER_CONFIG=$(mktemp -d) + export DOCKER_CONFIG + trap 'rm -rf "$DOCKER_CONFIG"' EXIT + printf '%s' "$HARBOR_PASSWORD" | + docker login harbor.vitatech.com.cn:19443 \ + --username "$HARBOR_USER" --password-stdin + bash "$DEPLOY_ROOT/deploy.sh" deploy "$SERVICE" \ + "$PUBLISHED_IMAGE" "$SOURCE_COMMIT" "$BUILD_NUMBER" + ''' + } + } + } + stage('Production deployment disabled') { + when { expression { params.DEPLOY_ENV == 'prod' } } + steps { + echo 'release image built and pushed. NOT DEPLOYED: production target is not configured.' + } + } + } + post { + success { + script { + currentBuild.description = env.SOURCE_BRANCH + ' / ' + + env.SOURCE_COMMIT.take(12) + + (params.DEPLOY_ENV == 'prod' ? ' / NOT DEPLOYED' : ' / test deployed') + } + } + } +} diff --git a/build/crontab/Dockerfile b/build/crontab/Dockerfile index 264bac9..6ab78e9 100644 --- a/build/crontab/Dockerfile +++ b/build/crontab/Dockerfile @@ -4,19 +4,17 @@ ENV GOPRIVATE=git.vitatech.com.cn RUN git config --global url."ssh://git@git.vitatech.com.cn:12222/".insteadOf "https://git.vitatech.com.cn/" \ && mkdir -p -m 0700 /root/.ssh WORKDIR /src -COPY micro_layout/go.mod micro_layout/go.sum ./micro_layout/ -WORKDIR /src/micro_layout +COPY go.mod go.sum ./ RUN --mount=type=ssh,required=true \ --mount=type=secret,id=git_known_hosts,target=/root/.ssh/known_hosts,required=true \ go mod download -COPY micro_layout /src/micro_layout -RUN CGO_ENABLED=0 go build -trimpath -o /out/crontab ./cmd/crontab +COPY . . +RUN CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/crontab ./cmd/crontab FROM alpine:3.23 RUN apk add --no-cache ca-certificates tzdata WORKDIR /app RUN mkdir -p logs && chown 65532:65532 logs COPY --from=builder /out/crontab ./crontab -COPY micro_layout/configs/config.yaml ./configs/config.yaml USER 65532:65532 ENTRYPOINT ["./crontab"] diff --git a/build/srv/Dockerfile b/build/srv/Dockerfile index 2f65d2b..2caa6a4 100644 --- a/build/srv/Dockerfile +++ b/build/srv/Dockerfile @@ -4,20 +4,20 @@ ENV GOPRIVATE=git.vitatech.com.cn RUN git config --global url."ssh://git@git.vitatech.com.cn:12222/".insteadOf "https://git.vitatech.com.cn/" \ && mkdir -p -m 0700 /root/.ssh WORKDIR /src -COPY micro_layout/go.mod micro_layout/go.sum ./micro_layout/ -WORKDIR /src/micro_layout +COPY go.mod go.sum ./ RUN --mount=type=ssh,required=true \ --mount=type=secret,id=git_known_hosts,target=/root/.ssh/known_hosts,required=true \ go mod download -COPY micro_layout /src/micro_layout -RUN CGO_ENABLED=0 go build -trimpath -o /out/srv ./cmd/srv +COPY . . +RUN CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/srv ./cmd/srv \ + && CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/grpc-health ./cmd/healthcheck FROM alpine:3.23 RUN apk add --no-cache ca-certificates tzdata WORKDIR /app RUN mkdir -p logs && chown 65532:65532 logs COPY --from=builder /out/srv ./srv -COPY micro_layout/configs/config.yaml ./configs/config.yaml +COPY --from=builder /out/grpc-health ./grpc-health USER 65532:65532 EXPOSE 50112 ENTRYPOINT ["./srv"] diff --git a/cmd/healthcheck/main.go b/cmd/healthcheck/main.go new file mode 100644 index 0000000..bb83071 --- /dev/null +++ b/cmd/healthcheck/main.go @@ -0,0 +1,38 @@ +// Command healthcheck checks the existing gRPC Health service for container readiness. +package main + +import ( + "context" + "fmt" + "os" + "time" + + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/health/grpc_health_v1" +) + +func main() { + if err := check(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func check() error { + conn, err := grpc.NewClient("127.0.0.1:50112", grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + result, err := grpc_health_v1.NewHealthClient(conn).Check(ctx, &grpc_health_v1.HealthCheckRequest{}) + if err != nil { + return err + } + if result.Status != grpc_health_v1.HealthCheckResponse_SERVING { + return fmt.Errorf("gRPC service is %s", result.Status) + } + return nil +}