jenkins docker 发布
分享下怎么使用jenkins 发布 docker
首先准备docker的相关部分
docker tomcat基础镜像,这边使用centos7做系统,dockerfile如下:
#tomcat基础镜像
#我这边先把tomcat 各项配置好了,然后重新打包,传入docker,tomcat的数据目录设置在 /data/www 下 ,机器上先装了JDK FROM centos:7 MAINTAINER www.cnblogs.com/fanpiao ENV VERSION=8.5.42 ENV JAVA_HOME /usr/local/jdk WORKDIR /usr/local/ ADD apache-tomcat-${VERSION}.tar.gz . RUN mv apache-tomcat-${VERSION} tomcat && \ rm -rf apache-tomcat-${VERSION}.tar.gz && \ yum install -y unzip && \ mkdir -p /data/www #ENV PATH $PATH:/usr/local/tomcat/bin #WORKDIR /usr/local/tomcat #EXPOSE 8080 #CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
打包成镜像,并上传到容器仓库
docker build -t registry.cn-beij.aliyuncs.com/basis/tomcat:v7 .
#可以和我一样使用阿里云提供的容器镜像服务,免费的
docker push registry.cn-beij.aliyuncs.com/basis/tomcat:v7
上传完成后备用
#jenkins pipeline 脚本准备
node { def JAVA_HOME = tool 'jdk' env.PATH = "${JAVA_HOME}/bin:${env.PATH}" stage('拉取代码') { checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '1', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'http://svn.yin.cn/repos/${version}/project']], quietOperation: false, workspaceUpdater: [$class: 'UpdateUpdater']]) } stage('构建') { if ("${Status}"== 'Deploy') { echo "JDK: ${JAVA_HOME}" sh "/usr/local/maven/bin/mvn clean install -pl $module -am -U -Dmaven.test.skip=true" echo '需要构建' } else { echo '不需要构建' } } def web_name = "${JOB_NAME}" stage('Status判断') { if ("${Status}"== 'Deploy') { jar_file = sh ( script: 'find $WORKSPACE/$module/target -name *.war', returnStdout: true ).trim() sh 'mkdir -p $WORKSPACE/bak/$BUILD_ID' sh "(cp -a '${jar_file}' $WORKSPACE/bak/$BUILD_ID)" echo '部署' } else { jar_file = sh ( script: 'find $WORKSPACE/bak/$Rollback_tag -name *.war', returnStdout: true ).trim() echo '回滚' } } war_file_name = sh ( script: "(basename '${jar_file}')", returnStdout: true ).trim() config_file_name = sh ( script: 'basename ${config_file}', returnStdout: true ).trim() old_BUILD_NUMBER = sh ( script: "(awk 'BEGIN{print '${BUILD_NUMBER}' - 1 }')", returnStdout: true ).trim() stage('打印部署参数') { echo "jar_file: ${jar_file}" echo "war_file_name: ${war_file_name}" echo "config_file: ${config_file}" echo "config_file_name: ${config_file_name}" echo "job_name: ${web_name}" echo "old_BUILD_NUMBER: ${old_BUILD_NUMBER}" echo "JDK: ${JAVA_HOME}" } stage('构建镜像') { if ("${Status}"== 'Deploy') { REPOSITORY="registry.cn-beij.aliyuncs.com/service/${web_name}:${BUILD_NUMBER}" sh """ mkdir -p dockerfile cd dockerfile yes|cp -a $config_file/log4j2.xml . #yes|cp -a $config_file/config.properties . yes|cp -a ${jar_file} . cat > Dockerfile << EOF FROM registry.cn-beij.aliyuncs.com/basis/tomcat:v7 COPY ${war_file_name} /tmp RUN unzip -oq /tmp/${war_file_name} -d /data/www/ && \ rm -rf /tmp/${war_file_name} COPY log4j2.xml /data/www/WEB-INF/classes/log4j2.xml #COPY config.properties /data/www/WEB-INF/classes/config.properties EXPOSE 8080 CMD ["/usr/local/tomcat/bin/catalina.sh", "run"] EOF docker build -t $REPOSITORY . docker push $REPOSITORY docker image rm $REPOSITORY |true """ } else { REPOSITORY="registry.cn-beij.aliyuncs.com/service/${web_name}:${Rollback_tag}" echo "无需构建镜像直接回滚" } } stage('部署') { sh "/usr/bin/ansible-playbook /etc/ansible/playbook/docker-deploy.yaml -f 5 -e host=${host} -e REPOSITORY=${REPOSITORY} -e docker_name=${web_name} -e run_port=${run_port}" } }
ansbile-playbook准备,ansbile ,host主机的配置请自己先配置
--- - hosts: '{{ host }}' remote_user: java gather_facts: false vars: REPOSITORY: '{{ REPOSITORY }}' docker_name: '{{ web_name }}' run_port: '{{ run_port }}' tasks: #下载镜像 - name: "Download images" shell: docker pull {{ REPOSITORY }} #删除在运行的旧容器 - name: "rm container" shell: docker rm -f {{ docker_name }} | true #启动容器 - name: "run container" #shell: docker run -d -p {{ run_port }}:8080 -v /usr/local/jdk:/usr/local/jdk -v /etc/localtime:/etc/localtime:ro --name {{ docker_name }} {{ REPOSITORY }} shell: docker run -d -p {{ run_port }}:8080 \ -v /usr/local/jdk:/usr/local/jdk \ -v /data/logs/Blockchain:/data/logs/Blockchain \ -v /data/logs/tomcatlogs/{{ docker_name }}:/usr/local/tomcat/logs \ -v /etc/localtime:/etc/localtime:ro \ --name {{ docker_name }} {{ REPOSITORY }} register: result #打印启动命令 - name: "print information" debug: msg={{ result.cmd }} ~
jenkisn 的配置,需要如下设置如下参数
在流水线这里配置上刚才准备的 jenkins pipeline 脚本
保存后就完成了,效果如下