refactor: Replace build-dev.sh and buildpush-dev.sh with unified Makefile
- commit
- f6c0a0b6f0f11d9d275a7dc13dd286f2e0d0ae7c
- author
- bamen · · 2024-10-16 08:07 UTC
- parent
- f83daa0
- signature
- unsigned
3 files · +55 -102
M Makefile +55 -11
| @@ -1,19 +1,63 @@ | |||
| 1 | 1 | # Variables | |
| 2 | - | INSTALL_DIR = /usr/bin | |
| 3 | - | BINARY_NAME = gordon | |
| 2 | + | REPO := ghcr.io/bnema/gordon | |
| 3 | + | TAG := dev | |
| 4 | + | DIST_DIR := ./dist | |
| 5 | + | ENGINE := podman | |
| 6 | + | ||
| 7 | + | # Architectures | |
| 8 | + | ARCHS := amd64 arm64 | |
| 9 | + | ||
| 10 | + | # Phony targets | |
| 11 | + | .PHONY: all build build-push clean | |
| 12 | + | ||
| 4 | 13 | # Default target | |
| 5 | 14 | all: build | |
| 6 | 15 | ||
| 7 | - | # Build the Go binary | |
| 16 | + | # Build binaries | |
| 8 | 17 | build: | |
| 9 | - | go build -o $(BINARY_NAME) cmd/cli/main.go | |
| 18 | + | @echo "Building Go binaries..." | |
| 19 | + | @mkdir -p $(DIST_DIR) | |
| 20 | + | @rm -f $(DIST_DIR)/* | |
| 21 | + | @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(DIST_DIR)/gordon-linux-amd64 ./main.go | |
| 22 | + | @CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $(DIST_DIR)/gordon-linux-arm64 ./main.go | |
| 23 | + | @echo "Go binaries built successfully" | |
| 10 | 24 | ||
| 11 | - | # Install the binary to /usr/local/bin | |
| 12 | - | install: | |
| 13 | - | mv $(BINARY_NAME) $(INSTALL_DIR) | |
| 25 | + | # Build and push Docker images | |
| 26 | + | build-push: build | |
| 27 | + | @echo "Cleaning up dangling images..." | |
| 28 | + | @$(ENGINE) image prune -f | |
| 14 | 29 | ||
| 15 | - | # Clean up the built binary | |
| 16 | - | clean: | |
| 17 | - | rm -f $(BINARY_NAME) | |
| 30 | + | @echo "Building and pushing Docker images..." | |
| 31 | + | @for arch in $(ARCHS); do \ | |
| 32 | + | cp $(DIST_DIR)/gordon-linux-$$arch gordon; \ | |
| 33 | + | $(ENGINE) build -t $(REPO):$(TAG)-$$arch .; \ | |
| 34 | + | rm gordon; \ | |
| 35 | + | $(ENGINE) push $(REPO):$(TAG)-$$arch; \ | |
| 36 | + | done | |
| 37 | + | ||
| 38 | + | @echo "Removing existing manifest..." | |
| 39 | + | @$(ENGINE) manifest rm $(REPO):$(TAG) || true | |
| 40 | + | ||
| 41 | + | @echo "Creating multi-arch manifest..." | |
| 42 | + | @$(ENGINE) manifest create $(REPO):$(TAG) \ | |
| 43 | + | $(REPO):$(TAG)-amd64 \ | |
| 44 | + | $(REPO):$(TAG)-arm64 | |
| 18 | 45 | ||
| 19 | - | .PHONY: all build install clean⏎̸ | |
| 46 | + | @echo "Annotating arm64 image..." | |
| 47 | + | @$(ENGINE) manifest annotate $(REPO):$(TAG) \ | |
| 48 | + | $(REPO):$(TAG)-arm64 --arch arm64 --variant v8 | |
| 49 | + | ||
| 50 | + | @echo "Inspecting manifest..." | |
| 51 | + | @$(ENGINE) manifest inspect $(REPO):$(TAG) | |
| 52 | + | ||
| 53 | + | @echo "Pushing multi-arch manifest..." | |
| 54 | + | @$(ENGINE) manifest push --all $(REPO):$(TAG) | |
| 55 | + | ||
| 56 | + | @echo "Script completed successfully." | |
| 57 | + | ||
| 58 | + | # Clean up | |
| 59 | + | clean: | |
| 60 | + | @echo "Cleaning up..." | |
| 61 | + | @rm -rf $(DIST_DIR) | |
| 62 | + | @$(ENGINE) system prune -f | |
| 63 | + | @echo "Cleanup completed."⏎̸ | |
| @@ -1,19 +1,63 @@ | |||||
| 1 | # Variables | 1 | # Variables | ||
| 2 | - | INSTALL_DIR = /usr/bin | 2 | + | REPO := ghcr.io/bnema/gordon |
| 3 | - | BINARY_NAME = gordon | 3 | + | TAG := dev |
| 4 | + | DIST_DIR := ./dist | |||
| 5 | + | ENGINE := podman | |||
| 6 | + | ||||
| 7 | + | # Architectures | |||
| 8 | + | ARCHS := amd64 arm64 | |||
| 9 | + | ||||
| 10 | + | # Phony targets | |||
| 11 | + | .PHONY: all build build-push clean | |||
| 12 | + | ||||
| 4 | # Default target | 13 | # Default target | ||
| 5 | all: build | 14 | all: build | ||
| 6 | 15 | ||||
| 7 | - | # Build the Go binary | 16 | + | # Build binaries |
| 8 | build: | 17 | build: | ||
| 9 | - | go build -o $(BINARY_NAME) cmd/cli/main.go | 18 | + | @echo "Building Go binaries..." |
| 19 | + | @mkdir -p $(DIST_DIR) | |||
| 20 | + | @rm -f $(DIST_DIR)/* | |||
| 21 | + | @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(DIST_DIR)/gordon-linux-amd64 ./main.go | |||
| 22 | + | @CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $(DIST_DIR)/gordon-linux-arm64 ./main.go | |||
| 23 | + | @echo "Go binaries built successfully" | |||
| 10 | 24 | ||||
| 11 | - | # Install the binary to /usr/local/bin | 25 | + | # Build and push Docker images |
| 12 | - | install: | 26 | + | build-push: build |
| 13 | - | mv $(BINARY_NAME) $(INSTALL_DIR) | 27 | + | @echo "Cleaning up dangling images..." |
| 28 | + | @$(ENGINE) image prune -f | |||
| 14 | 29 | ||||
| 15 | - | # Clean up the built binary | 30 | + | @echo "Building and pushing Docker images..." |
| 16 | - | clean: | 31 | + | @for arch in $(ARCHS); do \ |
| 17 | - | rm -f $(BINARY_NAME) | 32 | + | cp $(DIST_DIR)/gordon-linux-$$arch gordon; \ |
| 33 | + | $(ENGINE) build -t $(REPO):$(TAG)-$$arch .; \ | |||
| 34 | + | rm gordon; \ | |||
| 35 | + | $(ENGINE) push $(REPO):$(TAG)-$$arch; \ | |||
| 36 | + | done | |||
| 37 | + | ||||
| 38 | + | @echo "Removing existing manifest..." | |||
| 39 | + | @$(ENGINE) manifest rm $(REPO):$(TAG) || true | |||
| 40 | + | ||||
| 41 | + | @echo "Creating multi-arch manifest..." | |||
| 42 | + | @$(ENGINE) manifest create $(REPO):$(TAG) \ | |||
| 43 | + | $(REPO):$(TAG)-amd64 \ | |||
| 44 | + | $(REPO):$(TAG)-arm64 | |||
| 18 | 45 | ||||
| 19 | - | .PHONY: all build install clean⏎̸ | 46 | + | @echo "Annotating arm64 image..." |
| 47 | + | @$(ENGINE) manifest annotate $(REPO):$(TAG) \ | |||
| 48 | + | $(REPO):$(TAG)-arm64 --arch arm64 --variant v8 | |||
| 49 | + | ||||
| 50 | + | @echo "Inspecting manifest..." | |||
| 51 | + | @$(ENGINE) manifest inspect $(REPO):$(TAG) | |||
| 52 | + | ||||
| 53 | + | @echo "Pushing multi-arch manifest..." | |||
| 54 | + | @$(ENGINE) manifest push --all $(REPO):$(TAG) | |||
| 55 | + | ||||
| 56 | + | @echo "Script completed successfully." | |||
| 57 | + | ||||
| 58 | + | # Clean up | |||
| 59 | + | clean: | |||
| 60 | + | @echo "Cleaning up..." | |||
| 61 | + | @rm -rf $(DIST_DIR) | |||
| 62 | + | @$(ENGINE) system prune -f | |||
| 63 | + | @echo "Cleanup completed."⏎̸ | |||
D build-dev.sh +0 -20
| @@ -1,20 +0,0 @@ | |||
| 1 | - | #!/bin/bash | |
| 2 | - | ||
| 3 | - | set -e | |
| 4 | - | ||
| 5 | - | REPO="ghcr.io/bnema/gordon" | |
| 6 | - | TAG="dev" | |
| 7 | - | DIST_DIR="./dist" | |
| 8 | - | ||
| 9 | - | # Ensure dist directory exists | |
| 10 | - | mkdir -p $DIST_DIR | |
| 11 | - | ||
| 12 | - | # Clean up previous builds | |
| 13 | - | rm -f $DIST_DIR/* | |
| 14 | - | ||
| 15 | - | # Build Go binaries for multiple platforms with CGO_ENABLED=0 | |
| 16 | - | echo "Building Go binaries..." | |
| 17 | - | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $DIST_DIR/gordon-linux-amd64 ./main.go | |
| 18 | - | CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $DIST_DIR/gordon-linux-arm64 ./main.go | |
| 19 | - | ||
| 20 | - | echo "Successfully built Go binaries" | |
| @@ -1,20 +0,0 @@ | |||||
| 1 | - | #!/bin/bash | |||
| 2 | - | ||||
| 3 | - | set -e | |||
| 4 | - | ||||
| 5 | - | REPO="ghcr.io/bnema/gordon" | |||
| 6 | - | TAG="dev" | |||
| 7 | - | DIST_DIR="./dist" | |||
| 8 | - | ||||
| 9 | - | # Ensure dist directory exists | |||
| 10 | - | mkdir -p $DIST_DIR | |||
| 11 | - | ||||
| 12 | - | # Clean up previous builds | |||
| 13 | - | rm -f $DIST_DIR/* | |||
| 14 | - | ||||
| 15 | - | # Build Go binaries for multiple platforms with CGO_ENABLED=0 | |||
| 16 | - | echo "Building Go binaries..." | |||
| 17 | - | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $DIST_DIR/gordon-linux-amd64 ./main.go | |||
| 18 | - | CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $DIST_DIR/gordon-linux-arm64 ./main.go | |||
| 19 | - | ||||
| 20 | - | echo "Successfully built Go binaries" | |||
D buildpush-dev.sh +0 -71
| @@ -1,71 +0,0 @@ | |||
| 1 | - | #!/bin/bash | |
| 2 | - | set -e | |
| 3 | - | ENGINE="podman" | |
| 4 | - | REPO="ghcr.io/bnema/gordon" | |
| 5 | - | TAG="dev" | |
| 6 | - | DIST_DIR="./dist" | |
| 7 | - | ||
| 8 | - | # Function to handle errors | |
| 9 | - | handle_error() { | |
| 10 | - | echo "Error occurred. Cleaning up..." | |
| 11 | - | $ENGINE system prune -f | |
| 12 | - | exit 1 | |
| 13 | - | } | |
| 14 | - | ||
| 15 | - | # Set up error handling | |
| 16 | - | trap 'handle_error' ERR | |
| 17 | - | ||
| 18 | - | # Ensure dist directory exists | |
| 19 | - | mkdir -p $DIST_DIR | |
| 20 | - | ||
| 21 | - | # Build Go binaries for multiple platforms with CGO_ENABLED=0 | |
| 22 | - | echo "Building Go binaries..." | |
| 23 | - | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $DIST_DIR/gordon-linux-amd64 ./main.go | |
| 24 | - | CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $DIST_DIR/gordon-linux-arm64 ./main.go | |
| 25 | - | ||
| 26 | - | # Clean up any dangling images before building | |
| 27 | - | echo "Cleaning up dangling images..." | |
| 28 | - | $ENGINE image prune -f | |
| 29 | - | ||
| 30 | - | # Build Docker images for each architecture | |
| 31 | - | echo "Building Docker images..." | |
| 32 | - | for ARCH in amd64 arm64; do | |
| 33 | - | # Copy the appropriate binary to the root directory and rename it to 'gordon' | |
| 34 | - | cp $DIST_DIR/gordon-linux-$ARCH gordon | |
| 35 | - | ||
| 36 | - | # Build the Docker image | |
| 37 | - | $ENGINE build -t $REPO:${TAG}-$ARCH . | |
| 38 | - | ||
| 39 | - | # Remove the temporary 'gordon' file | |
| 40 | - | rm gordon | |
| 41 | - | done | |
| 42 | - | ||
| 43 | - | # Push images | |
| 44 | - | echo "Pushing Docker images..." | |
| 45 | - | $ENGINE push $REPO:${TAG}-amd64 | |
| 46 | - | $ENGINE push $REPO:${TAG}-arm64 | |
| 47 | - | ||
| 48 | - | # Remove existing manifest if it exists | |
| 49 | - | echo "Removing existing manifest..." | |
| 50 | - | $ENGINE manifest rm $REPO:$TAG || true | |
| 51 | - | ||
| 52 | - | # Create multi-arch manifest | |
| 53 | - | echo "Creating multi-arch manifest..." | |
| 54 | - | $ENGINE manifest create $REPO:$TAG \ | |
| 55 | - | $REPO:${TAG}-amd64 \ | |
| 56 | - | $REPO:${TAG}-arm64 | |
| 57 | - | ||
| 58 | - | # Annotate the arm64 image with variant information | |
| 59 | - | echo "Annotating arm64 image..." | |
| 60 | - | $ENGINE manifest annotate $REPO:$TAG \ | |
| 61 | - | $REPO:${TAG}-arm64 --arch arm64 --variant v8 | |
| 62 | - | ||
| 63 | - | # Debug: List manifests | |
| 64 | - | echo "Listing manifests..." | |
| 65 | - | $ENGINE manifest inspect $REPO:$TAG | |
| 66 | - | ||
| 67 | - | # Push multi-arch manifest | |
| 68 | - | echo "Pushing multi-arch manifest..." | |
| 69 | - | $ENGINE manifest push --all $REPO:$TAG | |
| 70 | - | ||
| 71 | - | echo "Script completed successfully." | |
| @@ -1,71 +0,0 @@ | |||||
| 1 | - | #!/bin/bash | |||
| 2 | - | set -e | |||
| 3 | - | ENGINE="podman" | |||
| 4 | - | REPO="ghcr.io/bnema/gordon" | |||
| 5 | - | TAG="dev" | |||
| 6 | - | DIST_DIR="./dist" | |||
| 7 | - | ||||
| 8 | - | # Function to handle errors | |||
| 9 | - | handle_error() { | |||
| 10 | - | echo "Error occurred. Cleaning up..." | |||
| 11 | - | $ENGINE system prune -f | |||
| 12 | - | exit 1 | |||
| 13 | - | } | |||
| 14 | - | ||||
| 15 | - | # Set up error handling | |||
| 16 | - | trap 'handle_error' ERR | |||
| 17 | - | ||||
| 18 | - | # Ensure dist directory exists | |||
| 19 | - | mkdir -p $DIST_DIR | |||
| 20 | - | ||||
| 21 | - | # Build Go binaries for multiple platforms with CGO_ENABLED=0 | |||
| 22 | - | echo "Building Go binaries..." | |||
| 23 | - | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $DIST_DIR/gordon-linux-amd64 ./main.go | |||
| 24 | - | CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $DIST_DIR/gordon-linux-arm64 ./main.go | |||
| 25 | - | ||||
| 26 | - | # Clean up any dangling images before building | |||
| 27 | - | echo "Cleaning up dangling images..." | |||
| 28 | - | $ENGINE image prune -f | |||
| 29 | - | ||||
| 30 | - | # Build Docker images for each architecture | |||
| 31 | - | echo "Building Docker images..." | |||
| 32 | - | for ARCH in amd64 arm64; do | |||
| 33 | - | # Copy the appropriate binary to the root directory and rename it to 'gordon' | |||
| 34 | - | cp $DIST_DIR/gordon-linux-$ARCH gordon | |||
| 35 | - | ||||
| 36 | - | # Build the Docker image | |||
| 37 | - | $ENGINE build -t $REPO:${TAG}-$ARCH . | |||
| 38 | - | ||||
| 39 | - | # Remove the temporary 'gordon' file | |||
| 40 | - | rm gordon | |||
| 41 | - | done | |||
| 42 | - | ||||
| 43 | - | # Push images | |||
| 44 | - | echo "Pushing Docker images..." | |||
| 45 | - | $ENGINE push $REPO:${TAG}-amd64 | |||
| 46 | - | $ENGINE push $REPO:${TAG}-arm64 | |||
| 47 | - | ||||
| 48 | - | # Remove existing manifest if it exists | |||
| 49 | - | echo "Removing existing manifest..." | |||
| 50 | - | $ENGINE manifest rm $REPO:$TAG || true | |||
| 51 | - | ||||
| 52 | - | # Create multi-arch manifest | |||
| 53 | - | echo "Creating multi-arch manifest..." | |||
| 54 | - | $ENGINE manifest create $REPO:$TAG \ | |||
| 55 | - | $REPO:${TAG}-amd64 \ | |||
| 56 | - | $REPO:${TAG}-arm64 | |||
| 57 | - | ||||
| 58 | - | # Annotate the arm64 image with variant information | |||
| 59 | - | echo "Annotating arm64 image..." | |||
| 60 | - | $ENGINE manifest annotate $REPO:$TAG \ | |||
| 61 | - | $REPO:${TAG}-arm64 --arch arm64 --variant v8 | |||
| 62 | - | ||||
| 63 | - | # Debug: List manifests | |||
| 64 | - | echo "Listing manifests..." | |||
| 65 | - | $ENGINE manifest inspect $REPO:$TAG | |||
| 66 | - | ||||
| 67 | - | # Push multi-arch manifest | |||
| 68 | - | echo "Pushing multi-arch manifest..." | |||
| 69 | - | $ENGINE manifest push --all $REPO:$TAG | |||
| 70 | - | ||||
| 71 | - | echo "Script completed successfully." | |||