From e31bbea2e02b0a388b223fff34c6997f73f497df Mon Sep 17 00:00:00 2001 From: Christopher Collins Date: Wed, 12 May 2021 10:32:59 -1000 Subject: [PATCH] Adds goReleaser for release creation and binary builds Adds [goReleaser](https://goreleaser.com/) support for creating binary builds and releases in a standardized way. Maintainers need to generate a Github token with the `repo` scope, and save it in `~/.config/goreleaser/token`. Then builds can be created by running `make build`, and a release can be created from the most recent tag by running `make release`. Binaries will be built, a release created in Github, and the binaries added to a *.tar.gz file with any README.md and LICENSE files. An sha256sum.txt file is generated for each of the artifacts, and added to the release. Signed-off-by: Christopher Collins --- .github/workflows/validate_goreleaser.yml | 33 ++++++++++++++++ .gitignore | 4 ++ .goreleaser.yml | 48 +++++++++++++++++++++++ Dockerfile | 4 +- Makefile | 28 +++++++++---- 5 files changed, 107 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/validate_goreleaser.yml create mode 100644 .goreleaser.yml diff --git a/.github/workflows/validate_goreleaser.yml b/.github/workflows/validate_goreleaser.yml new file mode 100644 index 00000000..d5b52425 --- /dev/null +++ b/.github/workflows/validate_goreleaser.yml @@ -0,0 +1,33 @@ +# Validate the goReleaser configuration + +name: validate_goreleaser + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + validate_goreleaser: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2.3.3 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2.1.3 + with: + go-version: 1.15.2 + + - name: Download GoReleaser + run: | + curl -sSLf https://github.com/goreleaser/goreleaser/releases/latest/download/goreleaser_Linux_x86_64.tar.gz -o - | tar --extract --gunzip --directory /usr/local/bin goreleaser + - name: Check goreleaser validity + run: | + goreleaser check diff --git a/.gitignore b/.gitignore index 2458929c..2838deeb 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ bin/ # MacOS .DS_Store + +# goReleaser +dist/ +bin/ diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 00000000..78f105de --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,48 @@ +# This is an example .goreleaser.yml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +env_files: + github_token: ~/.config/goreleaser/token + +before: + hooks: + # You may remove this if you don't use go modules. + - go mod tidy + # you may remove this if you don't need go generate + # - go generate ./... + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + ldflags: + - -s -w -X 'github.com/openshift/osdctl/cmd.GitCommit={{.ShortCommit}}' + +archives: + - replacements: + darwin: Darwin + linux: Linux + amd64: x86_64 + +checksum: + name_template: 'sha256sum.txt' + algorithm: sha256 + +snapshot: + name_template: "{{ .Tag }}-next" + +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + +release: + github: + owner: "openshift" + name: "osdctl" + prerelease: auto diff --git a/Dockerfile b/Dockerfile index ee7428ee..0bd01128 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,12 +2,12 @@ FROM registry.svc.ci.openshift.org/openshift/release:golang-1.14 WORKDIR /src COPY . . -RUN make build +RUN make ci-build FROM registry.access.redhat.com/ubi8/ubi-minimal:latest LABEL io.openshift.managed.name="osdctl" \ io.openshift.managed.description="OSD related command line utilities" -COPY --from=0 /src/bin/osdctl /bin/osdctl +COPY --from=0 /src/dist/osdctl_linux_amd64/osdctl /bin/osdctl ENTRYPOINT ["bin/osdctl"] diff --git a/Makefile b/Makefile index bcdb1e7b..27a61aa7 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,33 @@ -REPOSITORY = $(shell go list -m) -GIT_COMMIT = $(shell git rev-parse --short HEAD) - BUILDFLAGS ?= -LDFLAGS = -ldflags="-X '${REPOSITORY}/cmd.GitCommit=${GIT_COMMIT}'" +GORELEASER_BUILD_ARGS = "--rm-dist" unexport GOFLAGS all: format mod build test -format: vet fmt mockgen docs +format: vet fmt mockgen ci-build docs fmt: @echo "gofmt" @gofmt -w -s . @git diff --exit-code . +.PHONY: download-goreleaser +download-goreleaser: + mkdir -p ./bin && curl -sSLf https://github.com/goreleaser/goreleaser/releases/latest/download/goreleaser_Linux_x86_64.tar.gz -o - | tar --extract --gunzip --directory ./bin goreleaser + +# Need to use --snapshot here because the goReleaser +# requires more git info that is provided in Prow's clone. +# Snapshot allows the build without validation of the +# repository itself +.PHONY: ci-build +ci-build: download-goreleaser + ./bin/goreleaser build $(GORELEASER_BUILD_ARGS) --snapshot + build: - go build ${BUILDFLAGS} ${LDFLAGS} -o ./bin/osdctl main.go + goreleaser build $(GORELEASER_BUILD_ARGS) + +release: + goreleaser release --rm-dist vet: go vet ${BUILDFLAGS} ./... @@ -24,8 +36,8 @@ mod: go mod tidy @git diff --exit-code -- go.mod -docs: build - ./bin/osdctl docs ./docs/command +docs: + ./dist/osdctl_$(shell uname | tr [:upper:] [:lower:])_amd64/osdctl ./docs/command @git diff --exit-code -- ./docs/command/ mockgen: ensure-mockgen