Rainbond/release.sh

175 lines
8.1 KiB
Bash
Raw Normal View History

#!/bin/bash
set -o errexit
# define package name
WORK_DIR=/go/src/github.com/goodrain/rainbond
BASE_NAME=rainbond
IMAGE_BASE_NAME=rainbond
if [ $BUILD_IMAGE_BASE_NAME ];
then
IMAGE_BASE_NAME=${BUILD_IMAGE_BASE_NAME}
fi
2020-02-11 12:43:10 +08:00
2020-02-12 12:35:50 +08:00
GO_VERSION=1.13
GATEWAY_GO_VERSION=1.13-alpine
2020-03-22 12:54:17 +08:00
BRANCH=$(git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3)
2019-07-12 22:18:58 +08:00
if [ -z "$VERSION" ];then
if [ -z "$TRAVIS_TAG" ]; then
if [ -z "$TRAVIS_BRANCH" ]; then
2020-03-22 12:54:17 +08:00
VERSION=$BRANCH-dev
2019-07-12 22:18:58 +08:00
else
VERSION=$TRAVIS_BRANCH-dev
fi
else
VERSION=$TRAVIS_TAG
fi
2019-03-29 22:17:27 +08:00
fi
2019-07-12 22:18:58 +08:00
2018-05-21 16:21:27 +08:00
buildTime=$(date +%F-%H)
git_commit=$(git log -n 1 --pretty --format=%h)
2017-12-01 12:32:31 +08:00
2018-11-07 15:03:36 +08:00
release_desc=${VERSION}-${git_commit}-${buildTime}
2018-11-07 15:03:36 +08:00
build::node() {
local releasedir=./.release
local distdir=$releasedir/dist/usr/local
[ ! -d "$distdir" ] && mkdir -p $distdir/bin || rm -rf $distdir/bin/*
2018-05-21 16:21:27 +08:00
echo "---> Build Binary For RBD"
echo "rbd plugins version:$release_desc"
2019-04-11 18:49:45 +08:00
case $1 in
node)
echo "build node"
docker run --rm -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc} -X github.com/goodrain/rainbond/util/license.enterprise=${ENTERPRISE}" -o $releasedir/dist/usr/local/bin/node ./cmd/node
2019-04-11 18:49:45 +08:00
;;
2020-02-12 16:50:27 +08:00
grctl)
2019-04-11 18:49:45 +08:00
echo "build grctl"
docker run --rm -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o $releasedir/dist/usr/local/bin/grctl ./cmd/grctl
;;
certutil)
echo "build certutil"
docker run --rm -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o $releasedir/dist/usr/local/bin/grcert ./cmd/certutil
;;
*)
echo "build node"
docker run --rm -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc} -X github.com/goodrain/rainbond/util/license.enterprise=${ENTERPRISE}" -o $releasedir/dist/usr/local/bin/node ./cmd/node
2020-02-12 16:50:27 +08:00
if [ "${ENTERPRISE}" = "true" ];then
echo "build grctl enterprise"
docker run --rm -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc} -X github.com/goodrain/rainbond/util/license.enterprise=${ENTERPRISE}" -o $releasedir/dist/usr/local/bin/grctl ./cmd/grctl
else
echo "build grctl"
docker run --rm -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o $releasedir/dist/usr/local/bin/grctl ./cmd/grctl
fi
2019-04-11 18:49:45 +08:00
echo "build certutil"
docker run --rm -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o $releasedir/dist/usr/local/bin/grcert ./cmd/certutil
pushd $distdir
tar zcf pkg.tgz `find . -maxdepth 1|sed 1d`
2018-06-08 18:02:06 +08:00
2019-04-11 18:49:45 +08:00
cat >Dockerfile <<EOF
2018-06-08 18:02:06 +08:00
FROM alpine:3.6
COPY pkg.tgz /
EOF
2019-04-11 18:49:45 +08:00
docker build -t ${BASE_NAME}/cni:rbd_$VERSION .
if [ "$1" = "push" ];then
2020-02-12 16:50:27 +08:00
docker push ${BASE_NAME}/cni:rbd_$VERSION
2019-04-11 18:49:45 +08:00
fi
popd
;;
esac
2017-11-20 12:03:39 +08:00
}
2018-11-07 15:03:36 +08:00
build::binary() {
echo "---> build binary:$1"
2018-12-01 18:11:32 +08:00
local OUTPATH=./_output/$GOOS/${BASE_NAME}-$1
local DOCKER_PATH="./hack/contrib/docker/$1"
2018-07-26 15:58:30 +08:00
HOME=`pwd`
2017-11-22 13:56:59 +08:00
if [ "$1" = "eventlog" ];then
docker build -t goodraim.me/event-build:v1 ${DOCKER_PATH}/build
2018-12-01 18:11:32 +08:00
docker run --rm -e GOOS=${GOOS} -v `pwd`:${WORK_DIR} -w ${WORK_DIR} goodraim.me/event-build:v1 go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${OUTPATH} ./cmd/eventlog
2017-11-22 15:08:35 +08:00
elif [ "$1" = "chaos" ];then
2018-12-01 18:11:32 +08:00
docker run --rm -e GOOS=${GOOS} -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${OUTPATH} ./cmd/builder
elif [ "$1" = "monitor" ];then
2018-12-01 18:11:32 +08:00
docker run --rm -e GOOS=${GOOS} -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -extldflags '-static' -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -tags 'netgo static_build' -o ${OUTPATH} ./cmd/$1
elif [ "$1" = "gateway" ];then
docker run --rm -e GOOS=${GOOS} -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GATEWAY_GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${OUTPATH} ./cmd/$1
2017-11-22 13:56:59 +08:00
else
if [ "${ENTERPRISE}" = "true" ];then
echo "---> ENTERPRISE:${ENTERPRISE}"
docker run --rm -e GOOS=${GOOS} -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc} -X github.com/goodrain/rainbond/util/license.enterprise=${ENTERPRISE}" -o ${OUTPATH} ./cmd/$1
2019-09-17 19:24:52 +08:00
else
docker run --rm -e GOOS=${GOOS} -v `pwd`:${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${OUTPATH} ./cmd/$1
fi
2018-12-01 18:11:32 +08:00
fi
if [ "$GOOS" = "windows" ];then
mv $OUTPATH ${OUTPATH}.exe
2017-11-22 13:56:59 +08:00
fi
2018-11-07 15:03:36 +08:00
}
build::image() {
local REPO_PATH="$PWD"
2020-02-12 12:35:50 +08:00
pushd "./hack/contrib/docker/$1"
2018-11-07 15:03:36 +08:00
echo "---> build binary:$1"
local DOCKER_PATH="./hack/contrib/docker/$1"
if [ "$1" = "eventlog" ];then
docker build -t goodraim.me/event-build:v1 build
2020-02-12 12:35:50 +08:00
docker run --rm -v "${REPO_PATH}":${WORK_DIR} -w ${WORK_DIR} goodraim.me/event-build:v1 go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${DOCKER_PATH}/${BASE_NAME}-$1 ./cmd/eventlog
2018-11-07 15:03:36 +08:00
elif [ "$1" = "chaos" ];then
2020-02-12 12:35:50 +08:00
docker run --rm -v "${REPO_PATH}":${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${DOCKER_PATH}/${BASE_NAME}-$1 ./cmd/builder
2018-11-07 15:03:36 +08:00
elif [ "$1" = "monitor" ];then
2020-02-12 12:35:50 +08:00
docker run -e CGO_ENABLED=0 --rm -v "${REPO_PATH}":${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${DOCKER_PATH}/${BASE_NAME}-$1 ./cmd/$1
elif [ "$1" = "gateway" ];then
2020-02-12 12:35:50 +08:00
docker run --rm -v "${REPO_PATH}":${WORK_DIR} -w ${WORK_DIR} -it golang:${GATEWAY_GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${DOCKER_PATH}/${BASE_NAME}-$1 ./cmd/$1
2018-11-07 15:03:36 +08:00
else
2019-09-20 10:19:36 +08:00
if [ "${ENTERPRISE}" = "true" ];then
echo "---> ENTERPRISE:${ENTERPRISE}"
2020-02-12 12:35:50 +08:00
docker run --rm -v "${REPO_PATH}":${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc} -X github.com/goodrain/rainbond/util/license.enterprise=${ENTERPRISE}" -o ${DOCKER_PATH}/${BASE_NAME}-$1 ./cmd/$1
2019-09-20 10:19:36 +08:00
else
2020-02-12 12:35:50 +08:00
docker run --rm -v "${REPO_PATH}":${WORK_DIR} -w ${WORK_DIR} -it golang:${GO_VERSION} go build -ldflags "-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}" -o ${DOCKER_PATH}/${BASE_NAME}-$1 ./cmd/$1
2019-09-20 10:19:36 +08:00
fi
2018-11-07 15:03:36 +08:00
fi
echo "---> build image:$1"
sed "s/__RELEASE_DESC__/${release_desc}/" Dockerfile > Dockerfile.release
2020-02-11 12:43:10 +08:00
docker build -t "${IMAGE_BASE_NAME}/rbd-$1:${VERSION}" -f Dockerfile.release .
2018-12-01 15:18:40 +08:00
if [ "$2" = "push" ];then
2020-02-11 12:43:10 +08:00
docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD"
docker push "${IMAGE_BASE_NAME}/rbd-$1:${VERSION}"
if [ ${DOMESTIC_BASE_NAME} ];
then
2020-02-11 13:07:26 +08:00
docker tag "${IMAGE_BASE_NAME}/rbd-$1:${VERSION}" "${DOMESTIC_BASE_NAME}/${DOMESTIC_NAMESPACE}/rbd-$1:${VERSION}"
docker login -u "$DOMESTIC_DOCKER_USERNAME" -p "$DOMESTIC_DOCKER_PASSWORD" ${DOMESTIC_BASE_NAME}
docker push "${DOMESTIC_BASE_NAME}/${DOMESTIC_NAMESPACE}/rbd-$1:${VERSION}"
2020-02-11 12:43:10 +08:00
fi
2018-11-21 17:35:07 +08:00
fi
2018-11-07 15:03:36 +08:00
rm -f ./Dockerfile.release
rm -f ./${BASE_NAME}-$1
popd
}
build::all(){
local build_items=(api chaos gateway monitor mq webcli worker eventlog init-probe mesh-data-panel grctl node)
2019-10-16 10:02:12 +08:00
for item in "${build_items[@]}"
2018-11-07 15:03:36 +08:00
do
2019-10-16 10:02:12 +08:00
build::image "$item" "$1"
2018-11-07 15:03:36 +08:00
done
2017-11-22 13:56:59 +08:00
}
case $1 in
2018-12-01 18:11:32 +08:00
binary)
if [ "$2" = "all" ];then
2019-02-28 14:52:39 +08:00
build_items=(chaos grctl node gateway monitor mq worker eventlog api init-probe)
2019-10-16 10:02:12 +08:00
for item in "${build_items[@]}"
2018-12-01 18:11:32 +08:00
do
build::binary $item $1
done
else
build::binary $2
fi
;;
2017-11-22 13:56:59 +08:00
*)
2018-11-07 15:03:36 +08:00
if [ "$1" = "all" ];then
2018-11-21 17:35:07 +08:00
build::all $2
2018-11-07 15:03:36 +08:00
else
2018-11-21 17:35:07 +08:00
build::image $1 $2
2018-11-07 15:03:36 +08:00
fi
2017-11-22 13:56:59 +08:00
;;
2018-12-06 10:50:02 +08:00
esac