forgejo-runner

git clone git://git.lin.moe/forgejo-runner.git

  1DIST := dist
  2EXECUTABLE := forgejo-runner
  3GOFMT ?= gofumpt -l
  4DIST := dist
  5DIST_DIRS := $(DIST)/binaries $(DIST)/release
  6GO ?= go
  7SHASUM ?= shasum -a 256
  8HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
  9XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
 10XGO_VERSION := go-1.21.x
 11GXZ_PAGAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.10
 12
 13LINUX_ARCHS ?= linux/amd64,linux/arm64
 14DARWIN_ARCHS ?= darwin-12/amd64,darwin-12/arm64
 15WINDOWS_ARCHS ?= windows/amd64
 16GO_FMT_FILES := $(shell find . -type f -name "*.go" ! -name "generated.*")
 17GOFILES := $(shell find . -type f -name "*.go" -o -name "go.mod" ! -name "generated.*")
 18
 19DOCKER_IMAGE ?= gitea/act_runner
 20DOCKER_TAG ?= nightly
 21DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)
 22DOCKER_ROOTLESS_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)-dind-rootless
 23
 24EXTLDFLAGS = -extldflags "-static" $(null)
 25
 26ifeq ($(HAS_GO), GO)
 27	GOPATH ?= $(shell $(GO) env GOPATH)
 28	export PATH := $(GOPATH)/bin:$(PATH)
 29
 30	CGO_EXTRA_CFLAGS := -DSQLITE_MAX_VARIABLE_NUMBER=32766
 31	CGO_CFLAGS ?= $(shell $(GO) env CGO_CFLAGS) $(CGO_EXTRA_CFLAGS)
 32endif
 33
 34ifeq ($(OS), Windows_NT)
 35	GOFLAGS := -v -buildmode=exe
 36	EXECUTABLE ?= $(EXECUTABLE).exe
 37else ifeq ($(OS), Windows)
 38	GOFLAGS := -v -buildmode=exe
 39	EXECUTABLE ?= $(EXECUTABLE).exe
 40else
 41	GOFLAGS := -v
 42	EXECUTABLE ?= $(EXECUTABLE)
 43endif
 44
 45STORED_VERSION_FILE := VERSION
 46
 47ifneq ($(DRONE_TAG),)
 48	VERSION ?= $(subst v,,$(DRONE_TAG))
 49	RELASE_VERSION ?= $(VERSION)
 50else
 51	ifneq ($(DRONE_BRANCH),)
 52		VERSION ?= $(subst release/v,,$(DRONE_BRANCH))
 53	else
 54		VERSION ?= main
 55	endif
 56
 57	STORED_VERSION=$(shell cat $(STORED_VERSION_FILE) 2>/dev/null)
 58	ifneq ($(STORED_VERSION),)
 59		RELASE_VERSION ?= $(STORED_VERSION)
 60	else
 61		RELASE_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
 62	endif
 63endif
 64
 65GO_PACKAGES_TO_VET ?= $(filter-out gitea.com/gitea/act_runner/internal/pkg/client/mocks,$(shell $(GO) list ./...))
 66
 67
 68TAGS ?=
 69LDFLAGS ?= -X "gitea.com/gitea/act_runner/internal/pkg/ver.version=v$(RELASE_VERSION)"
 70
 71all: build
 72
 73fmt:
 74	@hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
 75		$(GO) install mvdan.cc/gofumpt@latest; \
 76	fi
 77	$(GOFMT) -w $(GO_FMT_FILES)
 78
 79.PHONY: go-check
 80go-check:
 81	$(eval MIN_GO_VERSION_STR := $(shell grep -Eo '^go\s+[0-9]+\.[0-9]+' go.mod | cut -d' ' -f2))
 82	$(eval MIN_GO_VERSION := $(shell printf "%03d%03d" $(shell echo '$(MIN_GO_VERSION_STR)' | tr '.' ' ')))
 83	$(eval GO_VERSION := $(shell printf "%03d%03d" $(shell $(GO) version | grep -Eo '[0-9]+\.[0-9]+' | tr '.' ' ');))
 84	@if [ "$(GO_VERSION)" -lt "$(MIN_GO_VERSION)" ]; then \
 85		echo "Act Runner requires Go $(MIN_GO_VERSION_STR) or greater to build. You can get it at https://go.dev/dl/"; \
 86		exit 1; \
 87	fi
 88
 89.PHONY: fmt-check
 90fmt-check:
 91	@hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
 92		$(GO) install mvdan.cc/gofumpt@latest; \
 93	fi
 94	@diff=$$($(GOFMT) -d $(GO_FMT_FILES)); \
 95	if [ -n "$$diff" ]; then \
 96		echo "Please run 'make fmt' and commit the result:"; \
 97		echo "$${diff}"; \
 98		exit 1; \
 99	fi;
