gitlab runner的安装和基本使用:https://bear-coding.blog.csdn.net/article/details/120591711
安装并给项目配置完gitlab runner后再操作后面步骤。
实现目标:
master分支代码有变更的时候自动构建build。
当开发人员在gitlab上给项目打一个tag标签分支的时候自动触发流水线,根据标签名称和分支名称来自动发布;
例如分支名称为"test_3.1.1.1_project1&project2"则发布到测试环境
例如分支名称为"prod_3.1.1.1_project1&project2"则发布到生产环境
在项目根目录下创建以下文件:
创建好之后,提交到gitlab上,就直接可以使用了
.gitlab-ci.yml 流水线文件
stages:
- build
- test
- prod
#当master分支变更的时候触发
build:
stage: build
script: sh ./build.sh
only:
- master #master主干有变更的发起编译任务
tags:
- trade_runner #任务标签,根据此标签匹配gitlab runner的标签。
test:
stage: test
script: sh ./test.sh
only:
refs:
- tags #创建tag分支且格式为”test_44.55.66.77_project1.project2“的时候发起发布任务
variables:
- $CI_COMMIT_TAG =~ /^test_[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+_[a-zA-Z0-9&\.]+$/
tags:
- trade_runner
prod:
stage: prod
script: sh ./prod.sh
only:
refs:
- tags #创建tag分支且格式为”prod_44.55.66.77_project1.project2“的时候发起发布任务
variables:
- $CI_COMMIT_TAG =~ /^prod_[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+_[a-zA-Z0-9&\.]+$/
tags:
- trade_runner
when: manual #设置需要手动触发
build.sh 执行编译项目的脚本文件
#!/bin/sh
echo "start build task,job_name=$CI_JOB_NAME"
#判断流水线任务必须是build
if [ "$CI_JOB_NAME" != "build" ]; then
echo "error job name: $CI_JOB_NAME"
exit 1
fi
cd GitLabTest/
dotnet build $build_args
#获取构建结果
build_result=$?
echo "end build task,$build_result"
exit $build_result
test.sh 执行发布测试环境的脚本
#!/bin/sh
thetime=`date +%Y%m%d%H%M`
tag_msg=`echo "$CI_COMMIT_TAG" | awk -F '_' '{print $3}' `
echo "start test task,tag=$CI_COMMIT_TAG,job_name=$CI_JOB_NAME,tag_message=$tag_msg,time=$thetime"
#判断流水线任务必须是deploy
if [ "$CI_JOB_NAME" != "test" ]; then
echo "error job name: $CI_JOB_NAME"
exit 1
fi
#要发布的项目列表
programs=(${tag_msg//&/ })
for program in ${programs[@]}
do
#切换到要发布的项目里,并发布项目
cd GitLabTest/$program
echo "start publish $program"
dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true $build_args -p:DebugSymbols=true -p:DebugType="portable"
if [ $? != 0 ]; then
echo "publish $program failed"
exit 1
fi
echo "publish $program success"
cd ../../
done
base_pwd=`pwd`
build_path_middle="publish"
for program in ${programs[@]}
do
#将发布好的文件复制到发布目录下
#也可以将文件打包成tar,传输到远程服务器上,或者生成docker镜像,推送到镜像服务器。见后面注释部分
echo "start copy $program"
#预先创建存放发布文件的目录,并将权限给到gitlab-runner
#mkdir /usr/local/publish
#chown -R gitlab-runner:gitlab-runner /usr/local/publish
mkdir -p /usr/local/$build_path_middle/$program/
cp -rf $base_pwd/GitLabTest/$program/bin/Release/net7.0/linux-x64/* /usr/local/$build_path_middle/$program/
echo "copy $program success"
done
#将文件打包成tar,传输到远程服务器上,
# rm -f program.tar.gz
# tar zcf program.tar.gz -C publish --exclude "*.tar.gz" .
# ssh -p $server_port $server_ip "mkdir -p $deploy_base_pwd/$program/"
# ssh -p $server_port $server_ip "{ cd $deploy_base_pwd/$program/ && [ -s program.tar.gz ] && mv -f program.tar.gz program_bak.tar.gz; } || true"
# scp -P $server_port program.tar.gz $server_ip:$deploy_base_pwd/$program/
#将文件生成docker镜像,并推送到镜像仓库
#docker build -f /usr/local/$build_path_middle/$program/Dockerfile -t 镜像仓库地址/trade-$program:${thetime} .
#docker push 镜像仓库地址/trade-$program:${thetime}
#docker rmi 镜像仓库地址/trade-$program:${thetime}
prod.sh 执行发布到生产环境的脚本,与test.sh基本一至,根据实际需要更改
#!/bin/sh
thetime=`date +%Y%m%d%H%M`
tag_msg=`echo "$CI_COMMIT_TAG" | awk -F '_' '{print $3}' `
echo "start product task,tag=$CI_COMMIT_TAG,job_name=$CI_JOB_NAME,tag_message=$tag_msg,time=$thetime"
#判断流水线任务必须是deploy
if [ "$CI_JOB_NAME" != "prod" ]; then
echo "error job name: $CI_JOB_NAME"
exit 1
fi
#要发布的项目列表
programs=(${tag_msg//&/ })
for program in ${programs[@]}
do
#切换到要发布的项目里,并发布项目
cd GitLabTest/$program
echo "start publish $program"
dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true $build_args -p:DebugSymbols=true -p:DebugType="portable"
if [ $? != 0 ]; then
echo "publish $program failed"
exit 1
fi
echo "publish $program success"
cd ../../
done
base_pwd=`pwd`
build_path_middle="publish"
for program in ${programs[@]}
do
#将发布好的文件复制到发布目录下
#也可以将文件打包成tar,传输到远程服务器上,或者生成docker镜像,推送到镜像服务器。见后面注释部分
echo "start copy $program"
#预先创建存放发布文件的目录,并将权限给到gitlab-runner
#mkdir /usr/local/publish
#chown -R gitlab-runner:gitlab-runner /usr/local/publish
mkdir -p /usr/local/$build_path_middle/$program/
cp -rf $base_pwd/GitLabTest/$program/bin/Release/net7.0/linux-x64/* /usr/local/$build_path_middle/$program/
echo "copy $program success"
done
#将文件打包成tar,传输到远程服务器上,
# rm -f program.tar.gz
# tar zcf program.tar.gz -C publish --exclude "*.tar.gz" .
# ssh -p $server_port $server_ip "mkdir -p $deploy_base_pwd/$program/"
# ssh -p $server_port $server_ip "{ cd $deploy_base_pwd/$program/ && [ -s program.tar.gz ] && mv -f program.tar.gz program_bak.tar.gz; } || true"
# scp -P $server_port program.tar.gz $server_ip:$deploy_base_pwd/$program/
#将文件生成docker镜像,并推送到镜像仓库
#docker build -f /usr/local/$build_path_middle/$program/Dockerfile -t 镜像仓库地址/trade-$program:${thetime} .
#docker push 镜像仓库地址/trade-$program:${thetime}
#docker rmi 镜像仓库地址/trade-$program:${thetime}