Skip to content

Commit

Permalink
packaging: add make file to build and upload deb
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-spin committed Dec 6, 2024
1 parent 5e8a900 commit ab0d7de
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DEB_USER=<youruser>
DEB_TOKEN=<yourdebianregistrytoken>
# example gitea debian package registry:
DEB_REGISTRY=https://<yourgitea>/api/packages/<youruserororg>/debian/pool/bookworm/stable/upload
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
.env
ringlog
test.log
.vscode/settings.json
build/
# Editor
*~
.emacs.d/
*.swp
*.swo
.vim/
__*
55 changes: 55 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Load variables from .env if it exists
ifneq (,$(wildcard .env))
include .env
export $(shell sed 's/=.*//' .env)
endif

BINARY_NAME=ringlog
GO_MODULE=github.com/anti-spin/ringlog
GOOS=linux
GOARCH=amd64
CGO_ENABLED=0

# Get the latest git tag or commit hash
VERSION := $(shell git describe --tags --always --dirty)

BUILD_DIR=build
DEB_DIR=$(BUILD_DIR)/$(BINARY_NAME)_$(VERSION)_amd64

# Default target
all: clean package upload

# Build the binary with static linking
build:
@echo "Building $(BINARY_NAME) $(VERSION)..."
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) go build -ldflags="-X $(GO_MODULE)/cmd.version=$(VERSION)" -o $(BINARY_NAME)
@echo "Build completed: $(BINARY_NAME)"

# Clean the binary
clean:
@echo "Cleaning up..."
rm -f $(BINARY_NAME)
rm -rf $(BUILD_DIR)
@echo "Clean completed."

package-prepare: build
mkdir -p $(DEB_DIR)/DEBIAN
install -m 755 -D $(BINARY_NAME) $(DEB_DIR)/usr/local/bin/$(BINARY_NAME)
install -m 644 -D debian/$(BINARY_NAME).1 $(DEB_DIR)/usr/share/man/man1/$(BINARY_NAME).1
sed "s/@VERSION@/$(VERSION)/g" < debian/control.template > $(DEB_DIR)/DEBIAN/control
if [ -f debian/postinst ]; then install -m 755 -D debian/postinst $(DEB_DIR)/DEBIAN/postinst; fi
if [ -f debian/postrm ]; then install -m 755 -D debian/postrm $(DEB_DIR)/DEBIAN/postrm; fi

package: package-prepare
@echo "Packaging $(BINARY_NAME)..."
dpkg-deb --build $(DEB_DIR)
@echo "Created $(BINARY_NAME)_$(VERSION)_amd64.deb"

upload:
@echo "Checking for DEB_REGISTRY..."
@if [ -n "$(DEB_REGISTRY)" ]; then \
echo "Uploading to registry $(DEB_REGISTRY)"; \
./upload-deb.sh $(BUILD_DIR)/$(BINARY_NAME)_$(VERSION)_amd64.deb; \
else \
echo "DEB_REGISTRY not set, skipping upload"; \
fi
30 changes: 30 additions & 0 deletions upload-deb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

# read DEB_USER, DEB_TOKEN, and DEB_REGISTRY from .env file
set -a
. ./.env
set +a

if [ $# -eq 0 ]; then
echo "Error: Please provide a file path as an argument."
echo "Usage: $0 <file_path>"
exit 1
fi

file_path="$1"

if [ ! -f "$file_path" ]; then
echo "Error: File '$file_path' does not exist."
exit 1
fi

curl --user $DEB_USER:$DEB_TOKEN \
--upload-file $file_path \
$DEB_REGISTRY

# Check the curl exit status
if [ $? -eq 0 ]; then
echo "Uploaded to debian registry $DEB_REGISTRY"
else
echo "Error: File upload failed."
fi

0 comments on commit ab0d7de

Please sign in to comment.