Improve build

This commit is contained in:
世界 2022-08-12 22:53:46 +08:00
parent 340fce9f1c
commit 44fcfab9aa
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
13 changed files with 210 additions and 30 deletions

View file

@ -47,6 +47,7 @@ jobs:
go mod init build
go get -v github.com/sagernet/sing-box@$version
popd
continue-on-error: true
- name: Run Test
run: |
go test -v ./...
@ -160,6 +161,7 @@ jobs:
GOARM: ${{ matrix.goarm }}
GOMIPS: ${{ matrix.gomips }}
CGO_ENABLED: 0
TAGS: with_clash_api,with_quic
steps:
- name: Checkout
uses: actions/checkout@v2
@ -181,18 +183,9 @@ jobs:
key: go-${{ hashFiles('**/go.sum') }}
- name: Build
id: build
run: |
VERSION="$(date +%Y%m%d).$(git rev-parse --short HEAD)"
BUILDTIME="$(LANG=en_US.UTF-8 date -u)"
go build -v -trimpath -ldflags '\
-X "github.com/sagernet/sing-box/constant.Version=$VERSION" \
-X "github.com/sagernet/sing-box/constant.BuildTime=$BUILDTIME" \
-s -w -buildid=' ./cmd/sing-box
echo "::set-output name=VERSION::$VERSION"
run: make
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: sing-box-${{ matrix.name }}-${{ steps.build.outputs.VERSION }}
name: sing-box-${{ matrix.name }}
path: sing-box*

3
.gitignore vendored
View file

