Gitlab CICD For Springboot

本贴最后更新于 2028 天前,其中的信息可能已经天翻地覆

环境

CentOS 7 (gitlab-runner 的服务器上事先安装 maven,sshpass)

gitlab-runner

官方安装文档:https://docs.gitlab.com/runner/install/linux-manually.html
官方注册文档:https://docs.gitlab.com/runner/register/index.html

.gitlab-ci.yml

#全局变量定义
variables:
  DEV_NAME: dev  		//开发服务器的用户名称
  DEV_HOST:  0.0.0.0  	//开发服务器地址
  CODE_PATH: ./Develop/SourceCode/Code/	代码路径
  MAVEN_CLI_OPTS_develop: "/mvn_config/mvn-settings.xml --batch-mode"  //编译时选择的maven路径

#阶段顺序
stages:
- before_all
- build
- deploy
- clean

#每一个阶段前面
before_script:
- ':'

#所有步骤之前
before_all:
  stage: before_all
  script:
  - ': "================      编译环境检测      ================="'
  dependencies: []
  only:
    refs:
    - develop
    - master
    - test

#发布、编译
mvn package:
  stage: build
  variables:
    MAVEN_CLI_OPTS: $MAVEN_CLI_OPTS_develop
  script:
  - cd ${CODE_PATH}
  - TEMP_PATH=`pwd`
  - mkdir -p final_target
  - rm -f ./final_target/*.zip
  - mvn -s ${TEMP_PATH}${MAVEN_CLI_OPTS} package deploy -B -e -U -T 1C -Dmaven.test.skip=true -Dmaven.compile.fork=true
  - find ./ -name *.zip | grep -v final_target | xargs -i mv {} ./final_target/
  - find ./ -name *.zip
  artifacts:
    name: "$CI_COMMIT_REF_NAME"
    paths:
    - ${CODE_PATH}final_target
  only:
    refs:
    - develop
    changes:
    - Develop/SourceCode/Code/**/*.{java,xml,properties,sh}

#部署,运行(开发)
deploy-agri:
  stage: deploy
  variables:
    PASSWD: $DEV_PASSWD		//开发环境密码设置在runner变量下
    NAME: $DEV_NAME
    HOST: $DEV_HOST
  dependencies:
  - mvn package
  script:
  - ': "==============        开发环境检测       ================="'
  - find ${CODE_PATH} -name *.zip

  - ': "================          备份          ================="'
  - ': "================    停止应用,删除旧包     ================="'
  - ': "================        开始执行         ================="'
  - sshpass -p ${PASSWD} ssh -o StrictHostKeychecking=no ${NAME}@${HOST} "mkdir -p ./Releases/"
  - sshpass -p ${PASSWD} ssh -o StrictHostKeychecking=no ${NAME}@${HOST} "rm -f  ./Releases/*.zip"
  - sshpass -p ${PASSWD} scp -o StrictHostKeychecking=no ${CODE_PATH}final_target/springboot.zip ${NAME}@${HOST}:./Releases/
  - sshpass -p ${PASSWD} ssh -o StrictHostKeychecking=no ${NAME}@${HOST} "python3 ./Releases/deploy.py"		//备份,停止,增量,启动脚本写在deploy.py中
  - ': "=============         检测是否成功启动        =============="'
  - sleep 30
  - sshpass -p ${PASSWD} ssh -o StrictHostKeychecking=no ${NAME}@${HOST} "jps | grep -v Jps "
  only:
    refs:
    - develop
    changes:
    - Develop/SourceCode/Code/**/*.{java,xml,properties,sh}

after all:
  stage: clean
  script:
  - ': "======================    END    ====================="'
  - ': "===================     结果反馈?     =================="'
  dependencies: []
  only:
    refs:
    - develop
    - master
    - test
  • GitLab

    GitLab 是利用 Ruby 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面操作公开或私有项目。

    46 引用 • 72 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...