本文共 5640 字,大约阅读时间需要 18 分钟。
标签(空格分隔):运维系列
- 一:gitlab CI/CD 介绍
- 二:配置gitlab的CI/CD 的runner
- 三:代码的MAVEN打包环境
- 四:配置gitlab的CI 文件
- 五:发布项目
一: gitlab CI/CD介绍
Gitlab是常用的开源git代码管理工具之一,随着发展也推出了ci/cd解决方案.顾名思义具体来说ci/cd主要完成以下两个工作. ci(持续构建) 代码提交后触发自动化的单元测试,代码预编译,构建镜像,上传镜像等. cd(持续发布) 持续发布则指将构建好的程序发布到各种环境,如预发布环境,正式环境.官网: https://docs.gitlab.com/ee/ci/README.html
gitlab ci/cd是由独立的runner程序完成,runner采用go语言编写,因此可以很好的进行跨平台,通常可以将runner部署到任何gitlab server之外的服务器,从而避免对gitlab server的影响.官网:https://docs.gitlab.com/runner/
参考: https://docs.gitlab.com/runner/install/linux-repository.html在 CentOS 7 上面的安装
Add GitLab's official repository:# For RHEL/CentOS/Fedoracurl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
安装runner# For RHEL/CentOS/Fedorasudo yum install gitlab-runner
参考官网:https://docs.gitlab.com/runner/register/index.html找到要发布项目的注册信息
Setup a specific Runner manually安装一个与 GitLab CI 兼容的 Runner (如需了解更多的安装信息,请查看 GitLab Runner)在 Runner 设置时指定以下 URL: http://172.17.100.11/在安装过程中使用以下注册令牌: kA5JA5myz4aZR6MRgEBm启动 Runner!
gitlab-runner register
service gitlab-runner start chkconfig gitlab-runner on在gitlab 上面找到runner 将项目添加到runner里面去
这边如果有多个项目都可以添加到这个runner 里面去
##三:代码的MAVEN打包环境
配置 maventar -zxvf apache-maven-3.5.3-bin.tar.gzmv apache-maven-3.5.3 /usr/local/mavenvim /etc/profile---在Java环境变量下面加上 maven的环境变量## maven export MAVEN_HOME=/usr/local/mavenPATH=$PATH:$HOME/bin:$MAVEN_HOME/bin---
source /etc/profilemvn -version
mkdir -p /deploy/solo # 创建 打出solo.war 的目录cd /root/project/rm -rf solo git clone http://172.17.100.11/TL-flyfish/solo.git尝试使用 命令行的方式编译项目:首次编译会下载包会很慢cd solomvn clean packagemvn installmvn install package -Dmaven.test.skip=true最后会打出solo.war包表示命令行打包完成maven 到默认下载的依赖包在/root/.m2/repository
stages: - build - run - deployvariables: 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 *
执行构建
最后打出solo.war的包
tomcat 服务器 IP: 172.17.100.12 打通172.17.100.11 和172.17.100.12 的无密码登录认证。下载:tomcatwget 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
启动tomcat:cd /usr/local/tomcat/bin./startup.sh
访问: 172.17.100.12:8080
stages: - build - run - deployvariables: 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.warsolo-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
发布需要配置 一个发布脚本:在 172.17.100.11 上面写编写脚本:mkdir /deploy/script -p---#!/bin/bashDATE=`date +%Y-%m-%d-%H-%M`ssh root@172.17.100.12 << eofcd /usr/local/tomcat/bin/sh -x shutdown.shcd /usr/local/tomcat/webapps/mv solo.war /data/backup/solo.war$DATEcp /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.12mkdir -p /data/backup
客户端从git服务器(172.17.100.11) 下载代码cd /softrm -rf * git clone git@172.17.100.11:TL-flyfish/solo-project.gitcd solo-projectcd src/main/resources/vim latke.properties---修改 这个 serverHost 改为 172.17.100.12serverHost=172.17.100.12---
提交项目: cd /soft/solo-project git add * git commit -m "add solo" git push -u origin master
发布项目:
点击执行
开始build
此处报一个 权限不够是因为默认构建的时候是gitlab-runner 这个用户 去执行这个命令的所以要为gitlab-runner 用户提权chmod +x /etc/sudoersvim /etc/sudoers gitlab-runner ALL=(ALL) NOPASSWD:ALL并要 做gitlab-runner 用户与 172.17.100.12 的root的用户的无密钥登录
gitlab-cli.yaml 构建 脚本应该为sudo stages: - build - run - deployvariables: 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.warsolo-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的构建
执行 步骤 run:
执行步骤:deploy
访问测试:
转载于:https://blog.51cto.com/flyfish225/2156602