版本控制回顾
version-control-systems;衔接 git-essentials。
20 分钟
版本控制:Git 与运维配置管理
Infrastructure as Code 时代,Nginx 配置、Postfix 模板、DNS 变更脚本、部署 playbook 都应纳入版本控制。本课在 git-essentials 基础上,聚焦 运维场景的 Git 工作流——与开发共用仓库还是独立 infra 仓、如何 review 配置变更、如何与 CI/CD 联动。服务器操作见 linux-essentials,网络与端口见 networking-essentials,DNS 记录见 dns-essentials,远程操作见 dnm-ssh,编辑器见 dnm-editors。
学习目标
- 建立 infra 配置仓库 的目录结构与分支策略
- 用 Git 管理 Postfix/Nginx 配置变更并 trace 历史
- 理解 配置 drift 与 Git 作为「期望状态」源的意义
运维仓库结构示例
infra/
├── nginx/
│ ├── sites-available/
│ └── snippets/
├── postfix/
│ ├── main.cf
│ └── master.cf
├── dovecot/
│ └── conf.d/
├── dns/
│ └── example.com.zone.txt
├── scripts/
│ ├── deploy.sh
│ └── backup-mail.sh
└── README.md
与 git-essentials 的 feature branch 一致:一个分支一个故事。
基本工作流
git clone git@github.com:org/infra.git
cd infra
git checkout -b feat/postfix-relay-sendgrid
vim postfix/main.cf
# 修改 relayhost
git diff
git add postfix/main.cf
git commit -m "postfix: switch relay to SendGrid on port 587"
git push -u origin feat/postfix-relay-sendgrid
# 开 PR,同事 review 后 merge
PR 描述应含:影响范围、回滚步骤、是否需 systemctl reload。
部署:从 Git 到服务器
# 服务器上 /opt/infra 为 clone
cd /opt/infra
git pull origin main
# 同步配置(示例)
sudo cp postfix/main.cf /etc/postfix/main.cf
sudo postfix check && sudo systemctl reload postfix
# 或 rsync / 软链 / Ansible(进阶见 devops-cloud-essentials)
# deploy.sh 示意
#!/bin/bash
set -euo pipefail
cd /opt/infra
git pull --ff-only origin main
sudo cp postfix/main.cf /etc/postfix/main.cf
sudo postfix check
sudo systemctl reload postfix
echo "Postfix config deployed: $(git rev-parse --short HEAD)"
脚本本身也在 Git 里——改 deploy 逻辑也要 PR。