-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
330 lines (300 loc) Β· 12.5 KB
/
Makefile
File metadata and controls
330 lines (300 loc) Β· 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Go build optimizations: smaller binaries, reproducible builds
# -ldflags="-s -w": strip debug info and symbol tables
# -trimpath: remove file system paths for reproducibility
# CGO_ENABLED=0: pure-Go build (no C dependencies)
# Optionally, run 'upx --best --lzma <binary>' after build for further compression
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build $(LDFLAGS)
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GO111MODULE=on
# Determine GOOS and GOARCH
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
# Binary and versioning
BINARY_NAME=dosync
LDFLAGS=-ldflags "-X main.Version=${VERSION} -s -w" -trimpath
BUILD_DIR=release
# Docker Hub parameters
DOCKER=docker
IMAGE_NAME=localrivet/dosync
TAG ?= latest
export CGO_ENABLED=0
.PHONY: help
help:
@echo "\nAvailable make targets:"
@echo " help Show this help message"
@echo " build Build for current platform (default Go env)"
@echo " build-linux Build for Linux (amd64)"
@echo " build-darwin-arm64 Build for macOS (arm64)"
@echo " build-darwin-amd64 Build for macOS (amd64)"
@echo " build-all Build for all major platforms (Linux/macOS)"
@echo " install Install the binary to /usr/local/bin"
@echo " clean Remove build artifacts"
@echo " run-dev Build and run in development mode"
@echo " test Run Go tests"
@echo " fmt Run go fmt on source files"
@echo " run Build and run the binary for the current platform"
@echo " docker-build Build Docker image for Docker Hub (single platform)"
@echo " docker-tag Tag Docker image with current version"
@echo " docker-push Push Docker image (latest and version) to Docker Hub"
@echo " docker-buildx Build and push multi-platform Docker image to Docker Hub (recommended)"
@echo " release Commit, tag, push, and build/push multi-platform Docker images for a new version (use: make release VERSION=v1.0.0)"
@echo " release-assets Build all binaries and create a GitHub release with attached assets (requires gh CLI)"
@echo " build-linux-arm64 Build for Linux (arm64)"
@echo " build-linux-armv7 Build for Linux (arm/v7)"
@echo " release-upload-assets Upload binaries to an existing GitHub release (use: make release-upload-assets VERSION=v1.0.0)"
@echo " docker-release Standalone Docker build and push for an existing release version"
@echo ""
.PHONY: all clean build build-linux build-darwin build-all
all: clean build-all
build:
@echo "Building for current platform..."
@mkdir -p $(BUILD_DIR)/$(GOOS)/$(GOARCH)
$(GOBUILD) -o $(BUILD_DIR)/$(GOOS)/$(GOARCH)/$(BINARY_NAME) .
build-linux:
@echo "Building for Linux (amd64)..."
@mkdir -p $(BUILD_DIR)/linux/amd64
GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/linux/amd64/$(BINARY_NAME) .
build-darwin-arm64:
@echo "Building for macOS (arm64)..."
@mkdir -p $(BUILD_DIR)/darwin/arm64
GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(BUILD_DIR)/darwin/arm64/$(BINARY_NAME) .
build-darwin-amd64:
@echo "Building for macOS (amd64)..."
@mkdir -p $(BUILD_DIR)/darwin/amd64
GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/darwin/amd64/$(BINARY_NAME) .
build-linux-arm64:
@echo "Building for Linux (arm64)..."
@mkdir -p $(BUILD_DIR)/linux/arm64
GOOS=linux GOARCH=arm64 $(GOBUILD) -o $(BUILD_DIR)/linux/arm64/$(BINARY_NAME) .
build-linux-armv7:
@echo "Building for Linux (arm/v7)..."
@mkdir -p $(BUILD_DIR)/linux/armv7
GOOS=linux GOARCH=arm GOARM=7 $(GOBUILD) -o $(BUILD_DIR)/linux/armv7/$(BINARY_NAME) .
build-all: build-linux build-linux-arm64 build-linux-armv7 build-darwin-arm64 build-darwin-amd64
install: build
@echo "Installing to /usr/local/bin/$(BINARY_NAME)..."
@sudo cp $(BUILD_DIR)/$(GOOS)/$(GOARCH)/$(BINARY_NAME) /usr/local/bin/$(BINARY_NAME)
@sudo chmod +x /usr/local/bin/$(BINARY_NAME)
@echo "Installation complete."
clean:
@echo "Cleaning build directory..."
@rm -rf $(BUILD_DIR)
# Helper for development testing
run-dev: build
@echo "Running in development mode..."
@./$(BUILD_DIR)/$(GOOS)/$(GOARCH)/$(BINARY_NAME) sync -f docker-compose.yml -i 30s --verbose
.PHONY: test
test:
$(GOTEST) ./
.PHONY: fmt
fmt:
$(GOCMD) fmt ./cmd/...
$(GOCMD) fmt ./main.go
.PHONY: run
run:
rm -f ./release/${GOOS}/$(GOARCH)/$(BINARY_NAME)
make build
./release/${GOOS}/$(GOARCH)/$(BINARY_NAME)
.PHONY: docker-build
# Build the Docker image for Docker Hub
docker-build:
$(DOCKER) build -t $(IMAGE_NAME):$(TAG) .
.PHONY: docker-tag
# Tag the image with the current version (from git)
docker-tag:
$(DOCKER) tag $(IMAGE_NAME):$(TAG) $(IMAGE_NAME):$(VERSION)
.PHONY: docker-push
# Push both :latest and :<version> tags to Docker Hub
docker-push:
$(DOCKER) push $(IMAGE_NAME):$(TAG)
$(DOCKER) push $(IMAGE_NAME):$(VERSION)
.PHONY: docker-buildx
# Build and push multi-platform images to Docker Hub (Alpine-based)
docker-buildx:
$(DOCKER) buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 \
-t $(IMAGE_NAME):$(TAG) \
-t $(IMAGE_NAME):$(VERSION) \
--push .
.PHONY: update-changelog
# Update the changelog file with the new version
update-changelog:
@sh -c '\
if [ -z "$(VERSION)" ]; then \
read -p "Enter release version (e.g., v1.0.0): " VERSION; \
else \
VERSION="$(VERSION)"; \
fi; \
DATE=$$(date +%Y-%m-%d); \
if ! grep -q "## \[$$VERSION\]" CHANGELOG.md; then \
echo "Updating CHANGELOG.md with version $$VERSION ($$DATE)"; \
sed -i "" "s/^# Changelog/# Changelog\n\n## [$$VERSION] - $$DATE\n\n### Added\n- Latest release of DOSync\n- See previous releases for full feature list/" CHANGELOG.md; \
else \
echo "Version $$VERSION already exists in CHANGELOG.md"; \
fi; \
'
.PHONY: release
# Complete release process - tag, build binaries, build Docker images, create GitHub release with assets
release: update-changelog
@sh -c '\
if [ -z "$(VERSION)" ]; then \
read -p "Enter release version (e.g., v1.0.0): " VERSION; \
else \
VERSION="$(VERSION)"; \
fi; \
export VERSION="$$VERSION"; \
echo "π Creating release $$VERSION..."; \
echo "-> Adding all changes to git..."; \
git add .; \
echo "-> Committing changes..."; \
git commit -m "Release $$VERSION" || echo "No changes to commit"; \
echo "-> Checking if tag $$VERSION exists..."; \
if git tag -l "$$VERSION" | grep -q "$$VERSION"; then \
echo " Tag $$VERSION already exists locally. Deleting..."; \
git tag -d "$$VERSION"; \
fi; \
echo "-> Creating tag $$VERSION..."; \
git tag "$$VERSION"; \
echo "-> Checking if tag exists on remote..."; \
if git ls-remote --tags origin | grep -q "refs/tags/$$VERSION"; then \
echo " Tag $$VERSION exists on remote. Forcing update..."; \
git push origin :refs/tags/"$$VERSION" || echo " Failed to delete remote tag, continuing..."; \
fi; \
echo "-> Pushing tag $$VERSION to remote..."; \
git push origin "$$VERSION" || (echo "FATAL: Failed to push tag, aborting release" && exit 1); \
echo "-> Pushing commits to remote..."; \
git push || echo " Failed to push commits, continuing..."; \
echo "-> Waiting for GitHub to register the tag..."; \
sleep 3; \
echo "π¦ Building and pushing Docker images..."; \
echo "-> Building multi-platform Docker images and pushing to Docker Hub..."; \
$(MAKE) docker-buildx VERSION="$$VERSION" || echo " Docker build/push failed, continuing..."; \
echo "π¦ Building platform binaries..."; \
$(MAKE) build-all VERSION="$$VERSION"; \
echo "π¦ Creating GitHub release..."; \
echo "-> Deleting release $$VERSION if it exists..."; \
gh release delete "$$VERSION" --yes 2>/dev/null || true; \
echo "-> Reading release notes from CHANGELOG.md..."; \
RELEASE_NOTES=$$(awk "/## \\[$$VERSION\\]/,/## \\[/{if(!/## \\[$$VERSION\\]/ && !/## \\[/){print}}" CHANGELOG.md | sed "/^$$/d"); \
if [ -z "$$RELEASE_NOTES" ]; then \
RELEASE_NOTES="Release $$VERSION"; \
fi; \
echo "-> Creating new release $$VERSION with notes from CHANGELOG.md..."; \
if ! gh release create "$$VERSION" --target main --title "$$VERSION" --notes "$$RELEASE_NOTES"; then \
echo "FATAL: Failed to create GitHub release. Aborting."; \
exit 1; \
fi; \
echo "-> Uploading assets to the release..."; \
for PLATFORM in "linux/amd64" "linux/arm64" "linux/armv7" "darwin/amd64" "darwin/arm64"; do \
BINARY_PATH="release/$${PLATFORM}/dosync"; \
ASSET_NAME="dosync-$${PLATFORM//\//-}"; \
if [ -f "$$BINARY_PATH" ]; then \
echo " Uploading $$ASSET_NAME..."; \
if ! gh release upload "$$VERSION" "$$BINARY_PATH#$$ASSET_NAME" --clobber; then \
echo " Warning: Failed to upload $$ASSET_NAME, continuing..."; \
fi; \
else \
echo " Warning: Binary $$BINARY_PATH not found, skipping."; \
fi; \
done; \
echo "β
Release process completed!"; \
'
.PHONY: release-assets
release-assets: update-changelog
@sh -c '\
if [ -z "$(VERSION)" ]; then \
read -p "Enter release version (e.g., v1.0.0): " VERSION; \
else \
VERSION="$(VERSION)"; \
fi; \
export VERSION="$$VERSION"; \
$(MAKE) build-all VERSION="$$VERSION"; \
echo "-> Verifying binaries exist before creating release..."; \
MISSING=0; \
for BINARY in "release/linux/amd64/dosync" "release/linux/arm64/dosync" "release/linux/armv7/dosync" "release/darwin/amd64/dosync" "release/darwin/arm64/dosync"; do \
if [ ! -f "$$BINARY" ]; then \
echo " Error: $$BINARY not found!"; \
MISSING=1; \
fi; \
done; \
if [ $$MISSING -eq 1 ]; then \
echo "FATAL: One or more binaries are missing. Aborting release."; \
exit 1; \
fi; \
echo "-> Creating GitHub release $$VERSION..."; \
CHANGELOG_SECTION=$$(sed -n "/## \[$$VERSION\]/,/## \[/p" CHANGELOG.md | sed $$'"$$/## \\\[.*$$/d"'); \
if [ -z "$$CHANGELOG_SECTION" ]; then \
RELEASE_NOTES="Release $$VERSION"; \
else \
RELEASE_NOTES="$$CHANGELOG_SECTION"; \
fi; \
if ! gh release create "$$VERSION" \
--title "$$VERSION" --notes "$$RELEASE_NOTES" \
release/linux/amd64/dosync#dosync-linux-amd64 \
release/linux/arm64/dosync#dosync-linux-arm64 \
release/linux/armv7/dosync#dosync-linux-armv7 \
release/darwin/amd64/dosync#dosync-darwin-amd64 \
release/darwin/arm64/dosync#dosync-darwin-arm64; then \
echo "FATAL: Failed to create GitHub release with assets. Aborting."; \
exit 1; \
fi; \
echo "β
Release $$VERSION created successfully with all assets!"; \
'
.PHONY: release-upload-assets
release-upload-assets: update-changelog
@sh -c '\
if [ -z "$(VERSION)" ]; then \
read -p "Enter release version (e.g., v1.0.0): " VERSION; \
else \
VERSION="$(VERSION)"; \
fi; \
export VERSION="$$VERSION"; \
$(MAKE) build-all VERSION="$$VERSION"; \
echo "-> Verifying GitHub release $$VERSION exists..."; \
if ! gh release view "$$VERSION" &>/dev/null; then \
echo "FATAL: GitHub release $$VERSION does not exist or cannot be accessed. Create it first."; \
exit 1; \
fi; \
echo "-> Verifying binaries exist before uploading..."; \
MISSING=0; \
for BINARY in "release/linux/amd64/dosync" "release/linux/arm64/dosync" "release/linux/armv7/dosync" "release/darwin/amd64/dosync" "release/darwin/arm64/dosync"; do \
if [ ! -f "$$BINARY" ]; then \
echo " Error: $$BINARY not found!"; \
MISSING=1; \
fi; \
done; \
if [ $$MISSING -eq 1 ]; then \
echo "FATAL: One or more binaries are missing. Aborting upload."; \
exit 1; \
fi; \
echo "-> Uploading assets to GitHub release $$VERSION..."; \
if ! gh release upload "$$VERSION" \
release/linux/amd64/dosync#dosync-linux-amd64 \
release/linux/arm64/dosync#dosync-linux-arm64 \
release/linux/armv7/dosync#dosync-linux-armv7 \
release/darwin/amd64/dosync#dosync-darwin-amd64 \
release/darwin/arm64/dosync#dosync-darwin-arm64 \
--clobber; then \
echo "FATAL: Failed to upload assets to GitHub release $$VERSION."; \
exit 1; \
fi; \
echo "β
Successfully uploaded all assets to GitHub release $$VERSION!"; \
'
.PHONY: docker-release
# Standalone Docker build and push for an existing release version
docker-release:
@sh -c '\
if [ -z "$(VERSION)" ]; then \
read -p "Enter release version (e.g., v1.0.0): " VERSION; \
else \
VERSION="$(VERSION)"; \
fi; \
export VERSION="$$VERSION"; \
echo "π Building and pushing Docker images for $$VERSION..."; \
echo "-> Building multi-platform Docker images and pushing to Docker Hub..."; \
$(MAKE) docker-buildx VERSION="$$VERSION" || (echo "FATAL: Docker build/push failed" && exit 1); \
echo "β
Docker images for $$VERSION successfully built and pushed!"; \
'