Docker构建go环境
FROM golang:1.20-alpine3.18 AS upload
WORKDIR /upload
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct
COPY . .
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
&& apk update \
&& apk add --no-cache bash \
&& apk add upx make bash gcc g++ git \
&& make
FROM alpine:3.18
WORKDIR /upload
ENV TZ Asia/Shanghai
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
&& apk update \
&& apk add musl-dev \
&& apk add --no-cache bash \
&& apk add --no-cache curl \
&& apk add --no-cache tzdata \
&& ln -s /usr/lib/libc.so /usr/lib/libresolv.so.2 \
&& mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
RUN apk add tzdata && cp /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& apk del tzdata
COPY --from=upload /upload/release /upload
#也可使用ubuntu镜像
FROM ubuntu:22.04
WORKDIR /user-center
ENV TZ=Asia/Shanghai \
DEBIAN_FRONTEND=noninteractive
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
&& sed -i s@/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
&& apt update \
&& apt install -y tzdata \
&& ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& rm -rf /var/lib/apt/lists/*
COPY ./zgy-usercenter .
RUN chmod +x zgy-usercenter
ENTRYPOINT ["./zgy-usercenter","--config=config.toml"]
#docker-compose.yaml
version: '3'
services:
app:
image: 192.168.2.247/app/app:1.2
container_name: app
restart: always
ports:
- "7201:7201"
volumes:
- .:/work
- /usr/local/work/center/conf/zgy-appCenter/:/conf/
working_dir: /work
command:
- sh
- -c
- |
cp -r /app/* /work/
./zgy-appCenter --config=/conf/config.toml >> app.log 2>&1
networks:
- work
tty: true
networks:
work:
external:
name: work
#pipeline脚本
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout scmGit(branches: [[name: '$test']], extensions: [], userRemoteConfigs: [[credentialsId: '123123123123123123', url: 'http://jjy.jjy.com/jjy/jjy.git']])
}
}
stage('镜像构建') {
steps {
sh '''case $status in
deploy)
rm -rf Dockerfile
if [ "$project" = "app" ]; then
cp /usr/local/work/docker/$project/Dockerfile ${JENKINS_HOME}/workspace/$JOB_NAME/
docker build -t $project .
elif [ "$project" = "base" ]; then
cp /usr/local/work/docker/$project/Dockerfile ${JENKINS_HOME}/workspace/$JOB_NAME/
docker build -t $project .
elif [ "$project" = "log" ]; then
cp /usr/local/work/docker/$project/Dockerfile ${JENKINS_HOME}/workspace/$JOB_NAME/
docker build -t $project .
elif [ "$project" = "ups" ]; then
cp /usr/local/work/docker/$project/Dockerfile ${JENKINS_HOME}/workspace/$JOB_NAME/
docker build -t $project .
elif [ "$project" = "user" ]; then
cp /usr/local/work/docker/$project/Dockerfile ${JENKINS_HOME}/workspace/$JOB_NAME/
docker build -t $project .
fi;;
rollback)
exit ;;
esac'''
}
}
stage('镜像上传') {
steps {
sh '''case $status in
deploy)
docker tag $project:latest 192.168.11.11/$project/$project:$version
docker push 192.168.11.11/$project/$project:$version
docker rmi $project:latest
;;
rollback)
exit
;;
esac'''
}
}
stage('发布版本') {
steps {
script {
def remoteHost = '192.168.11.11'
def remoteCommand = ""
switch (project) {
case 'app':
remoteCommand = "sed -i -E \"s/app:[0-9]+\\.[0-9]+/app:${version}/g\" /usr/local/work/center/zgy-app/docker-compose.yml && docker-compose -f /usr/local/work/center/zgy-app/docker-compose.yml up -d --force-recreate"
break
case 'base':
remoteCommand = "sed -i -E \"s/base:[0-9]+\\.[0-9]+/base:${version}/g\" /usr/local/work/center/zgy-base/docker-compose.yml && docker-compose -f /usr/local/work/center/zgy-base/docker-compose.yml up -d --force-recreate"
break
case 'log':
remoteCommand = "sed -i -E \"s/log:[0-9]+\\.[0-9]+/log:${version}/g\" /usr/local/work/center/zgy-log/docker-compose.yml && docker-compose -f /usr/local/work/center/zgy-log/docker-compose.yml up -d --force-recreate"
break
case 'ups':
remoteCommand = "sed -i -E \"s/ups:[0-9]+\\.[0-9]+/ups:${version}/g\" /usr/local/work/center/zgy-ups/docker-compose.yml && docker-compose -f /usr/local/work/center/zgy-ups/docker-compose.yml up -d --force-recreate"
break
case 'user':
remoteCommand = "sed -i -E \"s/user:[0-9]+\\.[0-9]+/user:${version}/g\" /usr/local/work/center/zgy-user/docker-compose.yml && docker-compose -f /usr/local/work/center/zgy-user/docker-compose.yml up -d --force-recreate"
break
}
sh "ssh root@${remoteHost} \"${remoteCommand}\""
}
}
}
}
}