部署准备

This commit is contained in:
admin committed 2026-11-12 09:34:43 +08:00
1 parent a82a843b76
commit 1c495d3ae0
3 files changed
+140 -5

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
.git
.idea
.DS_Store
bin
logs
.env
.env.*
*.local.yaml
*.local.yml
*.local.json
configs
deploy
release.txt
published-image.txt
+3 -5
View File
@@ -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/" \ RUN git config --global url."ssh://git@git.vitatech.com.cn:12222/".insteadOf "https://git.vitatech.com.cn/" \
&& mkdir -p -m 0700 /root/.ssh && mkdir -p -m 0700 /root/.ssh
WORKDIR /src WORKDIR /src
COPY gin_layout/go.mod gin_layout/go.sum ./gin_layout/ COPY go.mod go.sum ./
WORKDIR /src/gin_layout
RUN --mount=type=ssh,required=true \ RUN --mount=type=ssh,required=true \
--mount=type=secret,id=git_known_hosts,target=/root/.ssh/known_hosts,required=true \ --mount=type=secret,id=git_known_hosts,target=/root/.ssh/known_hosts,required=true \
go mod download go mod download
COPY gin_layout /src/gin_layout COPY . .
RUN CGO_ENABLED=0 go build -trimpath -o /out/api ./cmd/api RUN CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/api ./cmd/api
FROM alpine:3.23 FROM alpine:3.23
RUN apk add --no-cache ca-certificates tzdata RUN apk add --no-cache ca-certificates tzdata
WORKDIR /app WORKDIR /app
COPY --from=builder /out/api ./api COPY --from=builder /out/api ./api
COPY gin_layout/configs/config.yaml ./configs/config.yaml
USER 65532:65532 USER 65532:65532
EXPOSE 8080 EXPOSE 8080
ENTRYPOINT ["./api"] ENTRYPOINT ["./api"]
Vendored
+123
View File
@@ -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 = 'gin-api'
IMAGE_REPOSITORY = 'harbor.vitatech.com.cn:19443/backend/gin_layout-api'
DOCKERFILE = '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/gin_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')
}
}
}
}