gitlab 的 CI/CD 配置管理 (二)

标签(空格分隔):运维系列


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

一: gitlab CI/CD介绍

1.1 gitlab CI/CD概述

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

官网: https://docs.gitlab.com/ee/ci/README.html

image_1cijbtfpc5ks1o36lgcvn31td39.png-216.3kB

1.2 特性

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

官网:https://docs.gitlab.com/runner/

image_1cijc4gh3k211h0b16pskdnkamm.png-126.9kB

二:配置gitlab的CI/CD 的runner

2.1 安装gitlab-runner

参考: 
https://docs.gitlab.com/runner/install/linux-repository.html

在 CentOS 7 上面的安装
Add GitLab's official repository:

# For RHEL/CentOS/Fedora

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash

image_1cijccgn8g4t7ni12e3132dv7s13.png-713.6kB

安装runner

# For RHEL/CentOS/Fedora
sudo yum install gitlab-runner

image_1cijch4d1ovn1dg810nf59ku2u20.png-438.9kB

image_1cijckt49uq417dpvb6dt5ruk3d.png-453.5kB


2.2 项目注册到到runner

参考官网:

https://docs.gitlab.com/runner/register/index.html

找到要发布项目的注册信息

image_1cijcsl73102ho6v1f8v1lvn1k4n4a.png-185.5kB

image_1cijctoio1o0t1gumbga1ehcmg04n.png-416.1kB

image_1cijd0akv1anbrb1qev1iqb74h54.png-619.9kB

image_1cijd11pt1489g011rik1e11pdv5h.png-464.8kB

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

image_1cijd53e1qoq15v81kp613kb1ghv5u.png-371.6kB


service gitlab-runner start 
chkconfig gitlab-runner on

在gitlab 上面找到runner 将项目添加到runner里面去

image_1cijd803a119g1mdhmp08t4gca6b.png-418.6kB

image_1cijd9oav1b1cguc686tpqp1r78.png-488.5kB

1.png-229.2kB

image_1cijdb1521lueqo21ghq1d6a81q7l.png-353kB

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

##三:代码的MAVEN打包环境

配置 maven

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

mv apache-maven-3.5.3 /usr/local/maven

vim /etc/profile

---
在Java环境变量下面加上 maven的环境变量

## maven 

export MAVEN_HOME=/usr/local/maven
PATH=$PATH:$HOME/bin:$MAVEN_HOME/bin

---

image_1cijee19c1fbb4u5146312pp1llu9i.png-184.9kB

source /etc/profile
mvn -version

image_1cijeercbbi68b51111latos69v.png-272.7kB

mkdir -p /deploy/solo # 创建 打出solo.war 的目录

cd /root/project/

rm -rf solo 

git clone http://172.17.100.11/TL-flyfish/solo.git

尝试使用 命令行的方式编译项目:
首次编译会下载包会很慢

cd solo

mvn clean package
mvn install
mvn install package -Dmaven.test.skip=true

最后会打出solo.war包

表示命令行打包完成

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包测试

stages:
   - build
   - run
   - deploy

variables:
    MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
  paths:
    - .m2/repository/

.solo-build-job: &solo-build-job
  script:
    - mvn clean package
    - mvn install
    - mvn install package -Dmaven.test.skip=true
    - ls -ld *

solo-build:
   when: manual
   stage: build
   script:
      - mvn clean package
      - mvn install 
      - mvn install package -Dmaven.test.skip=true
      - cd $CI_PROJECT_DIR/target/
      - ls -ld *

image_1ckca8eub10t6f781fv71tjg1vqo16.png-405.3kB

执行构建

image_1ckcaa4es1b62nh4unv1bbnur61j.png-506.4kB

image_1ckcab7jk1mba1qsho4c17n1hli2g.png-256.8kB

image_1ckcabqtd1a2p8gj13r161phh72t.png-278.2kB

image_1ckcacihp1havid711ge1i5k1o573a.png-467.2kB

最后打出solo.war的包

image_1ckcakf6g1rmu189uvirmbm1lrm47.png-537.8kB


五: 发布项目

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

tomcat 服务器 IP: 172.17.100.12 

打通172.17.100.11 和172.17.100.12 的无密码登录认证。

下载:tomcat
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.32/bin/apache-tomcat-8.5.32.tar.gz

 tar -zxvf apache-tomcat-8.5.32.tar.gz

 mv apache-tomcat-8.5.32 /usr/local/tomcat

image_1ckcb2r6l19391nqfcrk0a85t53.png-565.7kB

image_1ckcb4a6p1ha55qkumb14gi5vi5m.png-167.9kB

启动tomcat:

cd /usr/local/tomcat/bin

./startup.sh

