1、jenkins全局配置
1.1、maven配置
1.2、jdk配置
1.3、git配置
2、构建环境配置
2.1、安装时间插件 Date Parameter
2.2、Git Parameter 插件安装
3、pipeline如下
pipeline {
agent any
environment {
image_name = "192.168.122.150/ken-test/price-service:${date}"
server_name = "ken-price-service"
namespace = "hc-ken"
}
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM',
branches: [[name:"${params.TAG}"]], extensions: [],
userRemoteConfigs: [[credentialsId: 'xxxxx',
url: 'https://codeup.aliyun.com/63dfsafaew16cdbdgwe2a87dd6/hc-ken/ken-price.git']]])
}
}
stage('编译jar包'){
steps {
retry(2) { sh "/opt/maven/bin/mvn clean package -U -Dmaven.test.skip=true" }
}
}
stage('构建镜像&推送镜像仓库'){
steps {
retry(2) {
sh '''
SPRING_PROFILES="ken_test"
LINUX_TIME_ZONE="Africa/Lagos"
JAR_FILE="price-service-1.0.0-SNAPSHOT.jar"
cd ./price-service
docker build --build-arg SPRING_PROFILES="${SPRING_PROFILES}" --build-arg LINUX_TIME_ZONE="${LINUX_TIME_ZONE}" --build-arg JAR_FILE="${JAR_FILE}" -t "${image_name}" .
docker login -uadmin -pHarbor12345 192.168.122.150
docker push ${image_name} && docker rmi ${image_name}
'''
}
}
}
stage('部署'){
steps {
retry(2) { sh 'kubectl --kubeconfig /opt/kubernetes/config set image -n hc-ken deploy/${server_name} ${server_name}=${image_name}' }
//retry(2) { echo 'test' }
}
}
}
}
4、Dockerfile文件如下
[root@jenkins price-service]# cat Dockerfile
#1基础镜像
FROM harbor.hcloud.com:81/library/jdk-time-as-skywalking:8-jdk-alpine
#2接收外部参数 阿波罗配置、激活的环境、jar、时区
ARG APOLLO_KEY
ARG APOLLO_APP_ID
ARG APOLLO_META
ARG APOLLO_CLUSTER
ARG SPRING_PROFILES
ARG LINUX_TIME_ZONE
ARG JVM_TIME_ZONE
ARG JAR_FILE
#3修改系统时区
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/${LINUX_TIME_ZONE} /etc/localtime
#4创建目录
RUN mkdir -p /home/juser/app/bin
ADD target/${JAR_FILE} /home/juser/app/bin/app.jar
#5cpoy启动脚本
ADD jenkins/restart_docker.sh /home/juser/app/bin/restart.sh
RUN chmod +x /home/juser/app/bin/restart.sh
RUN chown -R juser:juser /home/juser
ENV APOLLO_KEY "${APOLLO_KEY}"
ENV APOLLO_APP_ID "${APOLLO_APP_ID}"
ENV APOLLO_META "${APOLLO_META}"
ENV APOLLO_CLUSTER "${APOLLO_CLUSTER}"
ENV SPRING_PROFILES "${SPRING_PROFILES}"
ENV JVM_TIME_ZONE "${JVM_TIME_ZONE}"
#6切换用户
USER juser
WORKDIR /home/juser
#7暴露端口
EXPOSE 9070
#8执行脚本
ENTRYPOINT ["/home/juser/app/bin/restart.sh","start"]
#CMD ["/home/juser/app/bin/restart.sh","http://192.168.122.227:8080 ","175-dev-data","dev"]
5、构建完成如下