diff --git a/.gitignore b/.gitignore index c4a5d98b..f1608d85 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ bin/ # vendor/ .vscode cmd/temp +build/ +release/ diff --git a/Dockerfile b/Dockerfile index 80d9b2a7..2972344d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,11 +13,11 @@ RUN git clone https://github.com/cloudbase/garm-provider-openstack /build/garm-p RUN cd /build/garm && go build -o /bin/garm \ -tags osusergo,netgo,sqlite_omit_load_extension \ - -ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" \ + -ldflags "-extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" \ /build/garm/cmd/garm RUN mkdir -p /opt/garm/providers.d -RUN cd /build/garm-provider-azure && go build -ldflags="-linkmode external -extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-azure . -RUN cd /build/garm-provider-openstack && go build -ldflags="-linkmode external -extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-openstack . +RUN cd /build/garm-provider-azure && go build -ldflags="-extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-azure . +RUN cd /build/garm-provider-openstack && go build -ldflags="-extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-openstack . FROM scratch diff --git a/Dockerfile.build-static b/Dockerfile.build-static index ae023b71..2ed27168 100644 --- a/Dockerfile.build-static +++ b/Dockerfile.build-static @@ -12,4 +12,6 @@ RUN wget http://musl.cc/aarch64-linux-musl-cross.tgz -O /tmp/aarch64-linux-musl- ADD ./scripts/build-static.sh /build-static.sh RUN chmod +x /build-static.sh +ADD . /build/garm + CMD ["/bin/sh"] diff --git a/Makefile b/Makefile index b965d598..74c09dc0 100644 --- a/Makefile +++ b/Makefile @@ -7,17 +7,26 @@ USER_GROUP=$(shell ((docker --version | grep -q podman) && echo "0" || id -g)) ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) GOPATH ?= $(shell go env GOPATH) VERSION ?= $(shell git describe --tags --match='v[0-9]*' --dirty --always) +GARM_REF ?= $(shell git rev-parse --abbrev-ref HEAD) GO ?= go default: build -.PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify +.PHONY : build-static test install-lint-deps lint go-test fmt fmtcheck verify-vendor verify create-release-files release build-static: @echo Building garm docker build --tag $(IMAGE_TAG) -f Dockerfile.build-static . - docker run --rm -e USER_ID=$(USER_ID) -e USER_GROUP=$(USER_GROUP) -v $(PWD):/build/garm:z $(IMAGE_TAG) /build-static.sh - @echo Binaries are available in $(PWD)/bin + docker run --rm -e USER_ID=$(USER_ID) -e GARM_REF=$(GARM_REF) -e USER_GROUP=$(USER_GROUP) -v $(PWD)/build:/build/output:z $(IMAGE_TAG) /build-static.sh + @echo Binaries are available in $(PWD)/build + +create-release-files: + ./scripts/make-release.sh + +release: build-static create-release-files + +clean: + @rm -rf ./bin ./build ./release build: @echo Building garm ${VERSION} diff --git a/scripts/build-static.sh b/scripts/build-static.sh index cafa6a55..51ed2682 100755 --- a/scripts/build-static.sh +++ b/scripts/build-static.sh @@ -1,43 +1,67 @@ #!/bin/sh GARM_SOURCE="/build/garm" -BIN_DIR="$GARM_SOURCE/bin" -git config --global --add safe.directory "$GARM_SOURCE" +git config --global --add safe.directory /build/garm +cd $GARM_SOURCE -[ ! -d "$BIN_DIR" ] && mkdir -p "$BIN_DIR" +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) +if [ ! -z "$GARM_REF" ] && [ "$GARM_REF" != "$CURRENT_BRANCH" ];then + git checkout $GARM_REF +fi + +cd $GARM_SOURCE + +OUTPUT_DIR="/build/output" +VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always) +BUILD_DIR="$OUTPUT_DIR/$VERSION" + + +[ ! -d "$BUILD_DIR/linux" ] && mkdir -p "$BUILD_DIR/linux" +[ ! -d "$BUILD_DIR/windows" ] && mkdir -p "$BUILD_DIR/windows" export CGO_ENABLED=1 USER_ID=${USER_ID:-$UID} USER_GROUP=${USER_GROUP:-$(id -g)} -mkdir -p $BIN_DIR/amd64 $BIN_DIR/arm64 +# Garm cd $GARM_SOURCE/cmd/garm -go build -mod vendor \ - -o $BIN_DIR/amd64/garm \ + +# Linux +GOOS=linux GOARCH=amd64 go build -mod vendor \ + -o $BUILD_DIR/linux/amd64/garm \ -tags osusergo,netgo,sqlite_omit_load_extension \ - -ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" . -CC=aarch64-linux-musl-gcc GOARCH=arm64 go build \ + -ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" . +GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build \ -mod vendor \ - -o $BIN_DIR/arm64/garm \ + -o $BUILD_DIR/linux/arm64/garm \ -tags osusergo,netgo,sqlite_omit_load_extension \ - -ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" . -# GOOS=windows CC=x86_64-w64-mingw32-cc go build -mod vendor \ -# -o $BIN_DIR/amd64/garm.exe \ -# -tags osusergo,netgo,sqlite_omit_load_extension \ -# -ldflags "-s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" . + -ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" . +# Windows +GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-cc go build -mod vendor \ + -o $BUILD_DIR/windows/amd64/garm.exe \ + -tags osusergo,netgo,sqlite_omit_load_extension \ + -ldflags "-s -w -X main.Version=$VERSION" . + +# garm-cli cd $GARM_SOURCE/cmd/garm-cli -go build -mod vendor \ - -o $BIN_DIR/amd64/garm-cli \ + +# Linux +GOOS=linux GOARCH=amd64 go build -mod vendor \ + -o $BUILD_DIR/linux/amd64/garm-cli \ -tags osusergo,netgo,sqlite_omit_load_extension \ - -ldflags "-linkmode external -extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" . -CC=aarch64-linux-musl-gcc GOARCH=arm64 go build -mod vendor \ - -o $BIN_DIR/arm64/garm-cli \ + -ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" . +GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build -mod vendor \ + -o $BUILD_DIR/linux/arm64/garm-cli \ -tags osusergo,netgo,sqlite_omit_load_extension \ - -ldflags "-linkmode external -extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" . -# GOOS=windows CGO_ENABLED=0 go build -mod vendor \ -# -o $BIN_DIR/amd64/garm-cli.exe \ -# -tags osusergo,netgo,sqlite_omit_load_extension \ -# -ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" . + -ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" . + +# Windows +GOOS=windows GOARCH=amd64 go build -mod vendor \ + -o $BUILD_DIR/windows/amd64/garm-cli.exe \ + -tags osusergo,netgo,sqlite_omit_load_extension \ + -ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" . + -chown $USER_ID:$USER_GROUP -R "$BIN_DIR" +git checkout $CURRENT_BRANCH || true +chown $USER_ID:$USER_GROUP -R "$OUTPUT_DIR" diff --git a/scripts/make-release.sh b/scripts/make-release.sh new file mode 100755 index 00000000..fc9c6b04 --- /dev/null +++ b/scripts/make-release.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +echo $GARM_REF + +VERSION=$(git describe --tags --match='v[0-9]*' --dirty --always) +RELEASE="$PWD/release" + +[ ! -d "$RELEASE" ] && mkdir -p "$RELEASE" + +if [ ! -z "$GARM_REF" ]; then + VERSION=$(git describe --tags --match='v[0-9]*' --always $GARM_REF) +fi + +echo $VERSION + +if [ ! -d "build/$VERSION" ]; then + echo "missing build/$VERSION" + exit 1 +fi + +# Windows + +if [ ! -d "build/$VERSION/windows/amd64" ];then + echo "missing build/$VERSION/windows/amd64" + exit 1 +fi + +WINDOWS_FILES=("garm.exe" "garm-cli.exe") + +for file in ${WINDOWS_FILES[@]};do + if [ ! -f "build/$VERSION/windows/amd64/$file" ];then + echo "missing build/$VERSION/windows/amd64/$file" + exit 1 + fi + + pushd build/$VERSION/windows/amd64 + zip ${file%%.exe}-windows-amd64.zip $file + sha256sum ${file%%.exe}-windows-amd64.zip > ${file%%.exe}-windows-amd64.zip.sha256 + mv ${file%%.exe}-windows-amd64.zip $RELEASE + mv ${file%%.exe}-windows-amd64.zip.sha256 $RELEASE + popd +done + +# Linux +OS_ARCHES=("amd64" "arm64") +FILES=("garm" "garm-cli") + +for arch in ${OS_ARCHES[@]};do + for file in ${FILES[@]};do + if [ ! -f "build/$VERSION/linux/$arch/$file" ];then + echo "missing build/$VERSION/linux/$arch/$file" + exit 1 + fi + + pushd build/$VERSION/linux/$arch + tar czf ${file}-linux-$arch.tgz $file + sha256sum ${file}-linux-$arch.tgz > ${file}-linux-$arch.tgz.sha256 + mv ${file}-linux-$arch.tgz $RELEASE + mv ${file}-linux-$arch.tgz.sha256 $RELEASE + popd + done +done