2017-11-15 19:33:05 +08:00
|
|
|
#!/bin/bash
|
|
|
|
set -o errexit
|
|
|
|
|
|
|
|
# define package name
|
|
|
|
WORK_DIR=/go/src/github.com/goodrain/rainbond
|
|
|
|
BASE_NAME=rainbond
|
2020-10-21 10:31:40 +08:00
|
|
|
IMAGE_BASE_NAME=${BUILD_IMAGE_BASE_NAME:-'rainbond'}
|
2020-11-25 16:54:51 +08:00
|
|
|
DOMESTIC_NAMESPACE=${DOMESTIC_NAMESPACE:-'goodrain'}
|
2020-02-11 12:43:10 +08:00
|
|
|
|
2020-02-12 12:35:50 +08:00
|
|
|
GO_VERSION=1.13
|
2020-09-06 11:09:48 +08:00
|
|
|
|
2020-09-21 07:51:24 +08:00
|
|
|
GOPROXY=${GOPROXY:-'https://goproxy.io'}
|
2020-09-28 18:16:25 +08:00
|
|
|
|
|
|
|
if [ "$DISABLE_GOPROXY" == "true" ]; then
|
|
|
|
GOPROXY=
|
|
|
|
fi
|
2021-06-17 22:59:05 +08:00
|
|
|
if [ -z "$GOOS" ]; then
|
|
|
|
GOOS="linux"
|
2020-09-06 11:09:48 +08:00
|
|
|
fi
|
2021-06-17 22:59:05 +08:00
|
|
|
if [ "$DEBUG" ]; then
|
|
|
|
set -x
|
2020-09-06 11:42:38 +08:00
|
|
|
fi
|
2020-03-22 12:54:17 +08:00
|
|
|
BRANCH=$(git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3)
|
2021-06-17 22:59:05 +08:00
|
|
|
if [ -z "$VERSION" ]; then
|
|
|
|
if [ -z "$TRAVIS_TAG" ]; then
|
|
|
|
if [ -z "$TRAVIS_BRANCH" ]; then
|
|
|
|
VERSION=$BRANCH-dev
|
|
|
|
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)
|
2017-11-15 19:33:05 +08:00
|
|
|
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}
|
2020-09-25 15:02:26 +08:00
|
|
|
build_items=(api chaos gateway monitor mq webcli worker eventlog init-probe mesh-data-panel grctl node resource-proxy)
|
2017-11-15 19:33:05 +08:00
|
|
|
|
2018-11-07 15:03:36 +08:00
|
|
|
build::binary() {
|
|
|
|
echo "---> build binary:$1"
|
2020-09-06 11:09:48 +08:00
|
|
|
home=$(pwd)
|
|
|
|
local go_mod_cache="${home}/.cache"
|
|
|
|
local OUTPATH="./_output/binary/$GOOS/${BASE_NAME}-$1"
|
2019-02-15 17:20:08 +08:00
|
|
|
local DOCKER_PATH="./hack/contrib/docker/$1"
|
2020-09-06 11:09:48 +08:00
|
|
|
local build_image="golang:${GO_VERSION}"
|
|
|
|
local build_args="-w -s -X github.com/goodrain/rainbond/cmd.version=${release_desc}"
|
|
|
|
local build_dir="./cmd/$1"
|
|
|
|
local build_tag=""
|
2021-06-17 22:59:05 +08:00
|
|
|
if [ -f "${DOCKER_PATH}/ignorebuild" ]; then
|
2020-09-25 15:02:26 +08:00
|
|
|
return
|
|
|
|
fi
|
2020-09-06 11:09:48 +08:00
|
|
|
CGO_ENABLED=1
|
2021-06-17 22:59:05 +08:00
|
|
|
if [ "$1" = "eventlog" ]; then
|
2020-09-06 11:09:48 +08:00
|
|
|
docker build -t goodraim.me/event-build:v1 "${DOCKER_PATH}/build"
|
|
|
|
build_image="goodraim.me/event-build:v1"
|
2021-06-17 22:59:05 +08:00
|
|
|
elif [ "$1" = "chaos" ]; then
|
2020-09-06 11:09:48 +08:00
|
|
|
build_dir="./cmd/builder"
|
2021-06-17 22:59:05 +08:00
|
|
|
elif [ "$1" = "gateway" ]; then
|
2020-09-06 11:09:48 +08:00
|
|
|
build_image="golang:1.13-alpine"
|
2021-06-17 22:59:05 +08:00
|
|
|
elif [ "$1" = "monitor" ]; then
|
2020-09-06 11:09:48 +08:00
|
|
|
CGO_ENABLED=0
|
2021-06-17 22:59:05 +08:00
|
|
|
fi
|
|
|
|
docker run --rm -e CGO_ENABLED=${CGO_ENABLED} -e GOPROXY=${GOPROXY} -e GOOS="${GOOS}" -v "${go_mod_cache}":/go/pkg/mod -v "$(pwd)":${WORK_DIR} -w ${WORK_DIR} ${build_image} go build -ldflags "${build_args}" -tags "${build_tag}" -o "${OUTPATH}" ${build_dir}
|
|
|
|
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() {
|
2020-09-06 11:09:48 +08:00
|
|
|
local OUTPATH="./_output/binary/$GOOS/${BASE_NAME}-$1"
|
|
|
|
local build_image_dir="./_output/image/$1/"
|
2020-09-25 15:02:26 +08:00
|
|
|
local source_dir="./hack/contrib/docker/$1"
|
2020-11-25 16:54:51 +08:00
|
|
|
mkdir -p "${build_image_dir}"
|
|
|
|
chmod 777 "${build_image_dir}"
|
2021-06-17 22:59:05 +08:00
|
|
|
if [ ! -f "${source_dir}/ignorebuild" ]; then
|
|
|
|
if [ !${CACHE} ] || [ ! -f "${OUTPATH}" ]; then
|
2020-09-25 15:02:26 +08:00
|
|
|
build::binary "$1"
|
|
|
|
fi
|
2020-11-25 16:54:51 +08:00
|
|
|
cp "${OUTPATH}" "${build_image_dir}"
|
2021-06-17 22:59:05 +08:00
|
|
|
fi
|
2020-11-25 16:54:51 +08:00
|
|
|
cp -r ${source_dir}/* "${build_image_dir}"
|
2020-09-06 11:09:48 +08:00
|
|
|
pushd "${build_image_dir}"
|
2021-06-17 22:59:05 +08:00
|
|
|
echo "---> build image:$1"
|
|
|
|
docker build --build-arg RELEASE_DESC="${release_desc}" -t "${IMAGE_BASE_NAME}/rbd-$1:${VERSION}" -f Dockerfile .
|
|
|
|
docker run --rm "${IMAGE_BASE_NAME}/rbd-$1:${VERSION}" version
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "image version is different ${release_desc}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -f "${source_dir}/test.sh" ]; then
|
|
|
|
"${source_dir}/test.sh" "${IMAGE_BASE_NAME}/rbd-$1:${VERSION}"
|
|
|
|
fi
|
|
|
|
if [ "$2" = "push" ]; then
|
|
|
|
if [ $DOCKER_USERNAME ]; then
|
|
|
|
docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD"
|
2020-09-25 15:02:26 +08:00
|
|
|
fi
|
2021-06-17 22:59:05 +08:00
|
|
|
docker push "${IMAGE_BASE_NAME}/rbd-$1:${VERSION}"
|
|
|
|
if [ "${DOMESTIC_BASE_NAME}" ]; then
|
|
|
|
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-09-06 11:09:48 +08:00
|
|
|
fi
|
2021-06-17 22:59:05 +08:00
|
|
|
fi
|
2018-11-07 15:03:36 +08:00
|
|
|
popd
|
2020-11-25 16:54:51 +08:00
|
|
|
rm -rf "${build_image_dir}"
|
2018-11-07 15:03:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 22:59:05 +08:00
|
|
|
build::image::all() {
|
|
|
|
for item in "${build_items[@]}"; 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
|
|
|
}
|
|
|
|
|
2021-06-17 22:59:05 +08:00
|
|
|
build::binary::all() {
|
|
|
|
for item in "${build_items[@]}"; do
|
2020-09-06 11:09:48 +08:00
|
|
|
build::binary "$item" "$1"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:33:05 +08:00
|
|
|
case $1 in
|
2021-06-17 22:59:05 +08:00
|
|
|
binary)
|
|
|
|
if [ "$2" = "all" ]; then
|
|
|
|
build::binary::all "$2"
|
|
|
|
else
|
|
|
|
build::binary "$2"
|
|
|
|
fi
|
2018-12-01 18:11:32 +08:00
|
|
|
;;
|
2021-06-17 22:59:05 +08:00
|
|
|
*)
|
|
|
|
if [ "$1" = "all" ]; then
|
|
|
|
build::image::all "$2"
|
|
|
|
else
|
|
|
|
build::image "$1" "$2"
|
|
|
|
fi
|
2017-11-22 13:56:59 +08:00
|
|
|
;;
|
2018-12-06 10:50:02 +08:00
|
|
|
esac
|