# 1. 环境准备
yum install -y gcc make rpm-build redhat-rpm-config
go-bin-rpm
,用它可以很方便的将二进制文件打包成 rpm 包。go-bin-rpm
的作者提供了多种安装方式wget -O - https://raw.githubusercontent.com/mh-cbon/latest/master/bintray.sh \
| GH=mh-cbon/go-bin-rpm sh -xe
--version
命令查看是否安装成功$ go-bin-rpm --version
go-bin-rpm version 1.0.0
# 2. 配置文件
go-bin-rpm
的配置文件是 rpm.json
,当然你也可以用其他文件名,我这里直接使用默认的,这样后面打包时就不用指定配置文件的名字。rpm.json
的内容相当精简,且直观易懂,模板如下{
"name": "you-service-name", # rpm 包名称
"version": "0.0.1",
"release": "20221110",
"arch": "x86_64",
"summary": "",
"description": "",
"license": "iswbm.com",
"url": "https://gtihub.com/iswbm/!name!", # !name! 相当于变量,会取前面 rpm 包名称
"files": [
{
"from": "./bin/!name!",
"to": "/usr/local/!name!/",
"base": "",
"type": ""
},
{
"from": "./!name!.service",
"to": "/usr/lib/systemd/system/",
"base": "",
"type": ""
}
]
}
files
要包含如下几项内容:二进制文件 service 文件 配置文件(如果需要的话)
[Unit]
Description=
After=syslog.target network.target
[Service]
Environment=key=value
Type=simple
NotifyAccess=all
TimeoutStartSec=0
Restart=always
User=root
ExecStart=/usr/local/xxxx/bin/xxxx
[Install]
WantedBy=multi-user.target
# VERSION 和 RELEASE 请对应替换
go-bin-rpm generate -o rpms/xxxx-$(VERSION)-$(RELEASE).rpm
rpm.json
,然后在打包时,使用 -f rpm.json
来指定配置文件。# 3. 使用 Makefile
VERSION = 1.0.0
RELEASE = $(shell date +"%Y%m%d")
.PHONY: build-go
build-go:
go build -o ./bin/hello .
.PHONY: build
build: build-go
sed -i "s/VERSION/$(VERSION)/g" rpm.json
sed -i "s/RELEASE/$(RELEASE)/g" rpm.json
mkdir -p rpms
go-bin-rpm generate -o rpms/hellp-$(VERSION)-$(RELEASE).rpm
后面只要使用 make build
就可以直接一步实现 编译
+ 打包
,使用上更加顺手。
来源:https://mp.weixin.qq.com/s/cF-7vEAl3wtYItDg1Z6D9A
完整资料领取看这里
扫码免费领取,仅剩60个名额