From c09138d667b94b11cdd43109ba2d3e27f183a46d Mon Sep 17 00:00:00 2001 From: nabil1440 <52530910+nabil1440@users.noreply.github.com> Date: Mon, 21 Sep 2026 16:13:45 +0600 Subject: [PATCH] build: add a Makefile for the build, test, lint and release commands - make build, test, vet, lint, vuln, fmt, fmt-check, check, release, clean and help, in the style of migration-agent. - lint and vuln run pinned tool versions through go run, so the tools are built with the Go version of this module. - make check is the local gate before each merge (CI is skipped, #7). - make release makes the same archives as build.sh before, plus build/checksums.txt. build.sh is now a wrapper for make release, so the release workflow does not change. - README: document the targets. Closes #16 --- Makefile | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 16 ++++++++++++++ build.sh | 47 +++++---------------------------------- 3 files changed, 87 insertions(+), 42 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0d7f006 --- /dev/null +++ b/Makefile @@ -0,0 +1,66 @@ +# Developer commands for fly. Run "make help" for the list. +# Keep this file compatible with GNU Make 3.81, the version on macOS. + +.PHONY: build test vet lint vuln fmt fmt-check check release clean help + +BINARY := fly +PKG := github.com/flywp/server-cli + +# VERSION defaults to the git version. Override it for a release: +# make release VERSION=v0.2.0 +VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev) +COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo unknown) +BUILD_DATE := $(shell date -u +%Y-%m-%d) + +LDFLAGS := -X $(PKG)/internal/version.Version=$(VERSION) \ + -X $(PKG)/internal/version.CommitHash=$(COMMIT) \ + -X $(PKG)/internal/version.BuildDate=$(BUILD_DATE) + +# Release platforms. Installed CLIs download fly--.tar.gz and look +# for the binary fly-- in it, so do not change these names. +RELEASE_PLATFORMS := linux/amd64 linux/arm64 + +# "go run pkg@version" builds each tool with the Go version of this module, +# so a tool cannot be older than go.mod. +GOLANGCI_LINT := go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.13.2 +GOVULNCHECK := go run golang.org/x/vuln/cmd/govulncheck@v1.8.0 + +build: ## Build bin/fly for this platform + go build -trimpath -ldflags "$(LDFLAGS)" -o bin/$(BINARY) . + +test: ## Run the tests with the race detector + go test ./... -race -count=1 + +vet: ## Run go vet + go vet ./... + +lint: ## Run golangci-lint + $(GOLANGCI_LINT) run + +vuln: ## Scan for known vulnerabilities + $(GOVULNCHECK) ./... + +fmt: ## Format the code + gofmt -w . + +fmt-check: ## Fail if the code is not formatted + @files=$$(gofmt -l .); if [ -n "$$files" ]; then echo "Run make fmt for:"; echo "$$files"; exit 1; fi + +check: fmt-check vet lint test vuln ## Run all checks (the gate before each merge) + +release: ## Build the static release archives and checksums.txt in build/ + rm -rf build + mkdir -p build + @for platform in $(RELEASE_PLATFORMS); do \ + os=$${platform%/*}; arch=$${platform#*/}; out=$(BINARY)-$$os-$$arch; \ + echo "Building $$out ($(VERSION))"; \ + CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags "-s -w $(LDFLAGS)" -o build/$$out . || exit 1; \ + COPYFILE_DISABLE=1 tar -czf build/$$out.tar.gz -C build $$out || exit 1; \ + done + cd build && (command -v sha256sum >/dev/null 2>&1 && sha256sum *.tar.gz || shasum -a 256 *.tar.gz) > checksums.txt + +clean: ## Remove bin/ and build/ + rm -rf bin/ build/ + +help: ## Show the targets + @grep -E '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | awk -F':.*## ' '{printf " %-10s %s\n", $$1, $$2}' diff --git a/README.md b/README.md index eec72e8..cafa8a7 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,22 @@ fly sites stop # stops all sites fly sites restart # stops and starts all sites ``` +## Development + +Go 1.27 or later is required (`go.mod` selects the toolchain). The Makefile holds the common tasks: + +```bash +make build # builds bin/fly with the version from git +make test # go test ./... -race +make lint # golangci-lint (pinned version, built with the module's Go) +make vuln # govulncheck +make check # fmt-check, vet, lint, test and vuln: run this before each merge +make release # static linux/amd64 and linux/arm64 archives + checksums.txt in build/ +make help # lists all targets +``` + +`make release VERSION=v0.2.0` stamps a specific version. The release archives must keep the names `fly-linux-.tar.gz` with the binary `fly-linux-` inside: installed CLIs look for these names when they run `fly update`. + ## License This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. diff --git a/build.sh b/build.sh index 678304c..a7e5f0c 100755 --- a/build.sh +++ b/build.sh @@ -1,46 +1,9 @@ #!/bin/bash -set -e - -# Set variables -CLI_NAME="fly" -REPO_NAME="flywp/server-cli" -VERSION=$(git describe --tags --always --dirty) -COMMIT_HASH=$(git rev-parse HEAD) -BUILD_DATE=$(date -u +"%Y-%m-%d") -LDFLAGS="-X github.com/${REPO_NAME}/internal/version.Version=${VERSION} -X github.com/${REPO_NAME}/internal/version.CommitHash=${COMMIT_HASH} -X github.com/${REPO_NAME}/internal/version.BuildDate=${BUILD_DATE}" - -# Build function -build() { - local GOOS=$1 - local GOARCH=$2 - local OUTPUT="${CLI_NAME}-${GOOS}-${GOARCH}" - - echo "Building for ${GOOS}/${GOARCH}..." - # CGO_ENABLED=0 gives a static binary that does not depend on the host glibc - CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} go build -trimpath -ldflags "-s -w ${LDFLAGS}" -o "build/${OUTPUT}" . - echo "Done building ${OUTPUT}" - - create_archive "${OUTPUT}" -} +# The release workflow runs this script. The build steps are in the Makefile: +# see "make release". -# Create tar.gz archive function -create_archive() { - local OUTPUT=$1 - - echo "Creating archive for ${OUTPUT}..." - tar -czvf "build/${OUTPUT}.tar.gz" -C build "${OUTPUT}" - echo "Done creating archive ${OUTPUT}.tar.gz" -} - -# Clean build directory -rm -rf build - -# Create build directory if not exists -mkdir -p build - -# Build for different platforms -build linux amd64 -build linux arm64 +set -e -echo "All builds completed!" +cd "$(dirname "$0")" +exec make release