From af8edad1146989e2f2df4522117bd6b949666bcc Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Sun, 5 Jan 2025 11:13:23 -0500 Subject: [PATCH] Add CI --- .gitea/workflows/ci.yml | 85 +++++++++++++++++++++++++++++++++++++++++ .gitignore | 2 + Dockerfile | 4 +- Makefile | 39 +++++++++++++++++++ TODO.md | 1 + 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/ci.yml create mode 100644 Makefile diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..44769cf --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -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 }} diff --git a/.gitignore b/.gitignore index d43a39d..2620ce7 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ go.work .env + +bin/* diff --git a/Dockerfile b/Dockerfile index abd9a69..1d1afb7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d4bfca6 --- /dev/null +++ b/Makefile @@ -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} diff --git a/TODO.md b/TODO.md index ee35e07..5f5950f 100644 --- a/TODO.md +++ b/TODO.md @@ -7,3 +7,4 @@ - [ ] Helm Chart - [ ] Gitea CI - [ ] Update README +- [ ] Version flag