Skip to content

Commit

Permalink
Migrate deb packaging to GoReleaser (#87)
Browse files Browse the repository at this point in the history
* Makefile: Use install(1) instead of cp(1)

* Use GoReleaser to generate deb package

* Add postinstall script

* Add preremove script
  • Loading branch information
iBug authored Jul 24, 2024
1 parent 63bdc74 commit ca74a48
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
42 changes: 38 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: 2
before:
hooks:
- go mod tidy
builds:
-
id: yukid
- id: yukid
binary: yukid
env:
- CGO_ENABLED=0
Expand All @@ -16,8 +16,7 @@ builds:
- -trimpath
ldflags:
- -s -w -X github.com/ustclug/Yuki/pkg/info.Version={{.Version}} -X github.com/ustclug/Yuki/pkg/info.BuildDate={{.Date}} -X github.com/ustclug/Yuki/pkg/info.GitCommit={{.Commit}}
-
id: yukictl
- id: yukictl
binary: yukictl
env:
- CGO_ENABLED=0
Expand All @@ -30,6 +29,11 @@ builds:
- -trimpath
ldflags:
- -s -w -X github.com/ustclug/Yuki/pkg/info.Version={{.Version}} -X github.com/ustclug/Yuki/pkg/info.BuildDate={{.Date}} -X github.com/ustclug/Yuki/pkg/info.GitCommit={{.Commit}}
hooks:
post:
- cmd: sh -c "{{ .Path }} completion bash > bash_completion"
dir: "{{ dir (dir .Path) }}"
output: true
archives:
- format: binary
name_template: "{{ .Binary }}_{{ .Os }}_{{ .Arch }}"
Expand All @@ -39,6 +43,36 @@ snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
use: github-native
nfpms:
- id: default
package_name: yuki
homepage: https://github.com/ustclug/Yuki
maintainer: "USTC LUG <[email protected]>"
description: |-
USTC Mirror Manager
formats:
- deb
umask: 0o022
dependencies:
- "docker.io | docker-engine | docker-ce"
section: admin
priority: extra
provides:
- yukid
- yukictl
scripts:
postinstall: etc/postinst.sh
preremove: etc/prerm.sh
contents:
- src: dist/bash_completion
dst: /etc/bash_completion.d/yukictl
- src: etc/daemon.example.toml
dst: /etc/yuki/
- src: etc/yukid.service
dst: /lib/systemd/system/
- src: yukictl
dst: /usr/bin/yuki
type: symlink

# modelines, feel free to remove those if you don't want/use them:
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
Expand Down
20 changes: 5 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
.PHONY: all
all: yukid yukictl

.PHONY: release
release:
goreleaser release --snapshot --clean

.PHONY: clean
clean:
rm -f yukid yukictl *.deb
rm -rf yukid yukictl dist/

.PHONY: lint
lint:
Expand Down Expand Up @@ -33,17 +37,3 @@ yukid:
.PHONY: yukictl
yukictl:
go build -ldflags "$(go_ldflags)" -trimpath ./cmd/yukictl

.PHONY: deb

deb_dir := $(shell mktemp -d)
deb: | yukid yukictl
mkdir -p $(addprefix $(deb_dir)/, DEBIAN etc/bash_completion.d etc/yuki lib/systemd/system usr/local/bin)
cp etc/daemon.example.toml $(deb_dir)/etc/yuki
cp etc/yukid.service $(deb_dir)/lib/systemd/system
cp yukid yukictl $(deb_dir)/usr/local/bin
ln -s yukictl $(deb_dir)/usr/local/bin/yuki
$(deb_dir)/usr/local/bin/yukictl completion bash > $(deb_dir)/etc/bash_completion.d/yukictl
sed "s/\$$VERSION\>/$(version)/g;s/^Version: v/Version: /g;s/\$$ARCH\>/$(shell go env GOARCH)/g" \
etc/debian-control > $(deb_dir)/DEBIAN/control
dpkg-deb --root-owner-group --build -Zxz $(deb_dir) .
9 changes: 0 additions & 9 deletions etc/debian-control

This file was deleted.

6 changes: 6 additions & 0 deletions etc/postinst.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# Only run if systemd is running
[ -d /run/systemd ] || exit 0

systemctl enable yukid.service
6 changes: 6 additions & 0 deletions etc/prerm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# Only run if systemd is running
[ -d /run/systemd ] || exit 0

systemctl disable --now yukid.service
4 changes: 2 additions & 2 deletions etc/yukid.service
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ ConditionPathExists=/etc/yuki/daemon.toml
[Service]
Type=exec
User=mirror
ExecStart=/usr/local/bin/yukid
ExecReload=/usr/local/bin/yukictl reload
ExecStart=/usr/bin/yukid
ExecReload=/usr/bin/yukictl reload
Restart=on-failure
RestartSec=5

Expand Down

0 comments on commit ca74a48

Please sign in to comment.