[关闭]
@zhangyy 2021-04-15T10:51:57.000000Z 字数 3031 阅读 213

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

docker的部分


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

一:什么是CI/CD

  1. 持续集成(Continuous IntegrationCI):代码合并、构建、部署、测试都在一起,不断地执行这个过程,并对结果反馈。
  2. 持续部署(Continuous DeploymentCD):部署到测试环境、预生产环境、生产环境。
  3. 持续交付(Continuous DeliveryCD):将最终产品发布到生产环境,给用户使用。
  1. CI/CD 流水线特点:
  2. 高效的CI/CD环境可以获得:
  3. 及时发现问题
  4. 大幅度减少故障率
  5. 加快迭代速度
  6. 减少时间成本

二:发布流程设计

image_1e5makhk81nkqgfl14k47mt1ma49.png-114.8kB


image_1e5mal8bo44nt8d94kjuc6tom.png-1329kB

三:部署gitlab 并上传代码

  1. 参照文档:https://blog.51cto.com/flyfish225/2145495
  2. node05.flyfish 上面部署gitlab

image_1e5mcm0sg1429sec1lappvqh0u13.png-395.5kB

image_1e5mcmfjar0g1su6hpshmp1mmn1g.png-323.9kB


  1. Git global setup
  2. git config --global user.name "flyfish"
  3. git config --global user.email "yangyangsirit@163.com"
  4. Create a new repository
  5. git clone git@node05.flyfish:flyfish/java-project.git
  6. cd java-project
  7. touch README.md
  8. git add README.md
  9. git commit -m "add README"
  10. git push -u origin master
  11. Push an existing folder
  12. cd existing_folder
  13. git init
  14. git remote add origin git@node05.flyfish:flyfish/java-project.git
  15. git add .
  16. git commit -m "Initial commit"
  17. git push -u origin master
  18. Push an existing Git repository
  19. cd existing_repo
  20. git remote rename origin old-origin
  21. git remote add origin git@node05.flyfish:flyfish/java-project.git
  22. git push -u origin --all
  23. git push -u origin --tags

image_1e5mctcbj8ibfqg7eeuis2h82d.png-363.5kB

