From 92f92c82cc73c9c560c89950cba53985a1142225 Mon Sep 17 00:00:00 2001 From: mozillazg Date: Sat, 27 Apr 2024 10:30:56 +0800 Subject: [PATCH] ci: add goreleaser --- .github/workflows/goreleaser.yml | 33 ++++++++++++++++++++++++++++++++ .github/workflows/test.yml | 19 ++++++++++++++++++ .goreleaser.yml | 30 +++++++++++++++++++++++++++++ Makefile | 6 +++--- 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/goreleaser.yml create mode 100644 .goreleaser.yml diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml new file mode 100644 index 00000000..803af7c1 --- /dev/null +++ b/.github/workflows/goreleaser.yml @@ -0,0 +1,33 @@ +name: goreleaser + +on: + push: + tags: + - v* + +jobs: + goreleaser: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.21.0' + env: + GOPATH: ${{ env.HOME }} + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + version: latest + args: release --clean + env: + GOPATH: ${{ env.HOME }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b69ecea3..bf4e7b6c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,3 +32,22 @@ jobs: - name: Test run: make test + + releaser-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: 1.21 + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + distribution: goreleaser + version: latest + args: release --snapshot --clean --skip=publish --verbose + env: + WORKDIR: ${{ github.workspace }} diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 00000000..550cdf90 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,30 @@ +before: + hooks: + - sudo apt-get install -y gcc flex bison make libelf-dev + - sudo make libpcap LIBPCAP_DIST_DIR=/usr/local + +builds: + - id: ptcpdump + binary: ptcpdump + env: + - CGO_ENABLED=1 + flags: + - -tags=static + ldflags: + - -linkmode 'external' + - -extldflags "-static" + - -X github.com/mozillazg/ptcpdump/internal.Version={{.Version}} + - -X github.com/mozillazg/ptcpdump/internal.GitCommit={{.Commit}} + goos: + - linux + goarch: + - amd64 + +checksum: + name_template: 'checksums.txt' + +release: + prerelease: auto + +snapshot: + name_template: "{{ .Tag }}-next" diff --git a/Makefile b/Makefile index d45c23b6..503f319d 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ OUTPUT = ./output BPF_SRC = ./bpf LIBPCAP = ./lib/libpcap LIBPCAP_SRC = $(abspath $(LIBPCAP)) -LIBPCAP_DIST_DIR = $(abspath $(OUTPUT)/libpcap) +LIBPCAP_DIST_DIR ?= $(abspath $(OUTPUT)/libpcap) LIBPCAP_HEADER_DIR = $(abspath $(LIBPCAP_DIST_DIR)/include) LIBPCAP_OBJ_DIR = $(abspath $(LIBPCAP_DIST_DIR)/lib) LIBPCAP_OBJ = $(abspath $(LIBPCAP_OBJ_DIR)/libpcap.a) @@ -15,11 +15,11 @@ LIBPCAP_OBJ = $(abspath $(LIBPCAP_OBJ_DIR)/libpcap.a) GIT_COMMIT ?= $(shell git rev-parse --short HEAD) VERSION ?= $(shell git describe --tags --abbrev=0) CGO_CFLAGS_STATIC = "-I$(LIBPCAP_HEADER_DIR)" -CGO_LDFLAGS_STATIC = "-L$(LIBPCAP_OBJ_DIR) -lelf -lz $(LIBPCAP_OBJ)" +CGO_LDFLAGS_STATIC = "-L$(LIBPCAP_OBJ_DIR) -lpcap $(LIBPCAP_OBJ)" CGO_ENABLED ?= 1 GOARCH ?= $(shell go env GOARCH) GOOS ?= $(shell go env GOOS) -LDFLAGS := -extldflags "-static" +LDFLAGS := -linkmode "external" -extldflags "-static" LDFLAGS += -X github.com/mozillazg/ptcpdump/internal.Version=$(VERSION) LDFLAGS += -X github.com/mozillazg/ptcpdump/internal.GitCommit=$(GIT_COMMIT)