docker-compose
jenkins的docker-compose目录为为/home/jenkins,这个后面写脚本的时候需要对应上
version: '3'
services:docker_jenkins:restart: alwaysimage: jenkins/jenkins:ltscontainer_name: docker_jenkinsprivileged: true ports:- '8080:8080'- '50000:50000'volumes:- ./data/:/var/jenkins_home- /var/run/docker.sock:/var/run/docker.sock- /usr/bin/docker:/usr/bin/docker- /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
授权目录
- chmod 777 ./data -R
- chmod 777 /var/run/docker.sock (设置宿主机可执行,因为进入jenkins容器还会使用宿主机docker)
安装插件
- 搜索安装Git相关插件
- 搜索安装Gitlab相关插件
- 搜索安装Docker相关插件
凭据管理
- Gitlab 用户名密码
- 推送到阿里云镜像容器,docker login 的用户名密码
设置时区
System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', 'Asia/Shanghai')
点击运行
创建流水线
将jenkins生成的webhook地址复制到gitlab中
流水线脚本
下面脚本的/home/jenkins目录,需要指定为宿主机的挂载目录
需要修改credentialsId为自己的gitlab凭证、以及推送到镜像仓库的凭证
需要修改url为项目实际gitlab地址
java项目
node('built-in') { stage('Git Clone') {checkout([$class: 'GitSCM', branches: [[name: "origin/${env.gitlabSourceBranch}"]], extensions: [], userRemoteConfigs: [[credentialsId: 'ld-git', url: 'http://gitlab地址/asterism-back/aip-base.git']]])}stage('DockerPush-ld') { globalDockerRegistry="registry.cn-hangzhou.aliyuncs.com" credentials = "ledison7"imageStore="https://registry.cn-hangzhou.aliyuncs.com"tenantCode="fireworks" docker.withRegistry(imageStore,credentials) {sh encoding: 'UTF-8', label: 'Maven 缓存构建',script:'''docker run -i --rm -v /home/jenkins/mavenRespo:/var/jenkins_home/mavenRespo -v /home/jenkins/data/workspace/${JOB_NAME}:/usr/src/mymaven -w /usr/src/mymaven maven:3.6.3 mvn clean package -DskipTests -s ./go_settings.xmlsleep 1'''commitId = "${env.gitlabMergeRequestLastCommit}".substring(0,8)imageTag = "registry.cn-hangzhou.aliyuncs.com/fireworks/aip-base:latest"sh encoding: 'UTF-8', label: '镜像构建',script:' docker build -t '+imageTag+' -f Dockerfile . 'sh encoding: 'UTF-8', label: '镜像推送',script:' docker push '+imageTag sh encoding: 'UTF-8', label: '镜像清理',script:' docker rmi '+imageTag} }}
java项目Dockerfile
FROM openjdk:8LABEL maintainer="363192641@qq.com"COPY aip-base-core/target/*.jar /aip-base.jar
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo "Asia/Shanghai" > /etc/timezoneEXPOSE 8181ENTRYPOINT java -XX:+PrintFlagsFinal -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap $JAVA_OPTS -jar /aip-base.jar --spring.profiles.active=prod