Skip to content

Commit bf830d1

Browse files
committed
Add release building.
1 parent fbc2c89 commit bf830d1

File tree

11 files changed

+238
-150
lines changed

11 files changed

+238
-150
lines changed

.go-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.14.12

Makefile

+54-27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
SHELL=/bin/bash
2-
COMMIT=$(shell git rev-parse --short HEAD)
3-
VERSION ?= $(shell head -n 1 VERSION 2> /dev/null || echo "0.0.0")
4-
BUILD=$(shell date +%FT%T%z)
5-
LDFLAGS=-w -s -X main.Version=${VERSION} -X main.Build=${BUILD}
6-
PACKAGE_PATH=./dev-tools/package/
7-
DOCKER_BUILD=@export DOCKER_CONTENT_TRUST=1 && export DOCKER_BUILDKIT=1 && docker build --build-arg COMMIT='$(COMMIT)' --build-arg VERSION='$(VERSION)' --build-arg LDFLAGS='$(LDFLAGS)' -f $(PACKAGE_PATH)Dockerfile
8-
2+
DEFAULT_VERSION=$(shell awk '/const defaultVersion/{print $$NF}' main.go | tr -d '"')
3+
TARGET_ARCH_386=x86
4+
TARGET_ARCH_amd64=x86_64
5+
TARGET_ARCH_arm64=arm64
6+
PLATFORMS ?= darwin/amd64 linux/386 linux/amd64 linux/arm64 windows/386 windows/amd64
7+
VERSION ?= ${DEFAULT_VERSION}
8+
9+
ifeq ($(SNAPSHOT),true)
10+
BUILD_VERSION=${VERSION}-SNAPSHOT
11+
else
12+
BUILD_VERSION=${VERSION}
13+
endif
14+
15+
PLATFORM_TARGETS=$(addprefix release-, $(PLATFORMS))
16+
LDFLAGS=-w -s -X main.Version=${BUILD_VERSION}
917
CMD_COLOR_ON=\033[32m\xE2\x9c\x93
1018
CMD_COLOR_OFF=\033[0m
1119

@@ -14,33 +22,16 @@ help: ## - Show help message
1422
@printf "${CMD_COLOR_ON} usage: make [target]\n\n${CMD_COLOR_OFF}"
1523
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | sed -e "s/^Makefile://" | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
1624

17-
.PHONY: rpm
18-
rpm: ## - Build x86_64 linux RPM
19-
@printf "${CMD_COLOR_ON} Build rpm\n${CMD_COLOR_OFF}"
20-
@${DOCKER_BUILD} --ssh default --target rpm -o ./ .
21-
22-
.PHONY: image
23-
image: ## - Build the elastic fleet docker images
24-
@printf "${CMD_COLOR_ON} Build the elastic fleet docker image\n${CMD_COLOR_OFF}"
25-
${DOCKER_BUILD} --ssh default --target fleet -t fleet .
26-
27-
.PHONY: run
28-
run: image ## - Run the smallest and secured golang docker image based on scratch
29-
@printf "${CMD_COLOR_ON} Run the elastic fleet docker image\n${CMD_COLOR_OFF}"
30-
@docker-compose -f ./dev-tools/package/docker-compose.yml up
31-
32-
3325
.PHONY: local
34-
local: ## - Build packages using local environment
26+
local: ## - Build local binary for local environment (bin/fleet-server)
3527
@printf "${CMD_COLOR_ON} Build binaries using local go installation\n${CMD_COLOR_OFF}"
36-
go build -ldflags="${LDFLAGS}" -o ./bin/fleet .
28+
go build -ldflags="${LDFLAGS}" -o ./bin/fleet-server .
3729
@printf "${CMD_COLOR_ON} Binaries in ./bin/\n${CMD_COLOR_OFF}"
3830

39-
4031
.PHONY: clean
4132
clean: ## - Clean up build artifacts
4233
@printf "${CMD_COLOR_ON} Clean up build artifacts\n${CMD_COLOR_OFF}"
43-
rm -rf ./bin/ *.rpm ./build/
34+
rm -rf ./bin/ ./build/
4435

4536
.PHONY: generate
4637
generate: ## - Generate schema models
@@ -106,6 +97,42 @@ junit-report: ## - Run the junit-report generation for all the out files generat
10697
@go get -v -u github.com/jstemmer/go-junit-report
10798
$(foreach file, $(wildcard build/*.out), go-junit-report > "${file}.xml" < ${file};)
10899

100+
##################################################
101+
# Release building targets
102+
##################################################
103+
104+
build/distributions:
105+
@mkdir -p build/distributions
106+
107+
.PHONY: $(PLATFORM_TARGETS)
108+
$(PLATFORM_TARGETS): release-%:
109+
$(eval $@_OS := $(firstword $(subst /, ,$(lastword $(subst release-, ,$@)))))
110+
$(eval $@_GO_ARCH := $(lastword $(subst /, ,$(lastword $(subst release-, ,$@)))))
111+
$(eval $@_ARCH := $(TARGET_ARCH_$($@_GO_ARCH)))
112+
GOOS=$($@_OS) GOARCH=$($@_GO_ARCH) go build -ldflags="${LDFLAGS}" -o build/binaries/fleet-server-$(VERSION)-$($@_OS)-$($@_ARCH)/fleet-server .
113+
@$(MAKE) OS=$($@_OS) ARCH=$($@_ARCH) package-target
114+
115+
.PHONY: package-target
116+
package-target: build/distributions
117+
ifeq ($(OS),windows)
118+
@cd build/binaries && zip -q -r ../distributions/fleet-server-$(VERSION)-$(OS)-$(ARCH).zip fleet-server-$(VERSION)-$(OS)-$(ARCH)
119+
@cd build/distributions && shasum -a 512 fleet-server-$(VERSION)-$(OS)-$(ARCH).zip > fleet-server-$(VERSION)-$(OS)-$(ARCH).zip.sha512
120+
else
121+
@tar -C build/binaries -zcf build/distributions/fleet-server-$(VERSION)-$(OS)-$(ARCH).tar.gz fleet-server-$(VERSION)-$(OS)-$(ARCH)
122+
@cd build/distributions && shasum -a 512 fleet-server-$(VERSION)-$(OS)-$(ARCH).tar.gz > fleet-server-$(VERSION)-$(OS)-$(ARCH).tar.gz.sha512
123+
endif
124+
125+
.PHONY: release
126+
release: $(PLATFORM_TARGETS) ## - Builds a release. Specify exact platform with PLATFORMS env.
127+
128+
.PHONY: release-manager-snapshot
129+
release-manager-snapshot: ## - Builds a snapshot release. The Go version defined in .go-version will be installed and used for the build.
130+
@$(MAKE) SNAPSHOT=true release-manager-release
131+
132+
.PHONY: release-manager-release
133+
release-manager-release: ## - Builds a snapshot release. The Go version defined in .go-version will be installed and used for the build.
134+
./dev-tools/run_with_go_ver $(MAKE) release
135+
109136
##################################################
110137
# Integration testing targets
111138
##################################################

cmd/fleet/main.go

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232

3333
const kPolicyThrottle = time.Millisecond * 5
3434

35-
var Version string
36-
3735
func checkErr(err error) {
3836
if err != nil && err != context.Canceled {
3937
panic(err)

dev-tools/common.bash

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#
2+
# File: common.bash
3+
#
4+
# Common bash routines.
5+
#
6+
7+
# Script directory:
8+
_sdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
9+
10+
# debug "msg"
11+
# Write a debug message to stderr.
12+
debug()
13+
{
14+
if [ "$VERBOSE" == "true" ]; then
15+
echo "DEBUG: $1" >&2
16+
fi
17+
}
18+
19+
# err "msg"
20+
# Write and error message to stderr.
21+
err()
22+
{
23+
echo "ERROR: $1" >&2
24+
}
25+
26+
# get_go_version
27+
# Read the project's Go version and return it in the GO_VERSION variable.
28+
# On failure it will exit.
29+
get_go_version() {
30+
GO_VERSION=$(cat "${_sdir}/../.go-version")
31+
if [ -z "$GO_VERSION" ]; then
32+
err "Failed to detect the project's Go version"
33+
exit 1
34+
fi
35+
}
36+
37+
# install_gimme
38+
# Install gimme to HOME/bin.
39+
install_gimme() {
40+
# Install gimme
41+
if [ ! -f "${HOME}/bin/gimme" ]; then
42+
mkdir -p ${HOME}/bin
43+
curl -sL -o ${HOME}/bin/gimme https://raw.githubusercontent.com/travis-ci/gimme/v1.1.0/gimme
44+
chmod +x ${HOME}/bin/gimme
45+
fi
46+
47+
GIMME="${HOME}/bin/gimme"
48+
debug "Gimme version $(${GIMME} version)"
49+
}
50+
51+
# setup_go_root "version"
52+
# This configures the Go version being used. It sets GOROOT and adds
53+
# GOROOT/bin to the PATH. It uses gimme to download the Go version if
54+
# it does not already exist in the ~/.gimme dir.
55+
setup_go_root() {
56+
local version=${1}
57+
58+
install_gimme
59+
60+
# Setup GOROOT and add go to the PATH.
61+
${GIMME} "${version}" > /dev/null
62+
source "${HOME}/.gimme/envs/go${version}.env" 2> /dev/null
63+
64+
debug "$(go version)"
65+
}
66+
67+
# setup_go_path "gopath"
68+
# This sets GOPATH and adds GOPATH/bin to the PATH.
69+
setup_go_path() {
70+
local gopath="${1}"
71+
if [ -z "$gopath" ]; then return; fi
72+
73+
# Setup GOPATH.
74+
export GOPATH="${gopath}"
75+
76+
# Add GOPATH to PATH.
77+
export PATH="${GOPATH}/bin:${PATH}"
78+
79+
debug "GOPATH=${GOPATH}"
80+
}
81+
82+
jenkins_setup() {
83+
: "${HOME:?Need to set HOME to a non-empty value.}"
84+
: "${WORKSPACE:?Need to set WORKSPACE to a non-empty value.}"
85+
86+
if [ -z ${GO_VERSION:-} ]; then
87+
get_go_version
88+
fi
89+
90+
# Setup Go.
91+
export GOPATH=${WORKSPACE}
92+
export PATH=${GOPATH}/bin:${PATH}
93+
eval "$(gvm ${GO_VERSION})"
94+
95+
# Workaround for Python virtualenv path being too long.
96+
export TEMP_PYTHON_ENV=$(mktemp -d)
97+
export PYTHON_ENV="${TEMP_PYTHON_ENV}/python-env"
98+
99+
# Write cached magefile binaries to workspace to ensure
100+
# each run starts from a clean slate.
101+
export MAGEFILE_CACHE="${WORKSPACE}/.magefile"
102+
}
103+
104+
docker_setup() {
105+
OS="$(uname)"
106+
case $OS in
107+
'Darwin')
108+
# Start the docker machine VM (ignore error if it's already running).
109+
docker-machine start default || true
110+
eval $(docker-machine env default)
111+
;;
112+
esac
113+
}

dev-tools/dependencies-report

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -x
5+
6+
SRCPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
7+
outfile=dependencies.csv
8+
9+
while :; do
10+
case $1 in
11+
--csv)
12+
if [ "$2" ]; then
13+
outfile=$2
14+
else
15+
echo "ERROR: --csv needs a non-empty argument"
16+
exit 1
17+
fi
18+
shift
19+
;;
20+
--csv=?*)
21+
outfile=${1#*=}
22+
;;
23+
--csv=)
24+
echo "ERROR: --csv needs a non-empty argument"
25+
exit 1
26+
;;
27+
*)
28+
break
29+
;;
30+
esac
31+
32+
shift
33+
done
34+
35+
go mod tidy
36+
go mod download
37+
go list -m -json all $@ | go run go.elastic.co/go-licence-detector \
38+
-includeIndirect \
39+
-rules "$SRCPATH/notice/rules.json" \
40+
-overrides "$SRCPATH/notice/overrides.json" \
41+
-noticeTemplate "$SRCPATH/notice/dependencies.csv.tmpl" \
42+
-noticeOut "$outfile" \
43+
-depsOut ""

dev-tools/integration/.env

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
VERSION=8.0.0-SNAPSHOT
21
ELASTICSEARCH_USERNAME=elastic
32
ELASTICSEARCH_PASSWORD=changeme
43
TEST_ELASTICSEARCH_HOSTS=localhost:9200
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{- define "depInfo" -}}
2+
{{- range $i, $dep := . }}
3+
{{ $dep.Name }},{{ $dep.URL }},{{ $dep.Version | canonicalVersion }},{{ $dep.Version | revision }},{{ $dep.LicenceType }},
4+
{{- end -}}
5+
{{- end -}}
6+
7+
name,url,version,revision,license,sourceURL{{ template "depInfo" .Direct }}{{ template "depInfo" .Indirect }}

dev-tools/package/Dockerfile

-101
This file was deleted.

0 commit comments

Comments
 (0)