1
0
Fork 0
mirror of https://github.com/Luzifer/github-publish.git synced 2024-11-09 23:00:14 +00:00
github-publish/golang.sh

106 lines
2.6 KiB
Bash
Raw Normal View History

2016-11-21 12:48:30 +00:00
#!/bin/bash
set -euo pipefail
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
(which zip 2>&1 1>/dev/null) || {
(which apk 2>&1 1>/dev/null) && apk add --update zip
(which apt-get 2>&1 1>/dev/null) && apt-get update && apt-get install -y zip
}
function step() {
echo "===> $@..."
}
VERSION=$(git describe --tags --exact-match || echo "ghpublish__notags")
2016-11-21 12:48:30 +00:00
PWD=$(pwd)
godir=${PWD/${GOPATH}\/src\//}
2016-11-21 12:48:30 +00:00
REPO=${REPO:-$(echo ${godir} | cut -d '/' -f 3)}
GHUSER=${GHUSER:-$(echo ${godir} | cut -d '/' -f 2)}
2016-11-21 12:48:30 +00:00
ARCHS=${ARCHS:-"linux/amd64 linux/arm darwin/amd64 windows/amd64"}
DEPLOYMENT_TAG=${DEPLOYMENT_TAG:-${VERSION}}
PACKAGES=${PACKAGES:-$(echo ${godir} | cut -d '/' -f 1-3)}
BUILD_DIR=${BUILD_DIR:-.build}
DRAFT=${DRAFT:-true}
2019-02-04 15:24:54 +00:00
FORCE_SKIP_UPLOAD=${FORCE_SKIP_UPLOAD:-false}
2016-11-21 12:48:30 +00:00
go version
step "Retrieve dependencies"
go get github.com/aktau/github-release
go get github.com/mitchellh/gox
step "Test code"
go vet ${PACKAGES}
go test ${PACKAGES}
step "Cleanup build directory if present"
rm -rf ${BUILD_DIR}
step "Compile program"
mkdir ${BUILD_DIR}
gox -ldflags="-X main.version=${VERSION}" -osarch="${ARCHS}" \
-output="${BUILD_DIR}/{{.Dir}}_{{.OS}}_{{.Arch}}" \
${PACKAGES}
2016-11-21 12:48:30 +00:00
step "Generate binary SHASUMs"
cd ${BUILD_DIR}
sha256sum * >SHA256SUMS
2016-11-21 12:48:30 +00:00
step "Packing archives"
for file in *; do
if [ "${file}" = "SHA256SUMS" ]; then
continue
fi
if [[ ${file} == *linux* ]]; then
tar -czf "${file%%.*}.tar.gz" "${file}"
else
zip "${file%%.*}.zip" "${file}"
fi
rm "${file}"
done
step "Generate archive SHASUMs"
sha256sum * >>SHA256SUMS
grep -v 'SHA256SUMS' SHA256SUMS >SHA256SUMS.tmp
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
step "Publish builds to Github"
2019-02-04 15:24:54 +00:00
if [[ ${VERSION} == "ghpublish__notags" ]]; then
echo "No tag present, stopping build now."
exit 0
fi
2016-11-21 12:48:30 +00:00
if [ -z "${GITHUB_TOKEN}" ]; then
2019-02-04 15:24:54 +00:00
echo 'Please set $GITHUB_TOKEN environment variable'
exit 1
fi
2016-11-21 12:48:30 +00:00
2019-02-04 15:24:54 +00:00
if [[ ${DRAFT} == "true" ]]; then
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
step "Upload build assets"
for file in *; do
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
cd -