From 2f4c2250a8b6bb41502b68db99526d5ff79c3e79 Mon Sep 17 00:00:00 2001 From: Ronak Nathani <7279934+ronaknnathani@users.noreply.github.com> Date: Thu, 9 May 2024 15:29:13 -0400 Subject: [PATCH] Add github actions for test and release Everytime we want to release a new version of kubernetes-event-exporter, we need to build the binaries to be deployed. This change updates the existing GitHub actions to use https://goreleaser.com/ci/actions/ to build/test when code is pushed or a PR is created and create a release with binaries when a new tag is pushed. --- .github/workflows/release.yml | 60 +++++++++++++---------------------- .github/workflows/test.yml | 31 +++++++++--------- .gitignore | 2 ++ .goreleaser.yaml | 33 +++++++++++++++++++ 4 files changed, 72 insertions(+), 54 deletions(-) create mode 100644 .goreleaser.yaml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8000fe85..ad084eac 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,47 +1,31 @@ -name: Publish Docker image - +name: goreleaser on: - release: - types: [published] - + push: + # run only against tags + tags: + - "v*.*.*" +permissions: + contents: write jobs: - push_to_registries: - name: Push Docker image to multiple registries + goreleaser: runs-on: ubuntu-latest - permissions: - packages: write - contents: read steps: - - name: Check out the repo - uses: actions/checkout@v3 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - - name: Log in to the Container registry - uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + - name: Checkout + uses: actions/checkout@v4 with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + - name: Set up Go + uses: actions/setup-go@v5 with: - images: | - ghcr.io/${{ github.repository }} + go-version: '1.20' - - name: Build and push Docker images - uses: docker/build-push-action@v3 + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 with: - context: . - push: true - platforms: linux/amd64,linux/arm64 - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - build-args: | - VERSION=${{ steps.meta.outputs.version }} + distribution: goreleaser + version: latest + args: release --clean + + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3cd3a5cc..b474fbee 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,22 +1,21 @@ -name: Test -on: [push, pull_request] +name: build_and_test +on: + push: + pull_request: jobs: - build: - name: Build + goreleaser: runs-on: ubuntu-latest steps: + - name: Checkout + uses: actions/checkout@master - - name: Set up Go 1.20 - uses: actions/setup-go@v3 + - name: Setup Go + uses: actions/setup-go@v1 with: - go-version: "1.20" - id: go + go-version: '1.20' - - name: Check out code into the Go module directory - uses: actions/checkout@v3 - - - name: Build - run: go build -v . - - - name: Test - run: go test ./... + - name: GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + version: latest + args: release --snapshot --clean diff --git a/.gitignore b/.gitignore index 70332ee7..8732795f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /kubernetes-event-exporter *.code-workspace + +dist/* diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 00000000..f2960f9b --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,33 @@ +# Ref https://goreleaser.com + +version: 1 + +before: + hooks: + - go mod tidy + - go vet ./... + - go test -mod=mod -v ./... + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + +archives: + - format: tar.gz + name_template: >- + {{ .ProjectName }}- + {{- lower .Os }}- + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end }} + +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:"