[关闭]
@zhangyy 2019-12-11T10:44:35.000000Z 字数 4765 阅读 106

gitlab 的 CI/CD 配置管理

运维系列


  • 一:gitlab CI/CD 介绍
  • 二:配置gitlab的CI/CD 的runner
  • 三:代码的MAVEN打包环境
  • 四:配置gitlab的CI 文件
  • 五:发布项目

一: gitlab CI/CD介绍

1.1 gitlab CI/CD概述

  1. Gitlab是常用的开源git代码管理工具之一,随着发展也推出了ci/cd解决方案.
  2. 顾名思义具体来说ci/cd主要完成以下两个工作.
  3. ci(持续构建)
  4. 代码提交后触发自动化的单元测试,代码预编译,构建镜像,上传镜像等.
  5. cd(持续发布)
  6. 持续发布则指将构建好的程序发布到各种环境,如预发布环境,正式环境.
  7. 官网: https://docs.gitlab.com/ee/ci/README.html

image_1cijbtfpc5ks1o36lgcvn31td39.png-216.3kB

1.2 特性

  1. gitlab ci/cd是由独立的runner程序完成,runner采用go语言编写,因此可以很好的进行跨平台,通常可以将runner部署到任何gitlab server之外的服务器,从而避免对gitlab server的影响.
  2. 官网:https://docs.gitlab.com/runner/

image_1cijc4gh3k211h0b16pskdnkamm.png-126.9kB

二:配置gitlab的CI/CD 的runner

2.1 安装gitlab-runner

  1. 参考:
  2. https://docs.gitlab.com/runner/install/linux-repository.html
  3. CentOS 7 上面的安装
  1. Add GitLab's official repository:
  2. # For RHEL/CentOS/Fedora
  3. curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash

image_1cijccgn8g4t7ni12e3132dv7s13.png-713.6kB

  1. 安装runner
  2. # For RHEL/CentOS/Fedora
  3. sudo yum install gitlab-runner

image_1cijch4d1ovn1dg810nf59ku2u20.png-438.9kB

image_1cijckt49uq417dpvb6dt5ruk3d.png-453.5kB


2.2 项目注册到到runner

  1. 参考官网:
  2. https://docs.gitlab.com/runner/register/index.html
  3. 找到要发布项目的注册信息

image_1cijcsl73102ho6v1f8v1lvn1k4n4a.png-185.5kB

image_1cijctoio1o0t1gumbga1ehcmg04n.png-416.1kB

image_1cijd0akv1anbrb1qev1iqb74h54.png-619.9kB

image_1cijd11pt1489g011rik1e11pdv5h.png-464.8kB

  1. Setup a specific Runner manually
  2. 安装一个与 GitLab CI 兼容的 Runner (如需了解更多的安装信息,请查看 GitLab Runner)
  3. Runner 设置时指定以下 URL http://172.17.100.11/
  4. 在安装过程中使用以下注册令牌: kA5JA5myz4aZR6MRgEBm
  5. 启动 Runner!
  1. gitlab-runner register

image_1cijd53e1qoq15v81kp613kb1ghv5u.png-371.6kB

  1. service gitlab-runner start
  2. chkconfig gitlab-runner on
  3. gitlab 上面找到runner 将项目添加到runner里面去

image_1cijd803a119g1mdhmp08t4gca6b.png-418.6kB

image_1cijd9oav1b1cguc686tpqp1r78.png-488.5kB

1.png-229.2kB

image_1cijdb1521lueqo21ghq1d6a81q7l.png-353kB

  1. 这边如果有多个项目都可以添加到这个runner 里面去

三:代码的MAVEN打包环境

  1. 配置 maven
  2. tar -zxvf apache-maven-3.5.3-bin.tar.gz
  3. mv apache-maven-3.5.3 /usr/local/maven
  4. vim /etc/profile
  5. ---
  6. Java环境变量下面加上 maven的环境变量
  7. ## maven
  8. export MAVEN_HOME=/usr/local/maven
  9. PATH=$PATH:$HOME/bin:$MAVEN_HOME/bin
  10. ---

image_1cijee19c1fbb4u5146312pp1llu9i.png-184.9kB

  1. source /etc/profile
  2. mvn -version

