diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..19dce46 --- /dev/null +++ b/.dockerignore @@ -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 diff --git a/Dockerfile b/Dockerfile index 5bad33c..6884882 100644 --- a/Dockerfile +++ b/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 gin_layout/go.mod gin_layout/go.sum ./gin_layout/ -WORKDIR /src/gin_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 gin_layout /src/gin_layout -RUN CGO_ENABLED=0 go build -trimpath -o /out/api ./cmd/api +COPY . . +RUN CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/api ./cmd/api FROM alpine:3.23 RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /out/api ./api -COPY gin_layout/configs/config.yaml ./configs/config.yaml USER 65532:65532 EXPOSE 8080 ENTRYPOINT ["./api"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..e6639d9 --- /dev/null +++ b/Jenkinsfile @@ -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') + } + } + } +}