Skip to content

Commit c57fd43

Browse files
Merge pull request #29 from gabriel-samfira/update-build-scripts
Update build scripts and add GetVersion
2 parents 7d26413 + 91ad0de commit c57fd43

File tree

7 files changed

+135
-15
lines changed

7 files changed

+135
-15
lines changed

.github/workflows/go-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ jobs:
2424
- run: go version
2525

2626
- name: Run GARM Go Tests
27-
run: make go-test
27+
run: make test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
bin/
2+
release/
3+
build/

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ USER root
55

66
RUN apk add musl-dev gcc libtool m4 autoconf g++ make libblkid util-linux-dev git linux-headers mingw-w64-gcc
77

8+
RUN wget http://musl.cc/aarch64-linux-musl-cross.tgz -O /tmp/aarch64-linux-musl-cross.tgz && \
9+
tar --strip-components=1 -C /usr/local -xzf /tmp/aarch64-linux-musl-cross.tgz && \
10+
rm /tmp/aarch64-linux-musl-cross.tgz
11+
812
ADD ./scripts/build-static.sh /build-static.sh
913
RUN chmod +x /build-static.sh
1014

Makefile

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@ ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
44
GOPATH ?= $(shell go env GOPATH)
55
GO ?= go
66

7-
IMAGE_TAG = garm-provider-azure-build
7+
IMAGE_TAG = garm-provider-build
88

99
USER_ID=$(shell ((docker --version | grep -q podman) && echo "0" || id -u))
1010
USER_GROUP=$(shell ((docker --version | grep -q podman) && echo "0" || id -g))
11+
GARM_PROVIDER_NAME := garm-provider-azure
1112

1213
default: build
1314

14-
.PHONY : build build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify
15+
.PHONY : build build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify create-release-files release
1516

1617
build:
1718
@$(GO) build .
1819

20+
clean: ## Clean up build artifacts
21+
@rm -rf ./bin ./build ./release
22+
1923
build-static:
2024
@echo Building
2125
docker build --tag $(IMAGE_TAG) .
22-
docker run --rm -e USER_ID=$(USER_ID) -e USER_GROUP=$(USER_GROUP) -v $(PWD):/build/garm-provider-azure:z $(IMAGE_TAG) /build-static.sh
23-
@echo Binaries are available in $(PWD)/bin
26+
mkdir -p build
27+
docker run --rm -e GARM_PROVIDER_NAME=$(GARM_PROVIDER_NAME) -e USER_ID=$(USER_ID) -e USER_GROUP=$(USER_GROUP) -v $(PWD)/build:/build/output:z -v $(PWD):/build/$(GARM_PROVIDER_NAME):z $(IMAGE_TAG) /build-static.sh
28+
@echo Binaries are available in $(PWD)/build
2429

25-
test: verify go-test
30+
test: install-lint-deps verify go-test
2631

2732
install-lint-deps:
2833
@$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@@ -37,13 +42,19 @@ fmt:
3742
@$(GO) fmt $$(go list ./...)
3843

3944
fmtcheck:
40-
@gofmt -l -s $$(go list ./... | sed -n 's/github.com\/cloudbase\/garm-provider-azure\/\(.*\)/\1/p') | grep ".*\.go"; if [ "$$?" -eq 0 ]; then echo "gofmt check failed; please tun gofmt -w -s"; exit 1;fi
45+
@gofmt -l -s $$(go list ./... | sed -n 's/github.com\/cloudbase\/'$(GARM_PROVIDER_NAME)'\/\(.*\)/\1/p') | grep ".*\.go"; if [ "$$?" -eq 0 ]; then echo "gofmt check failed; please tun gofmt -w -s"; exit 1;fi
4146

4247
verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date
4348
$(eval TMPDIR := $(shell mktemp -d))
4449
@cp -R ${ROOTDIR} ${TMPDIR}
45-
@(cd ${TMPDIR}/garm-provider-azure && ${GO} mod tidy)
46-
@diff -r -u -q ${ROOTDIR} ${TMPDIR}/garm-provider-azure >/dev/null 2>&1; if [ "$$?" -ne 0 ];then echo "please run: go mod tidy && go mod vendor"; exit 1; fi
50+
@(cd ${TMPDIR}/$(GARM_PROVIDER_NAME) && ${GO} mod tidy)
51+
@diff -r -u -q ${ROOTDIR} ${TMPDIR}/$(GARM_PROVIDER_NAME) >/dev/null 2>&1; if [ "$$?" -ne 0 ];then echo "please run: go mod tidy && go mod vendor"; exit 1; fi
4752
@rm -rf ${TMPDIR}
4853

4954
verify: verify-vendor lint fmtcheck
55+
56+
##@ Release
57+
create-release-files:
58+
./scripts/make-release.sh
59+
60+
release: build-static create-release-files ## Create a release

main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@ var signals = []os.Signal{
3030
syscall.SIGTERM,
3131
}
3232

