话不多说,直接上提交信息的文本案例和shell脚本代码,要前面详细的步骤可以参考我的另一篇文章:《关于coding平台上如何使用持续集成部署前端项目 》
Git文本案例:
test:测试变更[BP=test][BF=test][PF=project_web/test]
shell-脚本-1
# 获取最近一次提交的完整消息 CommitMessage=$(git log -1 --pretty=%B) || { echo "获取提交消息失败"; exit 1; } # 从提交消息中提取 BP:buildProject 的值 BPValue=$(echo "$CommitMessage" | grep -oE '\[BP=[^]]*\]' | sed 's/^\[BP=//;s/\]$//') # 如果提取失败,BPValue 赋值为空 if [ -z "$BPValue" ]; then echo "未找到 BP:buildProject 值,BPValue 赋值为空" BPValue="" else echo "成功提取 BP:buildProject 值: $BPValue" fi # 设置 BuildParams 三目判断:BPValue和PROJECT BuildParams=${BPValue:-$PROJECT} echo "BuildParams 设置为: $BuildParams" # 获取 NPM 配置 echo "获取 NPM 配置" npm config list # 在没有 node_modules 文件夹下,如果有新依赖,需要注释本行重新跑 npm if ! [ -d "./node_modules" ]; then echo "未检测到 node_modules 目录,开始安装依赖" # 更改淘宝镜像源,网络好不需要则可去除 npm config set registry https://registry.npmmirror.com || { echo "设置 NPM 镜像源失败"; exit 1; } npm install --legacy-peer-deps --loglevel verbose || { echo "安装依赖失败"; exit 1; } else echo "已检测到 node_modules 目录,跳过安装依赖" fi # 执行 build 构建 echo "开始执行构建" npm run build "$BuildParams" || { echo "构建失败"; exit 1; } # 列出当前目录下的文件 echo "列出当前目录下的文件" ls
shell-脚本-2
# 如果test存在,删除test文件夹,重新git clone if [ -d "test" ]; then rm -rf test fi
shell-脚本-3
# 执行git命令拉取指定分支环境变量-BRANCH git clone --depth 5 -b ${BRANCH} https://${USERNAME}:${PASSWORD}@e.coding.test.com/test/test/test.git
shell-脚本-4
# 获取最近一次提交的完整消息 CommitMessage=$(git log -1 --pretty=%B) || { echo "获取提交消息失败"; exit 1; } echo "CommitMessage 值为:$CommitMessage" # 从提交消息中提取 BP:打包的项目 的值 BPValue=$(echo "$CommitMessage" | grep -oE '\[BP=[^]]*\]' | sed 's/^\[BP=//;s/\]$//') # 如果提取失败,BPValue 赋值为空 if [ -z "$BPValue" ]; then echo "未找到 BP:buildProject 值,BPValue 赋值为空" BPValue="" else echo "成功提取 BP:buildProject 值: $BPValue" fi # 设置 BuildParams 三目判断:BPValue和PROJECT BuildParams=${BPValue:-$PROJECT} echo "BuildParams 设置为: $BuildParams" # 从提交消息中提取 BF:要打包迁移的新文件夹名 的值 BFValue=$(echo "$CommitMessage" | grep -oE '\[BF=[^]]*\]' | sed 's/^\[BF=//;s/\]$//') echo "BFValue 值为: $BFValue" # 从提交消息中提取 ProjectFolder:要提交的目标路径 的值 ProjectFolder=$(echo "$CommitMessage" | grep -oE '\[PF=[^]]*\]' | sed 's/^\[PF=//;s/\]$//') echo "ProjectFolder 值为: $ProjectFolder" # 检查 BFValue 或 ProjectFolder 是否为空 if [ -z "$BFValue" ] || [ -z "$ProjectFolder" ]; then echo "BFValue 或 ProjectFolder 不存在,执行其它操作..." # 在这里从 BuildParams 获取值,枚举过滤 case $BuildParams in "NT") BFValue="newTest" ProjectFolder="project_web/test" ;; *) ;; esac fi # TSETValue不为空则正常执行迁移Git逻辑 if [ "$ProjectFolder" != "" ]; then # 先创建对应的项目文件夹路径 if ! [ -d "./test/$ProjectFolder" ]; then mkdir -p ./test/$ProjectFolder fi # 进入目标项目执行git cd ./test git pull --rebase cd .. rm -rf ./test/$ProjectFolder/* ls cd ./dist zip -r $BFValue.zip ./$BFValue cd .. # 移动原本dist的文件内容到要迁移的git目标项目上 mv ./dist/$BFValue/** ./test/$ProjectFolder cd ./test/$ProjectFolder git add . # 获取 git status 的输出 GitStatusMessage=$(git status) # 检查git输出中是否包含 "nothing to commit, working tree clean" if echo "$GitStatusMessage" | grep -q "nothing to commit, working tree clean"; then echo "Git提交失败:没有变更或需要提交的内容" fi
#这里面的Git地址换成自己的 git commit -m "$(echo "ci("coding"):多产品打包\n分支:$BRANCH_NAME\n打包项目:$BuildParams\n触发人:$TRIGGER_USER_NAME\n构建记录:https:// codingcorp.coding.test.com/p/map-b_touch/ci/job/${JOB_ID}/build/${CI_BUILD_NUMBER}/pipeline")" git push else echo "ProjectFolder 为空" exit 1 fi
标签:ProjectFolder,Git,Demo,coding,BPValue,echo,git,BuildParams,test From: https://www.cnblogs.com/xiaozhu007/p/18532491