部署准备
This commit is contained in:
1 parent
ee07869839
commit
c191974df7
6 files changed
+296
-10
No files matched your search
@@ -7,3 +7,7 @@ logs
|
||||
*.local.yaml
|
||||
*.local.yml
|
||||
*.local.json
|
||||
configs
|
||||
deploy
|
||||
release.txt
|
||||
published-image.txt
|
||||
@@ -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')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+123
@@ -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')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"]
|
||||
@@ -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"]
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user