This commit is contained in:
Ryan McGuire 2025-01-05 11:13:23 -05:00
parent d3e7d3f30d
commit af8edad114
5 changed files with 129 additions and 2 deletions

85
.gitea/workflows/ci.yml Normal file
View File

@ -0,0 +1,85 @@
name: Build and Publish
on:
push:
tags: ["v*"]
env:
PACKAGE_NAME: ambient-local-exporter
BINARY_PATH: bin
BINARY_NAME: ambient-local-exporter
GO_MOD_PATH: gitea.libretechconsulting.com/rmcguire/ambient-local-exporter
GO_GIT_HOST: gitea.libretechconsulting.com
VER_PKG: gitea.libretechconsulting.com/rmcguire/go-app/pkg/config
VERSION: ${{ github.ref_name }}
PLATFORMS: linux/amd64 linux/arm64 darwin/amd64 darwin/arm64
DOCKER_REGISTRY: gitea.libretechconsulting.com
DOCKER_USER: rmcguire
DOCKER_REPO: rmcguire/ambient-local-exporter
DOCKER_IMG: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_REPO }}
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Go Environment
uses: actions/setup-go@v4
with:
go-version: '1.23'
- name: Build Binary
run: make build
- name: Upload Binaries to Generic Registry
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
run: |
for platform in $PLATFORMS; do
OS=$(echo $platform | cut -d/ -f1)
ARCH=$(echo $platform | cut -d/ -f2)
BINARY_FILE="${BINARY_PATH}/${PACKAGE_NAME}-${OS}-${ARCH}"
echo "Uploading $BINARY_FILE"
if [ -f "$BINARY_FILE" ]; then
curl -X PUT \
-H "Authorization: token ${API_TOKEN}" \
--upload-file "$BINARY_FILE" \
"${GITHUB_SERVER_URL}/api/packages/${GITHUB_REPOSITORY_OWNER}/generic/${PACKAGE_NAME}/${{ github.ref_name }}/${PACKAGE_NAME}-${OS}-${ARCH}"
else
echo "Error: Binary $BINARY_FILE not found."
exit 1
fi
done
- name: Run Go List
env:
TAG_NAME: ${{ github.ref_name }} # Use the pushed tag name
run: |
if [[ "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
GOPROXY=proxy.golang.org go list -m ${GO_GIT_HOST}/${GITHUB_REPOSITORY}@$TAG_NAME
else
echo "Error: Invalid tag format '$TAG_NAME'. Expected 'vX.X.X'."
exit 1
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Custom Registry
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ env.DOCKER_USER }}
password: ${{ secrets.API_TOKEN }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.DOCKER_IMG }}:${{ github.ref_name }}
build-args:
VER_PKG: ${{ env.VER_PKG }}.version
VERSION: ${{ github.ref_name }}

2
.gitignore vendored
View File

@ -22,3 +22,5 @@
go.work
.env
bin/*

View File

@ -5,14 +5,14 @@ ENV GO111MODULE=auto CGO_ENABLED=0 GOOS=linux
ARG GOPROXY
ARG GONOSUMDB=gitea.libretechconsulting.com
ARG VERSIONPATH=gitea.libretechconsulting.com/rmcguire/go-app/pkg/config.Version
ARG VER_PKG=gitea.libretechconsulting.com/rmcguire/go-app/pkg/config.Version
ARG VERSION=(devel)
COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./ /app
RUN go build -C . -v -ldflags "-extldflags '-static' -X ${VERSIONPATH}=${VERSION}" -o ambient-local-exporter .
RUN go build -C . -v -ldflags "-extldflags '-static' -X ${VER_PKG}=${VERSION}" -o ambient-local-exporter .
FROM alpine:latest
WORKDIR /app

39
Makefile Normal file
View File

@ -0,0 +1,39 @@
CMD_NAME := ambient-local-exporter
.PHONY: all test build docker install clean
VERSION ?= development # Default to "development" if VERSION is not set
PLATFORMS := linux/amd64 darwin/amd64 darwin/arm64
OUTPUT_DIR := bin
VER_PKG := gitea.libretechconsulting.com/rmcguire/go-app/pkg/config.Version
DOCKER_IMG := gitea.libretechconsulting.com/rmcguire/ambient-local-exporter
all: test build docker
test:
go test -v ./...
build: test
@echo "Building for platforms: $(PLATFORMS)"
@for platform in $(PLATFORMS); do \
OS=$$(echo $$platform | cut -d/ -f1); \
ARCH=$$(echo $$platform | cut -d/ -f2); \
OUTPUT="$(OUTPUT_DIR)/$(CMD_NAME)-$$OS-$$ARCH"; \
GOOS=$$OS GOARCH=$$ARCH go build -ldflags "-X $(VER_PKG)=$(VERSION)" -o $$OUTPUT; \
echo "Built $$OUTPUT"; \
done
go build -ldflags "-X $(VER_PKG)=$(VERSION)" -o bin/${CMD_NAME}
docker:
@echo "Building Docker image $(DOCKER_IMG):$(VERSION)"
docker build \
--build-arg VER_PKG=$(VER_PKG) \
--build-arg VERSION=$(VERSION) \
-t $(DOCKER_IMG):$(VERSION) .
docker push $(DOCKER_IMG):$(VERSION)
install:
go install -v -ldflags "-X $(VER_PKG)=$(VERSION)" .
clean:
rm -rf bin/${CMD_NAME}

View File

@ -7,3 +7,4 @@
- [ ] Helm Chart
- [ ] Gitea CI
- [ ] Update README
- [ ] Version flag