Jenkins与Docker的自动化CI/CD流水线实战

标签(空格分隔): docker的部分


  • 一:什么是CI/CD
  • 二: 发布流程设计
  • 三:部署Git仓库并上传测试代码

一:什么是CI/CD

持续集成(Continuous Integration,CI):代码合并、构建、部署、测试都在一起,不断地执行这个过程,并对结果反馈。
持续部署(Continuous Deployment,CD):部署到测试环境、预生产环境、生产环境。
持续交付(Continuous Delivery,CD):将最终产品发布到生产环境,给用户使用。
CI/CD 的 流水线特点:

高效的CI/CD环境可以获得:
• 及时发现问题
• 大幅度减少故障率
• 加快迭代速度
• 减少时间成本

二:发布流程设计

image_1e5makhk81nkqgfl14k47mt1ma49.png-114.8kB


image_1e5mal8bo44nt8d94kjuc6tom.png-1329kB

三:部署gitlab 并上传代码

参照文档:https://blog.51cto.com/flyfish225/2145495

在node05.flyfish 上面部署gitlab 

image_1e5mcm0sg1429sec1lappvqh0u13.png-395.5kB

image_1e5mcmfjar0g1su6hpshmp1mmn1g.png-323.9kB


Git global setup
git config --global user.name "flyfish"
git config --global user.email "yangyangsirit@163.com"

Create a new repository
git clone git@node05.flyfish:flyfish/java-project.git
cd java-project
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Push an existing folder
cd existing_folder
git init
git remote add origin git@node05.flyfish:flyfish/java-project.git
git add .
git commit -m "Initial commit"
git push -u origin master

Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin git@node05.flyfish:flyfish/java-project.git
git push -u origin --all
git push -u origin --tags

image_1e5mctcbj8ibfqg7eeuis2h82d.png-363.5kB

四: 部署jenkins

在node06.flyfish 上面配置jenkins

tar -zxvf apache-tomcat-8.5.54.tar.gz
mv apache-tomcat-8.5.54 /usr/local/tomcat

cd /usr/local/tomcat/conf
vim context.xml 

---
在tomcat的/conf/context.xml中的<Context>中添加
<!--这里单位是 KB-->
<Resources cachingAllowed="true" cacheMaxSize="100000" />
---

rm -rf /usr/local/tomcat/webapps/* 

mv jenkins.war /usr/local/tomcat/webapps/jenkins.war

cd /usr/local/tomcat/bin/
./startup.sh 

image_1e5me64i8o1a1t2t1jmnd0ak9k2q.png-156.2kB

image_1e5me6n444ps1gvf13vs1v1enee37.png-83.2kB

image_1e5me7hak1os91ft11ohk1pbtjiu43.png-37.5kB

http://192.168.100.16:8080/jenkins

image_1e5me80rsbmm1qd1tgfmp61bme4g.png-294.7kB

image_1e5ofdfkvf90a5fpg31cjqrn99.png-520.6kB


jenkins 安装插件
更换下载安装源
https://updates.jenkins-zh.cn/update-center.json。

image_1e5ofg81qjtl1j0k117i1ikfi05m.png-161.8kB

image_1e5ofhlcb152ihql3ak1hdni5q13.png-102.1kB

装一个Pipeline

image_1e5ofn7bl1vbc1dr81imm1nj8vnb1t.png-29.3kB

image_1e5ofm3p013ol186ug9f23kqu61g.png-112.5kB

image_1e5og5kl01v0stts8141i381mvk2a.png-95.1kB

image_1e5ogb7prvt13u7l4v1ml9gf52n.png-98.3kB


创建 一个从节点

image_1e5ogda5pg9l1gbi18qpf4l7834.png-221.2kB

image_1e5ogdrfm7d6hgilii15k315g83h.png-405.9kB

image_1e5oiduqpr133u626k1hre3gv6p.png-317.2kB

image_1e5oifapc1991u3io6n1jfi2oe76.png-139kB


创建任务 

image_1e5oha40fp9o85s15501r1evok55.png-222.1kB

image_1e5ohar5f10531q01o1u1l76uad5i.png-231kB

image_1e5ohodaj8p6175218q61fcoul95v.png-123.5kB

image_1e5ohru00l7614cl1aee1e7upi96c.png-338.6kB


安装maven
node01.flyfish

tar -zxvf apache-maven-3.5.0-bin.tar.gz

mv apache-maven-3.5.0 /usr/local/maven

vim /etc/profile
---
export MAVEN_HOME=/usr/local/maven
export CLASSPATH=.:$MAVEN_HOME/jre/lib:$MAVEN_HOME/lib:$MAVEN_HOME/lib/tools.jar
PATH=$PATH:$HOME/bin:$MAVEN_HOME/bin:
---

配置maven 的 下载依赖包为阿里云的源

cd /usr/local/maven/conf

vim settings.xml 

在<mirrors>.....<mirrors> 之间 加上 阿里云的源 
-----
<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
----

mvn -version 

image_1e5oj4talagt10ah26imefuecc.png-74.5kB


无密码登录下载 代码

cd .ssh/

ssh-copy-id git@192.168.100.15

image_1e5p1ia3q1a0c1p2jgijs3r28p9.png-106.3kB


pipline 流水线语法:

node ("jenkins-slave-100-11") {   // 指定Slave标签
   // 拉取代码
   stage('Git Checkout') { 
     git credentialsId: '71eb6de5-abea-4416-a3ef-753efcdcaa98', url: 'http://node05.flyfish/flyfish/java-project.git'  (这个地方需要用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.100.16/project/java-project:${Branch}
cat > Dockerfile << EOF
FROM 192.168.100.16/library/tomcat:v1
RUN rm -rf /usr/local/tomcat/webapps/ROOT.war
COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
EOF
docker build -t $REPOSITORY .
docker login -u flyfish -p Flyfish225 192.168.100.16
docker push $REPOSITORY
'''
   }
   // 部署到Docker主机
   stage('Deploy to Docker') {
        sh '''
        REPOSITORY=192.168.100.16/project/java-project:${Branch}
        docker rm -f java-project |true
        docker image rm $REPOSITORY |true
        docker login -u flyfish -p Flyfish225 192.168.100.16
        docker container run -d --name java-project -p 89:8080 $REPOSITORY
        '''
   }
}

image_1e5rvi89gcsia3smroa5j120k26.png-114.9kB

image_1e5rvcv9jnv61419123d1mnd1aum.png-293.1kB

image_1e5s174nvg7s4m2b9t8qvea42j.png-100.1kB

image_1e5s17o0k5qk55ofbq4kd15rv30.png-155.7kB

image_1e5s18b6n1vt43s71n2mut72303d.png-252.9kB

image_1e5s193sl1ctbjg01dbd7lp1ur73q.png-461.6kB

image_1e5s1aots4t116c01uj519gdsn69.png-638kB

image_1e5s1blpq48u1cs21u7u1q7j5aom.png-264.5kB

image_1e5s1dkdc1rv01a481n3e1ud011ei13.png-148.6kB

访问项目

image_1e5s1ef6c1obahdrq51vtd13qv1g.png-4631.7kB