From 95aa1e47d18b920d0dac56ef15657bdd7ebc9b7a Mon Sep 17 00:00:00 2001 From: Ryan McGuire Date: Sun, 30 Mar 2025 20:41:35 -0400 Subject: [PATCH] add ci --- .gitea/workflows/ci.yml | 133 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 .gitea/workflows/ci.yml diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..9ab915f --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,133 @@ +name: Build and Publish +on: + push: + tags: ["v*"] + branches: ["main"] + +env: + PACKAGE_NAME: go-http-server-with-otel + BINARY_PATH: bin + BINARY_NAME: go-http-server-with-otel + GO_MOD_PATH: gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel + GO_GIT_HOST: gitea.libretechconsulting.com + VER_PKG: gitea.libretechconsulting.com/rmcguire/go-app/pkg/config.Version + VERSION: ${{ github.ref_name }} + PLATFORMS: linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 + DOCKER_REGISTRY: gitea.libretechconsulting.com + DOCKER_USER: rmcguire + DOCKER_REPO: rmcguire/go-http-server-with-otel + DOCKER_IMG: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_REPO }} + CHART_DIR: helm/go-http-server-with-otel + +jobs: + release: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/v') # Only run on tag push + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - 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 + continue-on-error: true + 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@v3 + + - 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@v6 + with: + context: . + push: true + tags: | + ${{ env.DOCKER_IMG }}:${{ github.ref_name }} + ${{ env.DOCKER_IMG }}:latest + build-args: | + VER_PKG=${{ env.VER_PKG }} + VERSION=${{ github.ref_name }} + + # Detect if the helm chart was updated + check-chart: + runs-on: ubuntu-latest + outputs: + chart-updated: ${{ steps.filter.outputs.chart }} + steps: + - uses: actions/checkout@v4 + - name: Check Chart Changed + uses: dorny/paths-filter@v3 + id: filter + with: + base: ${{ github.ref }} + filters: | + chart: + - helm/go-http-server-with-otel/Chart.yaml + + helm-release: + runs-on: ubuntu-latest + needs: check-chart + if: ${{ needs.check-chart.outputs.chart-updated == 'true' }} + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Install Helm + env: + BINARY_NAME: helm + run: | + curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash + + - name: Package Chart + run: | + helm package --app-version ${VERSION} ${CHART_DIR} + + - name: Publish Chart + env: + API_TOKEN: ${{ secrets.API_TOKEN }} + run: | + curl -X POST \ + -H "Authorization: token ${API_TOKEN}" \ + --upload-file ./${PACKAGE_NAME}-*.tgz \ + https://gitea.libretechconsulting.com/api/packages/${GITHUB_REPOSITORY_OWNER}/helm/api/charts