高效的CI/CD環境能得到什麼呢?html
要想作到一個高效的CI/CD流程,須要有能力整合DevOps工具鏈及多環境適配,而且設計之初以自動化爲原則,如一鍵部署、一鍵升級。java
1. 開發語言:Java 2. 項目代碼版本管理:Git 3. 代碼編譯:Maven 4. 持續集成:Jenkins 5. 交付:以Docker鏡像形式交付,提交至Harbor 6. 部署:Docker主機建立容器
環境規劃以下:node
角色 | IP |
---|---|
Docker | 192.168.30.128 |
Jenkins | 192.168.30.129 |
Git | 192.168.30.130 |
工做流程:linux
首先部署一個Git倉庫,存儲測試的代碼,這裏選擇solo博客程序作測試:https://github.com/b3log/solo 。該步驟在 192.168.30.130 上執行。git
# yum install -y git
# useradd git# passwd git
# su - git# mkdir solo.git# cd solo.git# git init --bare #建立裸倉庫# vim config #添加用戶,不然commit報錯[user] name = lzx email = lzx@lzx.com
拉取代碼github
# git clone https://github.com/b3log/solo.git
添加私有倉庫地址web
# cd solo/# git remote remove origin# git remote add origin git@192.168.30.130:/home/git/solo.git
提交到私有倉庫docker
# git add .# git commit -m "all"# git push origin master
以前部署過,此處省略,參考這裏:部署Harborapache
須要安裝Docker及配置Java環鏡。 該步驟在 192.168.30.128 上執行。vim
# yum install -y yum-utils device-mapper-persistent-data lvm2
# curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
# yum install -y docker-ce
# systemctl start docker && systemctl enable docker
# tar zxf jdk-8u191-linux-x64.tar.gz && mv jdk1.8.0_191/ /usr/local/jdk# wget http://mirrors.shu.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz# tar zxf apache-maven-3.6.0-bin.tar.gz && mv apache-maven-3.6.0/ /usr/local/maven# vim /etc/profileMAVEN_HOME=/usr/local/maven JAVA_HOME=/usr/local/jdk PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport JAVA_HOME PATH CLASSPATH# source !$
# ssh-keygen# ssh-copy-id git@192.168.30.130# ssh git@192.168.30.130 #測試登陸
基於CentOS鏡像構建Jenkins鏡像,同時將安裝jdk、maven。該步驟在 192.168.30.129 上執行。
# vim Dockerfile-java
FROM centos:latest MAINTAINER lzx lzx@lzxlinux.com RUN yum install -y wget vim curl unzip iproute net-tools && \ yum clean all && \ rm -rf /var/cache/yum/* ADD jdk-8u191-linux-x64.tar.gz /usr/local/ RUN mv /usr/local/jdk1.8.0_191/ /usr/local/jdk && \ wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.5.39/bin/apache-tomcat-8.5.39.tar.gz && \ tar zxf apache-tomcat-8.5.39.tar.gz && \ mv apache-tomcat-8.5.39 /usr/local/tomcat && \ wget http://mirrors.shu.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz && \ tar zxf apache-maven-3.6.0-bin.tar.gz && \ mv apache-maven-3.6.0 /usr/local/maven && \ wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war && \ rm -rf /usr/local/tomcat/webapps/* && \ unzip jenkins.war -d /usr/local/tomcat/webapps/ROOT && \ rm -rf jenkins.war jdk-* apache-* ENV JAVA_HOME /usr/local/jdk ENV JRE_HOME /usr/local/jdk/jre ENV CATALINA_HOME /usr/local/tomcat ENV MAVEN_HOME /usr/local/maven ENV CLASSPATH $JAVA_HOME/lib:$JRE_HOME/lib:$JRE_HOME/lib/charsets.jar ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$CATALINA_HOME/lib:$MAVEN_HOME/bin WORKDIR /usr/local/tomcat EXPOSE 8080 CMD ["catalina.sh","run"]
# docker build -t jenkins-2.164 -f Dockerfile-java .
# docker run -d -p 8080:8080 --name jenkins jenkins-2.164:latest
啓動完打開網頁訪問192.168.30.129:8080
,安裝推薦插件,設置帳戶密碼。
系統管理
→ 憑據配置
→ 憑據
→ Jenkins
→ 全局憑據
→ 添加憑據
系統管理
→ 節點管理
→ 新建節點
標籤請注意不要填錯,用於識別哪個slave執行任務。
新建任務
→ 流水線
,名稱任意。
node ("192.168.30.128") { #指定slave標籤#拉取代碼stage('Git Checkout') { checkout([$class: 'GitSCM',branches: [[name: '$Tag']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@192.168.30.130:/home/git/solo.git']]])} #$Tag引用用戶交互輸入的tag#代碼編譯stage('Maven Build') { sh ''' export JAVA_HOME=/usr/local/jdk /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true '''}#項目打包到鏡像並推送到鏡像倉庫stage('Build and Puah Image') { sh ''' docker login -u admin -p Harbor12345 harbor.uqp.com REPOSITORY=harbor.uqp.com/project/solo:${Tag} cat > Dockerfile2 <<EOF FROM harbor.uqp.com/library/tomcat-85:latest RUN rm -rf /usr/local/tomcat/webapps/ROOT COPY target/*.war /usr/local/tomcat/webapps/ROOT.war CMD ["catalina.sh","run"]EOF docker build -t $REPOSITORY -f Dockerfile2 . docker push $REPOSITORY ''' #根據$Tag做爲鏡像版本號}#部署到Docker主機stage('Deploy to Docker') { sh ''' REPOSITORY=harbor.uqp.com/project/solo:${Tag} docker rm -f blog-solo | true docker image rm $REPOSITORY | true docker login -u amdin -p Harbor12345 harbor.uqp.com docker container run -d -v /usr/local/jdk:/usr/local/jdk --name blog-solo -p 88:8080 $REPOSITORY '''}}
# cd solo/# touch src/main/webapp/123.txt# git add .# git commit -m "touch 123.txt"# git tag 1.0.0# git push origin 1.0.0
帶參數執行構建
# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 386b2097e507 harbor.uqp.com/project/solo:1.0.0 "catalina.sh run" 3 minutes ago Exited (255) 2 minutes ago blog-solo
容器ID與Jenkins上一致,但意外退出,查看緣由
# docker logs 38[ERROR]-[2019-03-26 16:41:47]-[org.b3log.latke.Latkes:786]: Read skin [Pinghsu]'s configuration failed: null [ERROR]-[2019-03-26 16:41:47]-[org.b3log.solo.SoloServletListener:316]: Can't load the default skins, please make sure skin [Pinghsu] is under skins directory and structure correctly
提示皮膚配置爲空,所以容器啓動不起來。整個過程當中,maven構建這一步可能有誤,但jenkins執行過程當中未報錯;也有多是b3log官方倉庫有問題。
對應業務的docker鏡像已經上傳到harbor中。
大體過程是沒有問題的,基本上能夠實現業務基於Jenkins與Docker的自動化發佈。後續有空再更新優化maven過程。做爲自動化CI/CD流水線仍是有不少優勢的,例如: