需求
- 项目使用
git
作为版本管理工具,使用的GitLab
在内网搭建的 - 外网上线时,需要将项目代码打包成
.zip
文件发送给运维人员 - 每次手动压缩文件有点麻烦,改成使用批处理文件处理
- 只有在
master
分支才可以打包 post-push-full.bat
用于全量打包,双击执行即可post-push.bat
用于增量打包,双击执行。需要输入SHA-1
。打包该SHA-1
之后变动的文件。默认打包最后一条记录的修改文件- 打包生成的
zip
文件命名为project_20191216.zip
。其中20191216
是打包时的日期 - 每次打包生成一个
gitlog.html
文件,显示最近的git
提交记录 - 配置
.gitignore
,不要将这几个文件提交到git
上面
思路
- 压缩成
zip
可以使用7-Zip
- 使用
git log
命令可以查看最近的提交记录 - 可以使用
git symbolic-ref --short -q HEAD
或者git rev-parse --abbrev-ref HEAD
获取分支名称
备注:
bat
文件可以使用记事本新建,新建完成后修改扩展名即可bat
文件的编码格式是GB2312
。如果使用其他编码格式,可能会造成中文等显示乱码
实现
安装7-Zip
- 到7-Zip官方网站下载安装包
- 安装
7-Zip
- 设置环境变量
比如安装位置:
C:\Program Files\7-Zip
- 点击
我的电脑
→右键
→属性
→高级
→环境变量
→xx的用户变量
→ 找到Path
→双击
编辑 → 点击新建
→ 输入C:\Program Files\7-Zip
- 设置完成后需要重启电脑
- 打开
cmd
,输入7z
可以显示相关命令
全量打包
- 在项目目录下新建文件
post-push-full.bat
echo on
echo "开始导出本地提交变动..."
@echo ^<html^>^<head^> ^<meta name="viewport" content="width=device-width,initial-scale=1"^> ^</head^>^<body^> ^<h2^>Recent Change log^</h2^> > gitlog.html
git log -n 100 --pretty=format:"<br/><h2 style='margin-bottom: 5px;'><span style='font-size: 14px;'>%%cd</span></h2><div style='font-size: 12px;'>%%an %%ae</div><div style='font-size: 14px;'>%%s</div>" --date=iso >> gitlog.html
@echo ^<body^>^</html^> >> gitlog.html
set "hour=%time:~0,2%"
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set "min=%time:~3,2%"
if "%min:~0,1%" == " " set min=0%min:~1,1%
set "t=%date:~0,4%%date:~5,2%%date:~8,2%%hour%%min%"
echo %t%
set "output_prepend=./project_"
set "output_append=.zip"
set "output=%output_prepend%%t%%output_append%"
echo %output%
del "%output_prepend%*"
for /f %%i in ('git rev-parse --abbrev-ref HEAD') do set "current_branch=%%i"
IF "%current_branch%"=="master" (
git archive --format zip --output "%output%" master -0
)
增量打包
- 在项目目录下新建文件
post-push.bat
echo on
echo "开始导出本地提交变动..."
@echo ^<html^>^<head^> ^<meta name="viewport" content="width=device-width,initial-scale=1"^> ^</head^>^<body^> ^<h2^>Recent Change log^</h2^> > gitlog.html
git log -n 100 --pretty=format:"<br/><h2 style='margin-bottom: 5px;'><span style='font-size: 14px;'>%%cd</span></h2><div style='font-size: 12px;'>%%an %%ae</div><div style='font-size: 14px;'>%%s</div>" --date=iso >> gitlog.html
@echo ^<body^>^</html^> >> gitlog.html
setlocal enabledelayedexpansion
set "hour=%time:~0,2%"
if "%hour:~0,1%" == " " set "hour=0%hour:~1,1%"
set "min=%time:~3,2%"
if "%min:~0,1%" == " " set "min=0%min:~1,1%"
set "t=%date:~0,4%%date:~5,2%%date:~8,2%%hour%%min%"
echo %t%
set "output_prepend=./project_"
set "output_append=.zip"
set "output=%output_prepend%%t%%output_append%"
echo %output%
del "%output_prepend%*"
for /f %%i in ('git rev-parse --abbrev-ref HEAD') do set "current_branch=%%i"
IF "%current_branch%"=="master" (
set /p commit_id=请输入版本SHA-1值:
if "!commit_id!"=="" (
set "commit_id=--no-commit-id"
)
echo !commit_id!
git diff-tree -r !commit_id! --name-only --diff-filter=ACMRT HEAD > diff.log
for /f "delims=" %%j in (diff.log) do 7z a "%output%" %%j
del diff.log
pause
)
.gitignore
- 修改项目的
.gitignore
文件,添加*.bat
、gitlog.html
、project_*.zip
# ...
# zip
project_*.zip
# bat
*.bat
# html
gitlog.html
发表评论