@ -3,4 +3,5 @@
/*.json
/*.db
/site/
/bin/
/bin/
/dist/

View file

@ -17,4 +17,4 @@ linters-settings:
- prefix(github.com/sagernet/)
- default
staticcheck:
go: '1.18'
go: '1.19'

82
.goreleaser.yaml Normal file
View file

@ -0,0 +1,82 @@
project_name: sing-box
builds:
- main: ./cmd/sing-box
flags:
- -v
- -trimpath
asmflags:
- all=-trimpath={{.Env.GOPATH}}
gcflags:
- all=-trimpath={{.Env.GOPATH}}
ldflags:
- -X github.com/sagernet/sing-box/constant.Commit={{ .ShortCommit }} -s -w -buildid=
tags:
- with_clash_api
- with_quic
env:
- CGO_ENABLED=0
targets:
- linux_amd64_v1
- linux_amd64_v3
- linux_arm64
- linux_arm_7
- windows_amd64_v1
- windows_amd64_v3
- windows_386
- windows_arm64
- darwin_amd64_v1
- darwin_amd64_v3
- darwin_arm64
mod_timestamp: '{{ .CommitTimestamp }}'
snapshot:
name_template: "{{ .Version }}.{{ .ShortCommit }}"
archives:
- id: archive
format: tar.gz
format_overrides:
- goos: windows
format: zip
wrap_in_directory: true
files:
- LICENSE
- src: release/config/config.json
strip_parent: true
name_template: '{{ .ProjectName }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}'
nfpms:
- id: package
package_name: sing-box
file_name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}'
vendor: sagernet
homepage: https://sing-box.sagernet.org/
maintainer: nekohasekai <contact-git@sekai.icu>
description: The universal proxy platform.
license: GPLv3 or later
formats:
- deb
- rpm
priority: extra
contents:
- src: release/config/config.json
dst: /etc/sing-box/config.json
type: config
- src: release/config/sing-box.service
dst: /etc/systemd/system/sing-box.service
- src: release/config/sing-box@.service
dst: /etc/systemd/system/sing-box@.service
- src: LICENSE
dst: /usr/share/licenses/sing-box/LICENSE
source:
enabled: true
name_template: '{{ .ProjectName }}-{{ .Version }}.source'
prefix_template: '{{ .ProjectName }}-{{ .Version }}/'
checksum:
name_template: '{{ .ProjectName }}-{{ .Version }}.checksum'
signs:
- artifacts: checksum
release:
github:
owner: SagerNet
name: sing-box
name_template: '{{ if .IsSnapshot }}{{ nightly }}{{ else }}{{ .Version }}{{ end }}'
draft: true
mode: replace

53
Makefile Normal file
View file

@ -0,0 +1,53 @@
NAME=sing-box
COMMIT=$(shell git rev-parse --short HEAD)
PARAMS=-trimpath -tags '$(TAGS)' -ldflags \
'-X "github.com/sagernet/sing-box/constant.Commit=$(COMMIT)" \
-w -s -buildid='
MAIN=./cmd/sing-box
.PHONY: test release
build:
go build $(PARAMS) $(MAIN)
action_version: build
echo "::set-output name=VERSION::`./sing-box version -n`"
install:
go install $(PARAMS) $(MAIN)
release:
goreleaser release --snapshot --rm-dist
fmt_install:
go install -v mvdan.cc/gofumpt@latest
go install -v github.com/daixiang0/gci@v0.4.0
fmt:
gofumpt -l -w .
gofmt -s -w .
gci write -s "standard,prefix(github.com/sagernet/),default" .
lint_install:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
lint:
GOOS=linux golangci-lint run ./...
GOOS=windows golangci-lint run ./...
GOOS=darwin golangci-lint run ./...
GOOS=freebsd golangci-lint run ./...
test:
go test -v . && \
pushd test && \
go test -v . && \
popd
clean:
rm -rf bin dist
rm -f $(shell go env GOPATH)/sing-box
update:
git fetch
git reset FETCH_HEAD --hard
git clean -fdx

View file

@ -17,11 +17,37 @@ var commandVersion = &cobra.Command{
Args: cobra.NoArgs,
}
func printVersion(cmd *cobra.Command, args []string) {
os.Stderr.WriteString(F.ToString("sing-box version ", C.Version, " (", runtime.Version(), ", ", runtime.GOOS, "/", runtime.GOARCH, ", CGO "))
if C.CGO_ENABLED {
os.Stderr.WriteString("enabled)\n")
} else {
os.Stderr.WriteString("disabled)\n")
}
var nameOnly bool
func init() {
commandVersion.Flags().BoolVarP(&nameOnly, "name", "n", false, "print version name only")
}
func printVersion(cmd *cobra.Command, args []string) {
var version string
if !nameOnly {
version = "sing-box "
}
version += F.ToString(C.Version)
if C.Commit != "" {
version += "." + C.Commit
}
if !nameOnly {
version += " ("
version += runtime.Version()
version += ", "
version += runtime.GOOS
version += ", "
version += runtime.GOARCH
version += ", "
version += "CGO "
if C.CGO_ENABLED {
version += "enabled"
} else {
version += "disabled"
}
version += ")"
}
version += "\n"
os.Stdout.WriteString(version)
}

View file

@ -1,6 +1,6 @@
package constant
var (
Version = "nightly"
BuildTime = "unknown"
Version = "0.1.0"
Commit = ""
)

View file

@ -1,7 +0,0 @@
package box
//go:generate go install -v mvdan.cc/gofumpt@latest
//go:generate go install -v github.com/daixiang0/gci@v0.4.0
//go:generate gofumpt -l -w .
//go:generate gofmt -s -w .
//go:generate gci write -s "standard,prefix(github.com/sagernet/),default" .

View file

@ -0,0 +1,15 @@
[Unit]
Description=sing-box service
Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target
[Service]
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
ExecStart=/usr/bin/sing-box run -c /etc/sing-box/config.json
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,15 @@
[Unit]
Description=sing-box service
Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target
[Service]
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
ExecStart=/usr/bin/sing-box run -c /etc/sing-box/%i.json
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target

View file

@ -11,6 +11,6 @@ popd
sudo cp $(go env GOPATH)/bin/sing-box /usr/local/bin/
sudo mkdir -p /usr/local/etc/sing-box
sudo cp $DIR/config.json /usr/local/etc/sing-box/config.json
sudo cp $PROJECT/release/config/config.json /usr/local/etc/sing-box/config.json
sudo cp $DIR/sing-box.service /etc/systemd/system
sudo systemctl daemon-reload

View file

@ -4,6 +4,8 @@ Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target
[Service]
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
ExecStart=/usr/local/bin/sing-box run -c /usr/local/etc/sing-box/config.json
Restart=on-failure
RestartSec=10s