2016-11-21 12:48:30 +00:00
|
|
|
#!/bin/bash
|
2017-12-28 17:47:32 +00:00
|
|
|
set -euo pipefail
|
2017-12-28 17:33:28 +00:00
|
|
|
|
2018-03-21 10:32:58 +00:00
|
|
|
curl -sL https://raw.githubusercontent.com/Luzifer/github-publish/master/SHA256SUMS |
|
|
|
|
grep "golang.sh" | sha256sum -c || exit 2
|
2016-11-21 12:48:30 +00:00
|
|
|
|
2018-03-21 10:32:58 +00:00
|
|
|
(which zip 2>&1 1>/dev/null) || {
|
|
|
|
(which apk 2>&1 1>/dev/null) && apk add --update zip
|
2018-07-23 14:47:43 +00:00
|
|
|
(which apt-get 2>&1 1>/dev/null) && apt-get update && apt-get install -y zip
|
2018-01-20 19:29:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 10:32:58 +00:00
|
|
|
function step() {
|
2019-07-10 12:13:32 +00:00
|
|
|
echo "===> $@..." >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
function substep() {
|
|
|
|
echo "======> $@..." >&2
|
2018-01-20 19:29:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:53:31 +00:00
|
|
|
VERSION=$(git describe --tags --always || echo "dev")
|
|
|
|
PWD=$(pwd)
|
|
|
|
godir=${PWD/${GOPATH}\/src\//}
|
|
|
|
REPO=${REPO:-$(echo ${godir} | cut -d '/' -f 3)}
|
|
|
|
GHUSER=${GHUSER:-$(echo ${godir} | cut -d '/' -f 2)}
|
2019-07-10 12:13:32 +00:00
|
|
|
ARCHS=(${ARCHS:-linux/amd64 linux/arm darwin/amd64 windows/amd64})
|
2019-07-10 10:46:51 +00:00
|
|
|
DEPLOYMENT_TAG=${DEPLOYMENT_TAG:-${VERSION}}
|
2019-07-10 12:13:32 +00:00
|
|
|
PACKAGES=(${PACKAGES:-$(echo ${godir} | cut -d '/' -f 1-3)})
|
2019-07-10 10:53:31 +00:00
|
|
|
BUILD_DIR=${BUILD_DIR:-.build}
|
2018-08-10 09:12:06 +00:00
|
|
|
DRAFT=${DRAFT:-true}
|
2019-02-04 15:24:54 +00:00
|
|
|
FORCE_SKIP_UPLOAD=${FORCE_SKIP_UPLOAD:-false}
|
2019-07-10 10:31:34 +00:00
|
|
|
MOD_MODE=${MOD_MODE:-}
|
2016-11-21 12:48:30 +00:00
|
|
|
|
2018-04-27 10:05:04 +00:00
|
|
|
go version
|
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Retrieve dependencies"
|
2019-07-10 10:46:51 +00:00
|
|
|
pushd ${GOPATH}
|
2017-03-06 10:27:54 +00:00
|
|
|
go get github.com/aktau/github-release
|
2019-07-10 10:46:51 +00:00
|
|
|
popd
|
2017-03-06 10:27:54 +00:00
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Test code"
|
2019-07-10 12:13:32 +00:00
|
|
|
go_params=()
|
2019-07-10 10:59:44 +00:00
|
|
|
|
|
|
|
if [[ -n ${MOD_MODE} ]]; then
|
2019-07-10 12:13:32 +00:00
|
|
|
go_params+=(-mod="${MOD_MODE}")
|
2019-07-10 10:59:44 +00:00
|
|
|
fi
|
|
|
|
|
2019-07-10 12:13:32 +00:00
|
|
|
go vet "${go_params[@]}" ${PACKAGES}
|
|
|
|
go test "${go_params[@]}" ${PACKAGES}
|
2017-03-06 10:27:54 +00:00
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Cleanup build directory if present"
|
2018-03-21 10:32:58 +00:00
|
|
|
rm -rf ${BUILD_DIR}
|
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Compile program"
|
2018-01-20 19:29:29 +00:00
|
|
|
mkdir ${BUILD_DIR}
|
2019-07-10 10:31:34 +00:00
|
|
|
|
2019-07-10 12:13:32 +00:00
|
|
|
build_params=("${go_params[@]}")
|
|
|
|
build_params+=(
|
2019-07-10 10:31:34 +00:00
|
|
|
-ldflags="-X main.version=${VERSION}"
|
|
|
|
)
|
|
|
|
|
2019-07-10 12:13:32 +00:00
|
|
|
for package in "${PACKAGES[@]}"; do
|
|
|
|
for osarch in "${ARCHS[@]}"; do
|
|
|
|
export GOOS=${osarch%%/*}
|
|
|
|
export GOARCH=${osarch##*/}
|
2019-07-10 10:31:34 +00:00
|
|
|
|
2019-07-10 12:13:32 +00:00
|
|
|
[[ ${GOOS} == "windows" ]] && suffix=".exe" || suffix=""
|
|
|
|
outfile="${BUILD_DIR}/${package##*/}_${GOOS}_${GOARCH}${suffix}"
|
|
|
|
|
|
|
|
substep "Building for ${osarch} into ${outfile}"
|
|
|
|
|
|
|
|
go build \
|
|
|
|
-o "${outfile}" \
|
|
|
|
"${build_params[@]}" \
|
|
|
|
"${package}"
|
|
|
|
done
|
|
|
|
done
|
2016-11-21 12:48:30 +00:00
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Generate binary SHASUMs"
|
2018-01-20 19:29:29 +00:00
|
|
|
cd ${BUILD_DIR}
|
2018-03-21 10:32:58 +00:00
|
|
|
sha256sum * >SHA256SUMS
|
2016-11-21 12:48:30 +00:00
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Packing archives"
|
2018-01-20 19:29:29 +00:00
|
|
|
for file in *; do
|
2018-03-21 10:32:58 +00:00
|
|
|
if [ "${file}" = "SHA256SUMS" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
2017-12-28 17:28:03 +00:00
|
|
|
|
2018-03-21 10:32:58 +00:00
|
|
|
if [[ ${file} == *linux* ]]; then
|
|
|
|
tar -czf "${file%%.*}.tar.gz" "${file}"
|
|
|
|
else
|
|
|
|
zip "${file%%.*}.zip" "${file}"
|
|
|
|
fi
|
2018-01-20 19:29:29 +00:00
|
|
|
|
2018-03-21 10:32:58 +00:00
|
|
|
rm "${file}"
|
2017-12-28 17:28:03 +00:00
|
|
|
done
|
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Generate archive SHASUMs"
|
2018-03-21 10:32:58 +00:00
|
|
|
sha256sum * >>SHA256SUMS
|
|
|
|
grep -v 'SHA256SUMS' SHA256SUMS >SHA256SUMS.tmp
|
2018-01-20 19:29:29 +00:00
|
|
|
mv SHA256SUMS.tmp SHA256SUMS
|
|
|
|
|
2019-02-04 15:24:54 +00:00
|
|
|
echo -e "\n\n=== Recorded checksums ==="
|
|
|
|
cat SHA256SUMS
|
|
|
|
|
|
|
|
if [[ ${FORCE_SKIP_UPLOAD} == "true" ]]; then
|
|
|
|
echo "Upload is skipped, stopping build now."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Publish builds to Github"
|
2018-01-20 19:29:29 +00:00
|
|
|
|
2019-02-05 14:31:41 +00:00
|
|
|
if ! git describe --tags --exact-match; then
|
2018-03-21 10:32:58 +00:00
|
|
|
echo "No tag present, stopping build now."
|
|
|
|
exit 0
|
2018-01-20 19:29:29 +00:00
|
|
|
fi
|
2016-11-21 12:48:30 +00:00
|
|
|
|
2018-01-20 19:29:29 +00:00
|
|
|
if [ -z "${GITHUB_TOKEN}" ]; then
|
2019-02-04 15:24:54 +00:00
|
|
|
echo 'Please set $GITHUB_TOKEN environment variable'
|
2018-03-21 10:32:58 +00:00
|
|
|
exit 1
|
2018-01-20 19:29:29 +00:00
|
|
|
fi
|
2016-11-21 12:48:30 +00:00
|
|
|
|
2019-02-04 15:24:54 +00:00
|
|
|
if [[ ${DRAFT} == "true" ]]; then
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Create a drafted release"
|
|
|
|
github-release release --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${DEPLOYMENT_TAG} --draft || true
|
|
|
|
else
|
|
|
|
step "Create a published release"
|
|
|
|
github-release release --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${DEPLOYMENT_TAG} || true
|
|
|
|
fi
|
2016-11-21 12:48:30 +00:00
|
|
|
|
2018-08-10 10:18:04 +00:00
|
|
|
step "Upload build assets"
|
2018-01-20 19:41:36 +00:00
|
|
|
for file in *; do
|
2018-03-21 10:32:58 +00:00
|
|
|
echo "- ${file}"
|
|
|
|
github-release upload --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${file} --file ${file}
|
2016-11-21 12:48:30 +00:00
|
|
|
done
|
2017-07-09 18:19:11 +00:00
|
|
|
|
2018-01-20 19:29:29 +00:00
|
|
|
cd -
|