This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
177 lines (154 loc) · 6.72 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
PROJECT=gsctl
ORGANISATION=giantswarm
BIN=$(PROJECT)
GOVERSION := 1.17
BUILDDATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
COMMITHASH := $(shell git rev-parse HEAD)
VERSION := $(shell (test -f VERSION && cat VERSION) || echo "")
SOURCE=$(shell find . -name '*.go')
USERID=$(shell id -u)
GROUPID=$(shell id -g)
ifndef GOOS
GOOS := $(shell go env GOOS)
endif
ifndef GOARCH
GOARCH := $(shell go env GOARCH)
endif
# binary to test with
TESTBIN := build/bin/${BIN}-${GOOS}-${GOARCH}
.PHONY: clean build test crosscompile
all: build
# install and execute packr in a way that works both locally and in CI
prebuild:
mkdir -p $(shell pwd)/go-build-cache
chown -R ${USERID}:${GROUPID} $(shell pwd)/go-build-cache
docker run --rm \
-v $(shell pwd):/go/src/github.com/$(ORGANISATION)/$(PROJECT) \
-v $(shell pwd)/go-build-cache:/.cache \
-e GOPATH=/go -e GOOS=linux -e GOARCH=amd64 \
-w /go/src/github.com/$(ORGANISATION)/$(PROJECT) \
--user ${USERID}:${GROUPID} \
golang:$(GOVERSION)-alpine /bin/sh -c "go install github.com/gobuffalo/packr/packr && packr"
# build binary for current platform
build: prebuild build/bin/$(BIN)-$(GOOS)-$(GOARCH)
# install binary for current platform (not expected to work on Win)
install: build
cp build/bin/$(BIN)-$(GOOS)-$(GOARCH) /usr/local/bin/$(BIN)
# build for all platforms
crosscompile: prebuild build/bin/$(BIN)-darwin-amd64 build/bin/$(BIN)-linux-amd64 build/bin/$(BIN)-windows-386 build/bin/$(BIN)-windows-amd64
# platform-specific build
build/bin/$(BIN)-darwin-amd64: $(SOURCE)
@mkdir -p build/bin
@mkdir -p go-build-cache
@echo "Commit hash: $(COMMITHASH)"
docker run --rm \
-v $(shell pwd):/go/src/github.com/$(ORGANISATION)/$(PROJECT) \
-v $(shell pwd)/go-build-cache:/.cache \
-e GOPATH=/go -e GOOS=darwin -e GOARCH=amd64 -e CGO_ENABLED=0 \
-w /go/src/github.com/$(ORGANISATION)/$(PROJECT) \
--user ${USERID}:${GROUPID} \
golang:$(GOVERSION)-alpine go build -a -installsuffix cgo -o build/bin/$(BIN)-darwin-amd64 \
-ldflags="-X github.com/giantswarm/gsctl/buildinfo.Version=$(VERSION) -X github.com/giantswarm/gsctl/buildinfo.BuildDate=$(BUILDDATE) -X github.com/giantswarm/gsctl/buildinfo.Commit=$(COMMITHASH)"
rm -rf go-build-cache
# platform-specific build for linux-amd64
# CGO disabled on purpose, to enable support for Docker containers, specifically those based on Alpine.
build/bin/$(BIN)-linux-amd64: $(SOURCE)
@mkdir -p build/bin
@mkdir -p go-build-cache
docker run --rm \
-v $(shell pwd):/go/src/github.com/$(ORGANISATION)/$(PROJECT) \
-v $(shell pwd)/go-build-cache:/.cache \
-e GOPATH=/go -e GOOS=linux -e GOARCH=amd64 -e CGO_ENABLED=0 \
-w /go/src/github.com/$(ORGANISATION)/$(PROJECT) \
--user ${USERID}:${GROUPID} \
golang:$(GOVERSION)-buster go build -a -o build/bin/$(BIN)-linux-amd64 \
-ldflags="-X github.com/giantswarm/gsctl/buildinfo.Version=$(VERSION) -X github.com/giantswarm/gsctl/buildinfo.BuildDate=$(BUILDDATE) -X github.com/giantswarm/gsctl/buildinfo.Commit=$(COMMITHASH)"
rm -rf go-build-cache
# platform-specific build
build/bin/$(BIN)-windows-386: $(SOURCE)
@mkdir -p build/bin
@mkdir -p go-build-cache
docker run --rm \
-v $(shell pwd):/go/src/github.com/$(ORGANISATION)/$(PROJECT) \
-v $(shell pwd)/go-build-cache:/.cache \
-e GOPATH=/go -e GOOS=windows -e GOARCH=386 -e CGO_ENABLED=0 \
-w /go/src/github.com/$(ORGANISATION)/$(PROJECT) \
--user ${USERID}:${GROUPID} \
golang:$(GOVERSION)-alpine go build -a -installsuffix cgo -o build/bin/$(BIN)-windows-386 \
-ldflags="-X github.com/giantswarm/gsctl/buildinfo.Version=$(VERSION) -X github.com/giantswarm/gsctl/buildinfo.BuildDate=$(BUILDDATE) -X github.com/giantswarm/gsctl/buildinfo.Commit=$(COMMITHASH)"
rm -rf go-build-cache
# platform-specific build
build/bin/$(BIN)-windows-amd64: $(SOURCE)
@mkdir -p build/bin
@mkdir -p go-build-cache
docker run --rm \
-v $(shell pwd):/go/src/github.com/$(ORGANISATION)/$(PROJECT) \
-v $(shell pwd)/go-build-cache:/.cache \
-e GOPATH=/go -e GOOS=windows -e GOARCH=amd64 -e CGO_ENABLED=0 \
-w /go/src/github.com/$(ORGANISATION)/$(PROJECT) \
--user ${USERID}:${GROUPID} \
golang:$(GOVERSION)-alpine go build -a -installsuffix cgo -o build/bin/$(BIN)-windows-amd64 \
-ldflags "-X 'github.com/giantswarm/gscliauth/config.Version=$(VERSION)' -X 'github.com/giantswarm/gscliauth/config.BuildDate=$(BUILDDATE)' -X 'github.com/giantswarm/gscliauth/config.Commit=$(COMMITHASH)'"
rm -rf go-build-cache
gotest:
go test -cover ./...
# run some tests
test: $(TESTBIN)
@${TESTBIN} >> /dev/null && echo "OK"
@${TESTBIN} help >> /dev/null && echo "OK"
@${TESTBIN} --help >> /dev/null && echo "OK"
@${TESTBIN} -h >> /dev/null && echo "OK"
@${TESTBIN} create --help >> /dev/null && echo "OK"
@${TESTBIN} info --help >> /dev/null && echo "OK"
@${TESTBIN} list --help >> /dev/null && echo "OK"
@${TESTBIN} login --help >> /dev/null && echo "OK"
@${TESTBIN} logout --help >> /dev/null && echo "OK"
@${TESTBIN} ping --help >> /dev/null && echo "OK"
# @${TESTBIN} ping >> /dev/null && echo "OK"
@${TESTBIN} info >> /dev/null && echo "OK"
# Create binary files for releases
bin-dist: crosscompile
@mkdir -p bin-dist
@# test if code signing certificate is available in ./certs/code-signing.p12
test -f ./certs/code-signing.p12
@# test if code signing bundle password is in $CODE_SIGNING_CERT_BUNDLE_PASSWORD
@ if [ "${CODE_SIGNING_CERT_BUNDLE_PASSWORD}" = "" ]; then \
echo 'Environment variable $$CODE_SIGNING_CERT_BUNDLE_PASSWORD not set'; \
exit 1; \
fi
for OS in darwin-amd64 linux-amd64; do \
mkdir -p build/$(BIN)-$(VERSION)-$$OS; \
cp README.md build/$(BIN)-$(VERSION)-$$OS/; \
cp LICENSE build/$(BIN)-$(VERSION)-$$OS/; \
cp build/bin/$(BIN)-$$OS build/$(BIN)-$(VERSION)-$$OS/$(BIN); \
cd build/; \
tar -cvzf ./$(BIN)-$(VERSION)-$$OS.tar.gz $(BIN)-$(VERSION)-$$OS; \
mv ./$(BIN)-$(VERSION)-$$OS.tar.gz ../bin-dist/; \
cd ..; \
done
@# little different treatment for windows,
@# becaue of .exe suffix and code signing
for OS in windows-386 windows-amd64; do \
mkdir -p build/$(BIN)-$(VERSION)-$$OS; \
cp README.md build/$(BIN)-$(VERSION)-$$OS/; \
cp LICENSE build/$(BIN)-$(VERSION)-$$OS/; \
docker run --rm -ti \
-v $(shell pwd)/certs:/mnt/certs \
-v $(shell pwd)/build:/mnt/binaries \
--user ${USERID}:${GROUPID} \
quay.io/giantswarm/signcode-util:latest \
sign \
-pkcs12 /mnt/certs/code-signing.p12 \
-n "gsctl" \
-i https://github.com/giantswarm/gsctl \
-in /mnt/binaries/bin/$(BIN)-$$OS \
-out /mnt/binaries/$(BIN)-$(VERSION)-$$OS/$(BIN).exe \
-pass $(CODE_SIGNING_CERT_BUNDLE_PASSWORD); \
cd build; \
zip $(BIN)-$(VERSION)-$$OS.zip $(BIN)-$(VERSION)-$$OS/*; \
mv ./$(BIN)-$(VERSION)-$$OS.zip ../bin-dist/; \
cd .. ; \
done
# remove generated stuff
clean:
rm -rf bin-dist build go-build-cache release ./gsctl