四: 部署jenkins

  1. node06.flyfish 上面配置jenkins
  2. tar -zxvf apache-tomcat-8.5.54.tar.gz
  3. mv apache-tomcat-8.5.54 /usr/local/tomcat
  4. cd /usr/local/tomcat/conf
  5. vim context.xml
  6. ---
  7. tomcat的/conf/context.xml中的<Context>中添加
  8. <!--这里单位是 KB-->
  9. <Resources cachingAllowed="true" cacheMaxSize="100000" />
  10. ---
  11. rm -rf /usr/local/tomcat/webapps/*
  12. mv jenkins.war /usr/local/tomcat/webapps/jenkins.war
  13. cd /usr/local/tomcat/bin/
  14. ./startup.sh

image_1e5me64i8o1a1t2t1jmnd0ak9k2q.png-156.2kB

image_1e5me6n444ps1gvf13vs1v1enee37.png-83.2kB

image_1e5me7hak1os91ft11ohk1pbtjiu43.png-37.5kB

  1. http://192.168.100.16:8080/jenkins

image_1e5me80rsbmm1qd1tgfmp61bme4g.png-294.7kB

image_1e5ofdfkvf90a5fpg31cjqrn99.png-520.6kB


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

image_1e5ofg81qjtl1j0k117i1ikfi05m.png-161.8kB

image_1e5ofhlcb152ihql3ak1hdni5q13.png-102.1kB

  1. 装一个Pipeline

image_1e5ofn7bl1vbc1dr81imm1nj8vnb1t.png-29.3kB

image_1e5ofm3p013ol186ug9f23kqu61g.png-112.5kB

image_1e5og5kl01v0stts8141i381mvk2a.png-95.1kB

image_1e5ogb7prvt13u7l4v1ml9gf52n.png-98.3kB


  1. 创建 一个从节点

image_1e5ogda5pg9l1gbi18qpf4l7834.png-221.2kB

image_1e5ogdrfm7d6hgilii15k315g83h.png-405.9kB

image_1e5oiduqpr133u626k1hre3gv6p.png-317.2kB

image_1e5oifapc1991u3io6n1jfi2oe76.png-139kB


  1. 创建任务

image_1e5oha40fp9o85s15501r1evok55.png-222.1kB

image_1e5ohar5f10531q01o1u1l76uad5i.png-231kB

image_1e5ohodaj8p6175218q61fcoul95v.png-123.5kB

image_1e5ohru00l7614cl1aee1e7upi96c.png-338.6kB


  1. 安装maven
  2. node01.flyfish
  3. tar -zxvf apache-maven-3.5.0-bin.tar.gz
  4. mv apache-maven-3.5.0 /usr/local/maven
  5. vim /etc/profile
  6. ---
  7. export MAVEN_HOME=/usr/local/maven
  8. export CLASSPATH=.:$MAVEN_HOME/jre/lib:$MAVEN_HOME/lib:$MAVEN_HOME/lib/tools.jar
  9. PATH=$PATH:$HOME/bin:$MAVEN_HOME/bin:
  10. ---
  11. 配置maven 下载依赖包为阿里云的源
  12. cd /usr/local/maven/conf
  13. vim settings.xml
  14. 在<mirrors>.....<mirrors> 之间 加上 阿里云的源
  15. -----
  16. <mirror>
  17. <id>alimaven</id>
  18. <name>aliyun maven</name>
  19. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  20. <mirrorOf>central</mirrorOf>
  21. </mirror>
  22. ----
  23. mvn -version

image_1e5oj4talagt10ah26imefuecc.png-74.5kB


  1. 无密码登录下载 代码
  2. cd .ssh/
  3. ssh-copy-id git@192.168.100.15

image_1e5p1ia3q1a0c1p2jgijs3r28p9.png-106.3kB


  1. pipline 流水线语法:
  2. node ("jenkins-slave-100-11") { // 指定Slave标签
  3. // 拉取代码
  4. stage('Git Checkout') {
  5. git credentialsId: '71eb6de5-abea-4416-a3ef-753efcdcaa98', url: 'http://node05.flyfish/flyfish/java-project.git' (这个地方需要用git 流水线生成用户名密码)
  6. }
  7. // 代码编译
  8. stage('Maven Build') {
  9. sh '''
  10. export JAVA_HOME=/usr/local/jdk
  11. /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true
  12. '''
  13. }
  14. // 项目打包到镜像并推送到镜像仓库
  15. stage('Build and Push Image') {
  16. sh '''
  17. REPOSITORY=192.168.100.16/project/java-project:${Branch}
  18. cat > Dockerfile << EOF
  19. FROM 192.168.100.16/library/tomcat:v1
  20. RUN rm -rf /usr/local/tomcat/webapps/ROOT.war
  21. COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
  22. CMD ["catalina.sh", "run"]
  23. EOF
  24. docker build -t $REPOSITORY .
  25. docker login -u flyfish -p Flyfish225 192.168.100.16
  26. docker push $REPOSITORY
  27. '''
  28. }
  29. // 部署到Docker主机
  30. stage('Deploy to Docker') {
  31. sh '''
  32. REPOSITORY=192.168.100.16/project/java-project:${Branch}
  33. docker rm -f java-project |true
  34. docker image rm $REPOSITORY |true
  35. docker login -u flyfish -p Flyfish225 192.168.100.16
  36. docker container run -d --name java-project -p 89:8080 $REPOSITORY
  37. '''
  38. }
  39. }

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

  1. 访问项目

image_1e5s1ef6c1obahdrq51vtd13qv1g.png-4631.7kB


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注