Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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-<os>-<arch>.tar.gz and look
# for the binary fly-<os>-<arch> 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}'
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<arch>.tar.gz` with the binary `fly-linux-<arch>` 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.
47 changes: 5 additions & 42 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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