持續集成(Continuous integration)是一種軟件開發實踐,每次集成都經過自動化的構建(包括編譯,發佈,自動化測試)來驗證,從而儘早地發現集成錯誤。html
持續集成工具:jenkinsjava
構建工具:mavennode
版本控制庫:gitlinux
鏡像倉庫:harborgit
工做流程:github
1. 開發人員提交代碼到Git版本倉庫;web
2. Jenkins人工/定時觸發項目構建;docker
3. Jenkins拉取代碼、代碼編碼、打包鏡像、推送到鏡像倉庫;apache
4. Jenkins在Docker主機建立容器併發布。json
環境規劃以下:
操做系統:centos7.6
# yum install git -y
# useradd git
# passwd git
# su - git
# mkdir tomcat-java-demo.git
# cd tomcat-java-demo.git
# git --bare init
# git clone git@192.168.1.165:/home/git/tomcat-java-demo.git
# cd /home/
# git clone https://github.com/lizhenliang/tomcat-java-demo
# cd /home/tomcat-java-demo
修改私有鏡像倉庫地址:
# vim .git/config
url = git@192.168.1.165:/home/git/tomcat-java-demo.git
本地代碼提交到git鏡像倉庫
# git add .
# git commit -m 'all'
# git push origin master
備註:初次提交可能報錯
解決方案:
# git config --global user.email "you@example.com"
# git config --global user.name "Your Name"
參考:https://www.cnblogs.com/521football/p/10571304.html
# tar -zxvf jdk-8u45-linux-x64.tar.gz
# tar -zxvf apache-maven-3.5.0-bin.tar.gz
# mv apache-maven-3.5.0 /usr/local/maven
# mv jdk1.8.0_45/ /usr/local/jdk
配置java環境變量
vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
# java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
# tar -zxvf apache-tomcat-8.0.46.tar.gz
# mv apache-tomcat-8.0.46 /usr/local/jenkins_tomcat
# mv /home/jenkins.war /usr/local/jenkins_tomcat/webapps/ROOT.war
# cd /usr/local/jenkins_tomcat
# sh bin/startup.sh
# tail -f logs/catalina.out
日誌中查看登陸密碼:cb2f3ddce17b494b847abf619a6c3ca5
登陸jenkins: http://192.168.1.166:8080
能夠跳過插件的安裝…….
更新jenkins官方源http替換https
Jenkins---->插件管理--->Advanced --->Update Site
修改後submit---> Check now
安裝插件
系統管理--->插件管理--->Available---> 選擇pipeline, SCM to job插件進行安裝
# ssh-keygen
# ssh-copy-id git@192.168.1.165
添加全局憑證
Jenkins--->憑據--->系統--->全局憑據
上圖key粘貼進去便可:
# cat ~/.ssh/id_rsa
參考:https://www.cnblogs.com/521football/p/10484390.html
建立Dockerfile-tomcat
# vim Dockerfile-tomcat
FROM centos:7
MAINTAINER xbl
ENV VERSION=8.5.39
RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \
yum clean all && \
rm -rf /var/cache/yum/*
RUN wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz && \
tar zxf apache-tomcat-${VERSION}.tar.gz && \
mv apache-tomcat-${VERSION} /usr/local/tomcat && \
rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/* && \
mkdir /usr/local/tomcat/webapps/test && \
echo "ok" > /usr/local/tomcat/webapps/test/status.html && \
sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/tomcat/bin
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]
# docker build -t tomcat:v1 -f Dockerfile-tomcat .
啓動鏡像
docker run -d tomcat:v1
添加harbor可信任
# vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://f1361db2.m.daocloud.io"],
"insecure-registries":["192.168.1.165"]
}
# systemctl restart docker.service
harbor用戶以及密碼登陸
# docker login 192.168.1.165 -u xubaolong -pxxx
# docker tag tomcat:v1 192.168.1.165/library/tomcat:v1
# docker push 192.168.1.165/library/tomcat:v1
建立流水線:
備註:此參數能夠拓展:master(生產),dev(開發),test(測試)
Pipeline工做流程:
1. 從git倉庫中拉取代碼
2. maven構建項目及打包
3. 項目打包並推送鏡像到倉庫
4. 部署到docker主機
Pipeline腳本以下:
node { // 指定Slave標籤
// 拉取代碼
stage('Git Checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'da6ba478-0899-4dcb-9a8f-0ec24dcd9022', url: 'git@192.168.1.165:/home/git/tomcat-java-demo.git']]])
}
// 代碼編譯
stage('Maven Build') {
sh '''
export JAVA_HOME=/usr/local/jdk
/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true
'''
}
// 項目打包到鏡像並推送到鏡像倉庫
stage('Build and Push Image') {
sh '''
REPOSITORY=192.168.1.165/project/tomcat-java-demo:${branch}
cat > Dockerfile << EOF
FROM 192.168.1.165/library/tomcat:v1
MAINTAINER xbl
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
EOF
docker build -t $REPOSITORY .
docker login 192.168.1.165 -u xubaolong -p xxx
docker push $REPOSITORY
'''
}
// 部署到Docker主機
stage('Deploy to Docker') {
sh '''
REPOSITORY=192.168.1.165/project/tomcat-java-demo:${branch}
docker rm -f tomcat-java-demo |true
# docker image rm $REPOSITORY |true
docker pull $REPOSITORY
docker container run -d --name tomcat-java-demo -p 88:8080 $REPOSITORY
'''
}
}
經過瀏覽器來訪問tomcat-java-demo項目:http://192.168.1.166:88/
備註:因爲tomcat,jdk,maven二進制包安裝,有須要的小夥伴可自行下載
連接:https://pan.baidu.com/s/1XKWEVy-nPErNBQDCSRd-5w 密碼:h8q4