image_1cijeercbbi68b51111latos69v.png-272.7kB

  1. mkdir -p /deploy/solo # 创建 打出solo.war 的目录
  2. cd /root/project/
  3. rm -rf solo
  4. git clone http://172.17.100.11/TL-flyfish/solo.git
  5. 尝试使用 命令行的方式编译项目:
  6. 首次编译会下载包会很慢
  7. cd solo
  8. mvn clean package
  9. mvn install
  10. mvn install package -Dmaven.test.skip=true
  11. 最后会打出solo.war
  12. 表示命令行打包完成
  13. maven 到默认下载的依赖包在/root/.m2/repository

image_1cijjapm61vh0rpu1ke1gsv12gu9.png-621kB

四:配置gitlab的CI 文件

image_1cijjj563smdqoo1cv51us1eg6m.png-476.7kB

image_1cijjkf6r1hjjcro9191je61nrj13.png-251.6kB

4.1 配置CI 的 运行文件gitlab-ci.yml 打出war包测试

  1. stages:
  2. - build
  3. - run
  4. - deploy
  5. variables:
  6. MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  7. MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  8. cache:
  9. paths:
  10. - .m2/repository/
  11. .solo-build-job: &solo-build-job
  12. script:
  13. - mvn clean package
  14. - mvn install
  15. - mvn install package -Dmaven.test.skip=true
  16. - ls -ld *
  17. solo-build:
  18. when: manual
  19. stage: build
  20. script:
  21. - mvn clean package
  22. - mvn install
  23. - mvn install package -Dmaven.test.skip=true
  24. - cd $CI_PROJECT_DIR/target/
  25. - ls -ld *

image_1ckca8eub10t6f781fv71tjg1vqo16.png-405.3kB

  1. 执行构建

image_1ckcaa4es1b62nh4unv1bbnur61j.png-506.4kB

image_1ckcab7jk1mba1qsho4c17n1hli2g.png-256.8kB

image_1ckcabqtd1a2p8gj13r161phh72t.png-278.2kB

image_1ckcacihp1havid711ge1i5k1o573a.png-467.2kB

  1. 最后打出solo.war的包

image_1ckcakf6g1rmu189uvirmbm1lrm47.png-537.8kB


五: 发布项目

5.1: 部署一台tomcat 中间件的服务器

  1. tomcat 服务器 IP: 172.17.100.12
  2. 打通172.17.100.11 172.17.100.12 的无密码登录认证。
  3. 下载:tomcat
  4. wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.32/bin/apache-tomcat-8.5.32.tar.gz
  5. tar -zxvf apache-tomcat-8.5.32.tar.gz
  6. mv apache-tomcat-8.5.32 /usr/local/tomcat

image_1ckcb2r6l19391nqfcrk0a85t53.png-565.7kB

image_1ckcb4a6p1ha55qkumb14gi5vi5m.png-167.9kB

  1. 启动tomcat
  2. cd /usr/local/tomcat/bin
  3. ./startup.sh

image_1ckcb5l721thuhicg371qcohrl63.png-649.8kB

  1. 访问: 172.17.100.12:8080

image_1ckcb8kpuas6plh14g71ha11tk77s.png-636.2kB

5.2 在gitlab-ci.yml 增加发布步骤

  1. stages:
  2. - build
  3. - run
  4. - deploy
  5. variables:
  6. MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  7. MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  8. cache:
  9. paths:
  10. - .m2/repository/
  11. .solo-build-job: &solo-build-job
  12. script:
  13. - mvn clean package
  14. - mvn install
  15. - mvn install package -Dmaven.test.skip=true
  16. - ls -ld *
  17. solo-build:
  18. when: manual
  19. stage: build
  20. script:
  21. - sudo rm -rf /deploy/solo/solo.war
  22. - mvn clean package
  23. - mvn install
  24. - mvn install package -Dmaven.test.skip=true
  25. - cd $CI_PROJECT_DIR/target/
  26. - ls -ld *
  27. - cp -ap solo.war /deploy/solo/
  28. solo-run:
  29. when: manual
  30. stage: run
  31. script:
  32. - cd /deploy/solo
  33. - md5sum solo.war
  34. solo-deploy:
  35. when: manual
  36. stage: deploy
  37. script:
  38. - cd /deploy/solo/
  39. - scp solo.war root@172.17.100.12:/root
  40. - cd /deploy/script
  41. - sh -x deploy-solo.sh