image_1ckcb5l721thuhicg371qcohrl63.png-649.8kB

访问: 172.17.100.12:8080

image_1ckcb8kpuas6plh14g71ha11tk77s.png-636.2kB

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

stages:
   - build
   - run
   - deploy

variables:
    MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
  paths:
    - .m2/repository/

.solo-build-job: &solo-build-job
  script:
    - mvn clean package
    - mvn install
    - mvn install package -Dmaven.test.skip=true
    - ls -ld *

solo-build:
   when: manual
   stage: build
   script:
      - sudo rm -rf /deploy/solo/solo.war
      - mvn clean package
      - mvn install 
      - mvn install package -Dmaven.test.skip=true
      - cd $CI_PROJECT_DIR/target/
      - ls -ld *
      - cp -ap solo.war /deploy/solo/

solo-run:
  when: manual
  stage: run
  script:
     - cd /deploy/solo
     - md5sum solo.war

solo-deploy:
   when: manual
   stage: deploy
   script:
      - cd /deploy/solo/
      - scp solo.war root@172.17.100.12:/root  
      - cd /deploy/script
      - sh -x deploy-solo.sh 

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

发布需要配置 一个发布脚本:

在 172.17.100.11 上面写编写脚本:

mkdir /deploy/script -p

---

#!/bin/bash
DATE=`date +%Y-%m-%d-%H-%M`

ssh root@172.17.100.12 << eof

cd /usr/local/tomcat/bin/

sh -x shutdown.sh

cd /usr/local/tomcat/webapps/

mv solo.war  /data/backup/solo.war$DATE

cp /root/solo.war /usr/local/tomcat/webapps/

cd /usr/local/tomcat/bin/

sh -x startup.sh 

ps -ef |grep tomcat 

eof
---

在 tomcat 服务器上面新建一个solo.war 包的备份目录
172.17.100.12

mkdir -p /data/backup

image_1ckcclbga1jbk1b81upl1dlg1p5rb9.png-119.5kB


客户端从git服务器(172.17.100.11) 下载代码

cd /soft
rm -rf * 

git clone git@172.17.100.11:TL-flyfish/solo-project.git

cd solo-project
cd src/main/resources/

vim latke.properties

---
修改 这个 serverHost 改为 172.17.100.12

serverHost=172.17.100.12
---

image_1ckcd617u1nf1bu518dg1r9h1gvmc3.png-652.2kB

image_1ckcdan6318qnqtpkms1o1baq5e2.png-342.1kB

提交项目: 

cd /soft/solo-project
  git add *
  git commit -m "add solo"
  git push -u origin master

image_1ckcdgshm44jpvt1oa41hi0cv7ei.png-502kB

发布项目:

2.png-318.7kB

点击执行

3.png-182.5kB

开始build

5.png-524.4kB

6.png-375.5kB

此处报一个 权限不够
是因为默认构建的时候是gitlab-runner 这个用户 去执行这个命令的
所以要为gitlab-runner 用户提权

chmod +x /etc/sudoers

vim /etc/sudoers 

gitlab-runner   ALL=(ALL)       NOPASSWD:ALL

并要 做gitlab-runner 用户与 172.17.100.12 的root的用户的无密钥登录

7.png-422.5kB

gitlab-cli.yaml 构建 脚本应该为sudo 

stages:
   - build
   - run
   - deploy

variables:
    MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
  paths:
    - .m2/repository/

.solo-build-job: &solo-build-job
  script:
    - mvn clean package
    - mvn install
    - mvn install package -Dmaven.test.skip=true
    - ls -ld *

solo-build:
   when: manual
   stage: build
   script:
      - sudo rm -rf /deploy/solo/solo.war
      - mvn clean package
      - mvn install 
      - mvn install package -Dmaven.test.skip=true
      - cd $CI_PROJECT_DIR/target/
      - ls -ld *
      - sudo cp -ap solo.war /deploy/solo/

solo-run:
  when: manual
  stage: run
  script:
     - cd /deploy/solo
     - md5sum solo.war

solo-deploy:
   when: manual
   stage: deploy
   script:
      - cd /deploy/solo/
      - sudo scp solo.war root@172.17.100.12:/root  
      - cd /deploy/script
      - sudo sh -x deploy-solo.sh

从新执行build的构建

8.png-305.3kB

10.png-196.6kB

11.png-482.4kB

12.png-399.7kB

执行 步骤 run:

13.png-412.9kB

14.png-258.5kB

执行步骤:deploy

15.png-278.4kB

16.png-155.5kB

image_1cke4tepcllffi3al31t9a9hk9.png-673.1kB

访问测试:

image_1cke4uar41gka1hhk1niaf617gmm.png-209.2kB