100
101test: fmt-check
102	@$(GO) test -v -cover -coverprofile coverage.txt ./... && echo "\n==>\033[32m Ok\033[m\n" || exit 1
103
104.PHONY: vet
105vet:
106	@echo "Running go vet..."
107	@$(GO) vet $(GO_PACKAGES_TO_VET)
108
109install: $(GOFILES)
110	$(GO) install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)'
111
112build: go-check $(EXECUTABLE)
113
114$(EXECUTABLE): $(GOFILES)
115	$(GO) build -v -tags 'netgo osusergo $(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o $@
116
117.PHONY: deps-backend
118deps-backend:
119	$(GO) mod download
120	$(GO) install $(GXZ_PAGAGE)
121	$(GO) install $(XGO_PACKAGE)
122
123.PHONY: release
124release: release-windows release-linux release-darwin release-copy release-compress release-check
125
126$(DIST_DIRS):
127	mkdir -p $(DIST_DIRS)
128
129.PHONY: release-windows
130release-windows: | $(DIST_DIRS)
131	CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -buildmode exe -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets '$(WINDOWS_ARCHS)' -out $(EXECUTABLE)-$(VERSION) .
132ifeq ($(CI),true)
133	cp -r /build/* $(DIST)/binaries/
134endif
135
136.PHONY: release-linux
137release-linux: | $(DIST_DIRS)
138	CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets '$(LINUX_ARCHS)' -out $(EXECUTABLE)-$(VERSION) .
139ifeq ($(CI),true)
140	cp -r /build/* $(DIST)/binaries/
141endif
142
143.PHONY: release-darwin
144release-darwin: | $(DIST_DIRS)
145	CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets '$(DARWIN_ARCHS)' -out $(EXECUTABLE)-$(VERSION) .
146ifeq ($(CI),true)
147	cp -r /build/* $(DIST)/binaries/
148endif
149
150.PHONY: release-copy
151release-copy: | $(DIST_DIRS)
152	cd $(DIST); for file in `find . -type f -name "*"`; do cp $${file} ./release/; done;
153
154.PHONY: release-check
155release-check: | $(DIST_DIRS)
156	cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "checksumming $${file}" && $(SHASUM) `echo $${file} | sed 's/^..//'` > $${file}.sha256; done;
157
158.PHONY: release-compress
159release-compress: | $(DIST_DIRS)
160	cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && $(GO) run $(GXZ_PAGAGE) -k -9 $${file}; done;
161
162.PHONY: docker
163docker:
164	if ! docker buildx version >/dev/null 2>&1; then \
165		ARG_DISABLE_CONTENT_TRUST=--disable-content-trust=false; \
166	fi; \
167	docker build $${ARG_DISABLE_CONTENT_TRUST} -t $(DOCKER_REF) .
168	docker build $${ARG_DISABLE_CONTENT_TRUST} -t $(DOCKER_ROOTLESS_REF) -f Dockerfile.rootless .
169
170clean:
171	$(GO) clean -x -i ./...
172	rm -rf coverage.txt $(EXECUTABLE) $(DIST)
173
174version:
175	@echo $(VERSION)