image_1ckccqufpc961kb61qqp1buo9ftbm.png-328.5kB
1.png-370.7kB

  1. 发布需要配置 一个发布脚本:
  2. 172.17.100.11 上面写编写脚本:
  3. mkdir /deploy/script -p
  4. ---
  5. #!/bin/bash
  6. DATE=`date +%Y-%m-%d-%H-%M`
  7. ssh root@172.17.100.12 << eof
  8. cd /usr/local/tomcat/bin/
  9. sh -x shutdown.sh
  10. cd /usr/local/tomcat/webapps/
  11. mv solo.war /data/backup/solo.war$DATE
  12. cp /root/solo.war /usr/local/tomcat/webapps/
  13. cd /usr/local/tomcat/bin/
  14. sh -x startup.sh
  15. ps -ef |grep tomcat
  16. eof
  17. ---

  1. tomcat 服务器上面新建一个solo.war 包的备份目录
  2. 172.17.100.12
  3. mkdir -p /data/backup

image_1ckcclbga1jbk1b81upl1dlg1p5rb9.png-119.5kB


  1. 客户端从git服务器(172.17.100.11 下载代码
  2. cd /soft
  3. rm -rf *
  4. git clone git@172.17.100.11:TL-flyfish/solo-project.git
  5. cd solo-project
  6. cd src/main/resources/
  7. vim latke.properties
  8. ---
  9. 修改 这个 serverHost 改为 172.17.100.12
  10. serverHost=172.17.100.12
  11. ---

image_1ckcd617u1nf1bu518dg1r9h1gvmc3.png-652.2kB

image_1ckcdan6318qnqtpkms1o1baq5e2.png-342.1kB

  1. 提交项目:
  2. cd /soft/solo-project
  3. git add *
  4. git commit -m "add solo"
  5. git push -u origin master

image_1ckcdgshm44jpvt1oa41hi0cv7ei.png-502kB

  1. 发布项目:

2.png-318.7kB

  1. 点击执行

3.png-182.5kB

  1. 开始build

5.png-524.4kB

6.png-375.5kB

  1. 此处报一个 权限不够
  2. 是因为默认构建的时候是gitlab-runner 这个用户 去执行这个命令的
  3. 所以要为gitlab-runner 用户提权
  4. chmod +x /etc/sudoers
  5. vim /etc/sudoers
  6. gitlab-runner ALL=(ALL) NOPASSWD:ALL
  7. 并要 gitlab-runner 用户与 172.17.100.12 root的用户的无密钥登录

7.png-422.5kB

  1. gitlab-cli.yaml 构建 脚本应该为sudo
  2. stages:
  3. - build
  4. - run
  5. - deploy
  6. variables:
  7. MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  8. MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  9. cache:
  10. paths:
  11. - .m2/repository/
  12. .solo-build-job: &solo-build-job
  13. script:
  14. - mvn clean package
  15. - mvn install
  16. - mvn install package -Dmaven.test.skip=true
  17. - ls -ld *
  18. solo-build:
  19. when: manual
  20. stage: build
  21. script:
  22. - sudo rm -rf /deploy/solo/solo.war
  23. - mvn clean package
  24. - mvn install
  25. - mvn install package -Dmaven.test.skip=true
  26. - cd $CI_PROJECT_DIR/target/
  27. - ls -ld *
  28. - sudo cp -ap solo.war /deploy/solo/
  29. solo-run:
  30. when: manual
  31. stage: run
  32. script:
  33. - cd /deploy/solo
  34. - md5sum solo.war
  35. solo-deploy:
  36. when: manual
  37. stage: deploy
  38. script:
  39. - cd /deploy/solo/
  40. - sudo scp solo.war root@172.17.100.12:/root
  41. - cd /deploy/script
  42. - sudo sh -x deploy-solo.sh

  1. 从新执行build的构建

8.png-305.3kB

10.png-196.6kB

11.png-482.4kB

12.png-399.7kB

  1. 执行 步骤 run:

13.png-412.9kB

14.png-258.5kB

  1. 执行步骤:deploy

15.png-278.4kB

16.png-155.5kB

image_1cke4tepcllffi3al31t9a9hk9.png-673.1kB

  1. 访问测试:

image_1cke4uar41gka1hhk1niaf617gmm.png-209.2kB

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