33+
var (
34+
// Version is the version of the application
35+
Version = "v0.0.0-unknown"
36+
)
37+
3338
func main() {
39+
// This is an unofficial command. It will be added into future versions of the
40+
// external provider interface. For now we manually hardcode it here. This is not
41+
// used by GARM itself. It is informative for the user to be able to check the version
42+
// of the provider.
43+
garmCommand := os.Getenv("GARM_COMMAND")
44+
if garmCommand == "GetVersion" {
45+
fmt.Println(Version)
46+
os.Exit(0)
47+
}
48+
3449
ctx, stop := signal.NotifyContext(context.Background(), signals...)
3550
defer stop()
3651

scripts/build-static.sh

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
#!/bin/sh
22

3-
GARM_SOURCE="/build/garm-provider-azure"
4-
BIN_DIR="$GARM_SOURCE/bin"
5-
git config --global --add safe.directory "$GARM_SOURCE"
3+
GARM_PROVIDER_NAME=${GARM_PROVIDER_NAME:-garm-provider-azure}
4+
GARM_SOURCE="/build/$GARM_PROVIDER_NAME"
5+
git config --global --add safe.directory /build/$GARM_PROVIDER_NAME
6+
cd $GARM_SOURCE
7+
8+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
9+
if [ ! -z "$GARM_REF" ] && [ "$GARM_REF" != "$CURRENT_BRANCH" ];then
10+
git checkout $GARM_REF
11+
fi
12+
13+
cd $GARM_SOURCE
614

7-
[ ! -d "$BIN_DIR" ] && mkdir -p "$BIN_DIR"
15+
OUTPUT_DIR="/build/output"
16+
VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always)
17+
BUILD_DIR="$OUTPUT_DIR/$VERSION"
18+
19+
20+
[ ! -d "$BUILD_DIR/linux" ] && mkdir -p "$BUILD_DIR/linux"
21+
[ ! -d "$BUILD_DIR/windows" ] && mkdir -p "$BUILD_DIR/windows"
822

923
export CGO_ENABLED=1
1024
USER_ID=${USER_ID:-$UID}
1125
USER_GROUP=${USER_GROUP:-$(id -g)}
1226

27+
# Garm
1328
cd $GARM_SOURCE
14-
go build -mod vendor -o $BIN_DIR/garm-provider-azure -tags osusergo,netgo -ldflags "-linkmode external -extldflags '-static' -s -w" .
1529

16-
chown $USER_ID:$USER_GROUP -R "$BIN_DIR"
30+
# Linux
31+
GOOS=linux GOARCH=amd64 go build -mod vendor \
32+
-o $BUILD_DIR/linux/amd64/$GARM_PROVIDER_NAME \
33+
-tags osusergo,netgo,sqlite_omit_load_extension \
34+
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .
35+
GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build \
36+
-mod vendor \
37+
-o $BUILD_DIR/linux/arm64/$GARM_PROVIDER_NAME \
38+
-tags osusergo,netgo,sqlite_omit_load_extension \
39+
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .
40+
41+
# Windows
42+
GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-cc go build -mod vendor \
43+
-o $BUILD_DIR/windows/amd64/$GARM_PROVIDER_NAME.exe \
44+
-tags osusergo,netgo,sqlite_omit_load_extension \
45+
-ldflags "-s -w -X main.Version=$VERSION" .
46+
47+
git checkout $CURRENT_BRANCH || true
48+
chown $USER_ID:$USER_GROUP -R "$OUTPUT_DIR"

scripts/make-release.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
echo $GARM_REF
4+
GARM_PROVIDER_NAME=${GARM_PROVIDER_NAME:-garm-provider-azure}
5+
6+
VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always)
7+
RELEASE="$PWD/release"
8+
9+
[ ! -d "$RELEASE" ] && mkdir -p "$RELEASE"
10+
11+
if [ ! -z "$GARM_REF" ]; then
12+
VERSION=$(git describe --tags --match='v[0-9]*' --always $GARM_REF)
13+
fi
14+
15+
echo $VERSION
16+
17+
if [ ! -d "build/$VERSION" ]; then
18+
echo "missing build/$VERSION"
19+
exit 1
20+
fi
21+
22+
# Windows
23+
24+
if [ ! -d "build/$VERSION/windows/amd64" ];then
25+
echo "missing build/$VERSION/windows/amd64"
26+
exit 1
27+
fi
28+
29+
if [ ! -f "build/$VERSION/windows/amd64/$GARM_PROVIDER_NAME.exe" ];then
30+
echo "missing build/$VERSION/windows/amd64/$GARM_PROVIDER_NAME.exe"
31+
exit 1
32+
fi
33+
34+
pushd build/$VERSION/windows/amd64
35+
zip $GARM_PROVIDER_NAME-windows-amd64.zip $GARM_PROVIDER_NAME.exe
36+
sha256sum $GARM_PROVIDER_NAME-windows-amd64.zip > $GARM_PROVIDER_NAME-windows-amd64.zip.sha256
37+
mv $GARM_PROVIDER_NAME-windows-amd64.zip $RELEASE
38+
mv $GARM_PROVIDER_NAME-windows-amd64.zip.sha256 $RELEASE
39+
popd
40+
41+
# Linux
42+
OS_ARCHES=("amd64" "arm64")
43+
44+
for arch in ${OS_ARCHES[@]};do
45+
if [ ! -f "build/$VERSION/linux/$arch/$GARM_PROVIDER_NAME" ];then
46+
echo "missing build/$VERSION/linux/$arch/$GARM_PROVIDER_NAME"
47+
exit 1
48+
fi
49+
50+
pushd build/$VERSION/linux/$arch
51+
tar czf $GARM_PROVIDER_NAME-linux-$arch.tgz $GARM_PROVIDER_NAME
52+
sha256sum $GARM_PROVIDER_NAME-linux-$arch.tgz > $GARM_PROVIDER_NAME-linux-$arch.tgz.sha256
53+
mv $GARM_PROVIDER_NAME-linux-$arch.tgz $RELEASE
54+
mv $GARM_PROVIDER_NAME-linux-$arch.tgz.sha256 $RELEASE
55+
popd
56+
done

0 commit comments

Comments
 (0)