Git 工作流与 Hook¶
记录公司 Commit 规范、Git Hook 与 GitHub Actions,以及个人常用配置。
-
Commit 规范
缺陷类 / 任务类必填字段
-
Git Hook
commit-msg校验、全局hooksPath下发 -
:lucide-github: GitHub Actions
构建并发布 Docker 镜像
-
常用配置
别名、代理、SSH 走代理、清空历史
📝 Commit 规范¶
缺陷 · 任务 · 结构化字段。提交信息必填字段的两类规范。
提交信息需包含相应的结构化字段,常见两类:
| 类型 | 需包含的字段 |
|---|---|
| 缺陷类 | 缺陷、缺陷原因分析、缺陷修复方案、自测结果、修复影响范围 |
| 任务类 | 任务、功能说明、自测结果、受影响模块;或任务 + tag;或任务 + 文档 |
校验用的正则写在下方 commit-msg 钩子里。
🪝 Git Hook¶
提交日志校验(commit-msg)¶
将该脚本保存为 commit-msg,放在仓库的 .git/hooks/ 下即可对每次提交信息进行校验:
文件必须保存为 commit-msg
#!/bin/bash
exec < /dev/tty
# 最前面加一个 exec < /dev/tty,就能交互了,不等待超时的原因是打开的这个 shell 不是交互式的
commit_msg=`cat $1`
msg_re="(缺陷.*缺陷原因分析.*缺陷修复方案.*自测结果.*修复影响范围.*|任务.*功能说明.*自测结果.*受影响模块.*|任务.*tag.*|任务.*文档.*)"
# 检查提交信息是否匹配新的格式要求
if [[ ! $commit_msg =~ $msg_re ]]
then
echo "***************************************"
echo "不合法的 commit 消息提交格式 git commit"
echo "提交规范:缺陷类需包含 缺陷/缺陷原因分析/缺陷修复方案/自测结果/修复影响范围,任务类需包含 任务/功能说明/自测结果/受影响模块"
read -t 3 -p "格式不合规,错误日志被记录会影响绩效,是否要继续提交: y继续提交,其他任意键退出 " input
if [ "$input" == "y" ]; then
exit 0
else
echo "提交终止!!"
exit 1
fi
fi
全局 Hook 路径下发¶
逐个用户设置 core.hooksPath,可统一使用 /opt/githook/ 下的钩子:
获取所有用户并以用户身份执行命令
#!/bin/bash
# 获取系统中的所有用户列表
users=$(cut -d: -f1 /etc/passwd)
# 遍历每个用户并执行命令
for user in $users; do
# 获取用户的 home 目录
home_dir=$(getent passwd "$user" | cut -d: -f6)
# 检查 home 目录是否存在
if [ -d "$home_dir" ]; then
# 使用 su -c 来以用户身份执行命令
su - "$user" -c "git config --global core.hooksPath /opt/githook/"
echo "Command executed for user: $user"
else
echo "Home directory for user $user does not exist, skipping."
fi
done
🚀 GitHub Actions¶
workflow · buildx · secrets · cron。构建并发布 Docker 镜像的流水线。
下面是快速构建并发布 Docker 镜像的 workflow:
.github/workflows/docker.yml
name: Docker Image CI # 定义工作流的名称为 "Docker Image CI"
on: # 定义触发工作流的事件
push: # 当代码推送到以下分支时触发
branches: [ "main" ]
pull_request: # 当有拉取请求到以下分支时触发
branches: [ "main" ]
schedule:
# 设置定时任务的 CRON 表达式,这个例子是每天凌晨 1 点执行(注意时差)
- cron: '10 9 * * *'
# 设置 GITHUB_TOKEN 的权限以允许部署到 GitHub Pages
permissions:
contents: write
pages: write
id-token: write
jobs: # 定义作业
build: # 定义一个名为 build 的作业
runs-on: ubuntu-latest # 指定作业在 ubuntu-latest 系统上运行
environment: xxx # 指定作业的环境
steps: # 定义作业中的一系列步骤
- uses: actions/checkout@v3 # 使用 actions/checkout 动作检出代码到工作目录
- name: Build the Docker image # 给步骤命名为 "Build the Docker image"
run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) # 构建 Docker 镜像的命令
- name: Docker Setup Buildx # 设置 Docker 的 Buildx
uses: docker/setup-buildx-action@v2.10.0 # 使用 docker/setup-buildx-action 动作
- name: Docker Login # 登录到 Docker 仓库
uses: docker/login-action@v2.2.0 # 使用 docker/login-action 动作
with: # 指定登录所需的用户名和密码,从 GitHub 仓库的 secrets 中获取
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and Push Images # 构建并推送镜像
id: buildx # 给这个步骤一个唯一的 id
uses: docker/build-push-action@v4.1.1 # 使用 docker/build-push-action 动作
with: # 指定构建和推送镜像所需的参数
context: . # 指定构建上下文为当前目录
platforms: linux/amd64,linux/arm64,linux/arm/v7 # 指定要构建的平台
push: true # 将构建的镜像推送到 Docker 仓库
tags: admibo/clash_vpn:latest # 指定要为镜像打的标签,使用 latest 标签
tests: # 发送通知,执行 cloudflare 上的任务
name: Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.5.0
- name: deploy-nav
run: curl -X POST "https://api.cloudflare.com/client/v4/pages/webhooks/deploy_hooks/7"
- name: deploy-hook
run: curl -X POST "https://api.cloudflare.com/client/v4/pages/webhooks/deploy_hooks/7"
- name: deploy-hook # 更新一些记录到本仓库,注意使用下面方式后就不要开启 push 触发任务的功能,否则会陷入死循环,任务反复执行
run: echo `date "+%Y-%m-%d %H:%M:%S"` >> .github/readme123
- name: Commit changes
uses: actions-x/commit@v6
with:
email: me@example.com
name: GitHub Actions Autocommitter
branch: master
files: .github/readme123
force: true
directory: .
🔧 常用配置与技巧¶
别名与代理¶
# 别名
git config --global alias.st status
# 代理(privoxy 可把 socks 转 http)
git config --global http.proxy 'http://127.0.0.1:8001'
git config --global https.proxy 'http://127.0.0.1:8001'
git config --global socks.proxy "127.0.0.1:1080"
SSH 走代理¶
带令牌克隆¶
# GitHub
git clone https://用户名:私人令牌@github.com/用户名/仓库名.git
# GitLab
git clone https://oauth2:私人令牌@gitlab.example.com/group/project.git
清空历史提交记录¶
把当前内容作为唯一一次提交推送:
git checkout --orphan=bo
git add .
git commit -m 'commit'
git branch -D master
git branch -m master
git push -f origin master
git branch --set-upstream-to=origin/master
Warning
push -f 会重写远端历史,协作分支慎用。