From 30e96a03a9255e1d2e26fa00857fc56fd0f77cec Mon Sep 17 00:00:00 2001 From: Charles Guertin Date: Mon, 7 Mar 2022 23:32:55 -0500 Subject: [PATCH] Major/GitHub actions setup (#7) * Base workflows files created, need to be filled * Add build stage * Fix build stage * Added test CI, makefile test cmd * Modified release stage. * Output go version * Fix tests step * Fix typos * Edit test stage with codecov. * Fix test codecov step * Added coverage badge. * Fix release stage. * Add goreleaser config * Add quay readme badge * Add version command, edit CI * Fixed dockerfile ldflags. * Fix missing docker args * Fix ldflags dockerfile. * Update readme --- .github/workflows/build.yml | 22 ++++++++++++ .github/workflows/goreleaser.yml | 35 ++++++++++++++++++++ .github/workflows/release.yml | 57 ++++++++++++++++++++++++++++++++ .github/workflows/tests.yml | 32 ++++++++++++++++++ .gitignore | 3 +- .goreleaser.yml | 30 +++++++++++++++++ Dockerfile | 12 +++++-- Makefile | 8 +++-- README.md | 4 ++- cmd/version.go | 38 +++++++++++++++++++++ 10 files changed, 235 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/goreleaser.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/tests.yml create mode 100644 .goreleaser.yml create mode 100644 cmd/version.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..274546a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,22 @@ +name: Build app + +on: [push, pull_request] + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '^1.17.8' + + - name: Show Go version + run: go version + + - name: Check out source code + uses: actions/checkout@v1 + + - name: Build + run: make build \ No newline at end of file diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml new file mode 100644 index 0000000..23cec15 --- /dev/null +++ b/.github/workflows/goreleaser.yml @@ -0,0 +1,35 @@ +name: Release binaries + +on: + push: + tags: + - '*' + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Fetch all tags + run: git fetch --force --tags + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '^1.17.8' + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + distribution: goreleaser + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..2a3face --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,57 @@ +name: Release app + +on: + push: + tags: + - '*' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Prepare + id: prep + run: | + VERSION=$(git describe --tags $(git rev-list --tags --max-count=1)) + GIT_COMMIT=$(git rev-parse --short HEAD) + BUILD_DATE=$(date '+%FT%TZ') + echo ::set-output name=version::${VERSION} + echo ::set-output name=git_commit::${GIT_COMMIT} + echo ::set-output name=build_date::${BUILD_DATE} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + with: + platforms: all + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + with: + install: true + version: latest + + - name: Login to Quay + uses: docker/login-action@v1 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_ROBOT_TOKEN }} + + - name: Build and Push + uses: docker/build-push-action@v2 + with: + build-args: | + VERSION=${{ steps.prep.outputs.version }} + GIT_COMMIT=${{ steps.prep.outputs.git_commit }} + BUILD_DATE=${{ steps.prep.outputs.build_date }} + context: ./ + platforms: linux/amd64,linux/arm/v7,linux/arm64 + push: true + tags: | + quay.io/cguertin14/k3supdater:latest + quay.io/cguertin14/k3supdater:${{ github.sha }} + quay.io/cguertin14/k3supdater:${{ steps.prep.outputs.version }} \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..ecae750 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,32 @@ +name: Test app + +on: [push, pull_request] + +jobs: + build: + name: Test + runs-on: ubuntu-latest + steps: + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '^1.17.8' + + - name: Show Go version + run: go version + + - name: Check out source code + uses: actions/checkout@v1 + + - name: Run tests + run: make test + + - name: Upload Artifact + uses: actions/upload-artifact@v3 + with: + name: tests-results + path: coverage.html + retention-days: 5 + + - name: Upload coverage to Codecov + run: bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1700750..898f53f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ -k3supdater \ No newline at end of file +k3supdater +coverage.* diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..224fb98 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,30 @@ +project_name: k3supdater + +before: + hooks: + - go mod download + +builds: + - env: + - CGO_ENABLED=0 + + goos: + - linux + - windows + - darwin + + binary: "{{ .ProjectName }}" + + ldflags: + - -X github.com/cguertin14/k3supdater/cmd.BuildDate="{{ .Date }}" + - -X github.com/cguertin14/k3supdater/cmd.GitCommit="{{ .Commit }}" + - -X github.com/cguertin14/k3supdater/cmd.Version="{{ .Version }}" + + +changelog: + sort: asc + +release: + github: + owner: cguertin14 + name: k3supdater \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index ee245cb..b0fb803 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,11 +6,17 @@ LABEL maintainer="Charles Guertin " ARG TARGETOS ARG TARGETARCH ARG TARGETVARIANT="" +ARG BUILD_DATE +ARG VERSION +ARG GIT_COMMIT ENV CGO_ENABLED=0 \ GOOS=${TARGETOS} \ GOARCH=${TARGETARCH} \ - GOARM=${TARGETVARIANT} + GOARM=${TARGETVARIANT} \ + BUILD_DATE=${BUILD_DATE} \ + VERSION=${VERSION} \ + GIT_COMMIT=${GIT_COMMIT} RUN apk add --no-cache --update ca-certificates make @@ -20,7 +26,9 @@ COPY go.* ./ RUN go mod download COPY . ./ -RUN make build +RUN go build \ + -ldflags "-X github.com/cguertin14/k3supdater/cmd.Version=${VERSION} -X github.com/cguertin14/k3supdater/cmd.BuildDate=${BUILD_DATE} -X github.com/cguertin14/k3supdater/cmd.GitCommit=${GIT_COMMIT}" \ + -o ./k3supdater . # Add user & group RUN addgroup -S updater-group && \ diff --git a/Makefile b/Makefile index 9d111fb..d974747 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,14 @@ BIN_NAME = k3supdater default: setup setup: - go install github.com/golang/mock/mockgen@latest + @go install github.com/golang/mock/mockgen@latest build: - go build -o ./${BIN_NAME} . + @go build -o ./${BIN_NAME} . + +test: + @go test -v ./... -coverprofile coverage.out + go tool cover -html=coverage.out -o coverage.html generate-mock: @go generate -v ./... \ No newline at end of file diff --git a/README.md b/README.md index 2a1bda1..ab04c8e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # k3supdater [![Go Report Card](https://goreportcard.com/badge/github.com/cguertin14/k3supdater)](https://goreportcard.com/report/github.com/cguertin14/k3supdater) +[![codecov](https://codecov.io/gh/cguertin14/k3supdater/branch/main/graph/badge.svg?token=BUUUB7F5HX)](https://codecov.io/gh/cguertin14/k3supdater) +[![Docker Repository on Quay](https://quay.io/repository/cguertin14/k3supdater/status "Docker Repository on Quay")](https://quay.io/repository/cguertin14/k3supdater) Updater (similar to Renovate Bot) for k3s ansible playbook versions. @@ -12,7 +14,7 @@ First, you need to create a Github access token with write access to the reposit ## Usage -To use `k3supdater`, you'll need to download the binary (TODO: Add download link here) or compile it locally. +To use `k3supdater`, you'll need to [download the appropriate binary for your machine](https://github.com/cguertin14/k3supdater/releases) or compile it locally. It can also be used via docker, using the `quay.io/cguertin14/k3supdater` image. Then, you'll be able to run update commands like so: ```bash diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..94b0f18 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ( + // GitCommit + // The latest git commit hash + GitCommit string + + // BuildDate + // The date when the build was made + BuildDate string + + // Version + // The version of the app + Version string +) + +var ( + versionCmd = &cobra.Command{ + Use: "version", + Short: "Show k3supdater's installed version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf( + "Version: %s, GitCommit: %s, BuildDate: %s\n", + Version, GitCommit, BuildDate, + ) + }, + } +) + +func init() { + rootCmd.AddCommand(versionCmd) +}