From 0babb20d10b6e531a061c90d14968533b93fd0a1 Mon Sep 17 00:00:00 2001 From: Adam Stokes <51892+adam-stokes@users.noreply.github.com> Date: Wed, 31 Mar 2021 12:17:50 -0400 Subject: [PATCH] Build binaries via goreleaser (#977) This handles building for all supported architectures including running packr for embedding the binary files. This allows us to easily extend our release process for tagging official cli releases, building in various package formats and publishing to different package registries Signed-off-by: Adam Stokes <51892+adam-stokes@users.noreply.github.com> --- .gitignore | 1 + .goreleaser.yml | 37 +++++++++++++++++++++++ Makefile | 7 +++++ README.md | 14 +++++++++ cli/.gitignore | 1 + cli/Building.md | 10 ------- cli/Makefile | 15 ---------- cli/scripts/build-cli.sh | 64 ---------------------------------------- 8 files changed, 60 insertions(+), 89 deletions(-) create mode 100644 .goreleaser.yml delete mode 100644 cli/Building.md delete mode 100755 cli/scripts/build-cli.sh diff --git a/.gitignore b/.gitignore index 9cbd69f371..7204345470 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin outputs +dist/ diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000000..9d658ea8b4 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,37 @@ +# This is an example .goreleaser.yml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +env: + - GO111MODULE=on + +before: + hooks: + - go mod download + - go install github.com/gobuffalo/packr/v2/packr2 + - cd cli && packr2 + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + main: ./cli/main.go + binary: op +archives: + - replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' diff --git a/Makefile b/Makefile index 175c7ecca9..74086fc259 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,10 @@ +# Get current directory of a Makefile: https://stackoverflow.com/a/23324703 + +# Builds cli for all supported platforms +.PHONY: build +build: + goreleaser --snapshot --skip-publish --rm-dist + .PHONY: clean clean: clean-workspace clean-docker diff --git a/README.md b/README.md index 544b59c9d1..93110f0574 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,20 @@ This repository contains: - Redis - vSphere +## Building + +This project utilizes `goreleaser` to build the cli binaries for all supported +platforms. Please see [goreleaser installation](https://goreleaser.com/install/) +for instructions on making that available to you. + +Once `goreleaser` is installed building the cli is as follows: + +``` +$ make build +``` + +This will put the built distribution inside of `dist` in the current working directory. + ## Contributing ### pre-commit diff --git a/cli/.gitignore b/cli/.gitignore index 9aac782ebc..8cad3d3de9 100644 --- a/cli/.gitignore +++ b/cli/.gitignore @@ -1,2 +1,3 @@ op .github +packrd diff --git a/cli/Building.md b/cli/Building.md deleted file mode 100644 index 9e3473701e..0000000000 --- a/cli/Building.md +++ /dev/null @@ -1,10 +0,0 @@ -# Building the binary -We use Gobuffalo's Packr to embbed static resources into the binary, so for that reason we are installing `packr2` in the CI worker. - -The `build-cli.sh` script builds the binary based on a few environment variables specifying the target distribution: - -```shell -$ GOOS=("darwin" "linux" "windows") ./.ci/scripts/build-cli.sh -# and/or -$ GOARCH=("386" "amd64") ./.ci/scripts/build-cli.sh -``` diff --git a/cli/Makefile b/cli/Makefile index fd70ebd7ea..13b7ec7d03 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -4,23 +4,8 @@ ROOT_DIR:=$(CURDIR) include ../commons.mk TEST_TIMEOUT?=5m - -GO_IMAGE?='golang' -GO_VERSION='$(shell cat ../.go-version )' -GO_IMAGE_TAG?='stretch' -GOOS?='linux' -GOARCH?='amd64' LOG_LEVEL?=INFO -.PHONY: build -build: - docker run --rm \ - -v $(ROOT_DIR):/go/src/github.com/elastic/e2e-testing/cli \ - -w /go/src/github.com/elastic/e2e-testing/cli \ - -e TARGET_OS=$(GOOS) -e TARGET_ARCH=$(GOARCH) -e GO111MODULE=on \ - $(GO_IMAGE):$(GO_VERSION)-$(GO_IMAGE_TAG) \ - scripts/build-cli.sh - .PHONY: install install: go get -v -t ./... diff --git a/cli/scripts/build-cli.sh b/cli/scripts/build-cli.sh deleted file mode 100755 index bd62557af2..0000000000 --- a/cli/scripts/build-cli.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env bash - -## Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -## or more contributor license agreements. Licensed under the Elastic License; -## you may not use this file except in compliance with the Elastic License. - -set -exo pipefail - -readonly supportedOSS=("darwin" "linux" "windows") -readonly supportedArchs=("386" "amd64") - -arch="${TARGET_ARCH}" -extension="" -goos="${TARGET_OS}" -separator="" - -if [[ "${TARGET_OS}" != "" ]]; then - # GO_OS represents the Golang's supported Operative Systems. - # Possible values: darwin, linux, windows - readonly GO_OS="${TARGET_OS:-linux}" - if [[ ! " ${supportedOSS[@]} " =~ " ${GO_OS} " ]]; then - echo "It's not possible to build a binary for ${GO_OS}. Supported values: darwin, linux, windows" - exit 1 - fi - - goos="${GO_OS}" - if [[ "$GO_OS" == "windows" ]]; then - goos="win" - extension=".exe" - fi - - separator="-" -fi - -if [[ "${TARGET_ARCH}" != "" ]]; then - # GO_ARCH represents the Golang's supported Architectures. - # Possible values: 386, amd64 - readonly GO_ARCH="${TARGET_ARCH:-amd64}" - if [[ ! " ${supportedArchs[@]} " =~ " ${GO_ARCH} " ]]; then - echo "It's not possible to build a binary for ${GO_ARCH}. Supported values: 386, amd64" - exit 1 - fi - - arch="${GO_ARCH}" - if [[ "$GO_ARCH" == "amd64" ]]; then - arch="64" - fi - - separator="-" -fi - -readonly VERSION="$(cat ./VERSION.txt)" - -echo ">>> Installing Packr2" -go get github.com/gobuffalo/packr/v2/packr2@v2.7.1 - -echo ">>> Generating Packr boxes" -packr2 - -echo ">>> Building CLI" -GOOS=${GO_OS} GOARCH=${GO_ARCH} go build -v -o $(pwd)/.github/releases/download/${VERSION}/${goos}${arch}${separator}op${extension} - -echo ">>> Cleaning up Packr boxes" -packr2 clean