1
0
Fork 0
mirror of https://github.com/Luzifer/github-publish.git synced 2024-12-29 22:31:21 +00:00

Update golang.sh to work with recent containers

- Use dedicated install path for tools
- Update file formatting to follow Google code convention
- Fix golang parameters only getting set when test is enabled
- Move logging to stderr
- Default to `readonly` mod-mode
- Use `-trimpath` by default

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-04-10 16:08:57 +02:00
parent dea3a59f9c
commit 31eb52b7fc
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 66 additions and 57 deletions

View file

@ -1,2 +1,2 @@
05e4977d541b2ef07c757acca912b7b58b379fb425eceb3a1f53c829a78a4aa4 docker2aci.sh 05e4977d541b2ef07c757acca912b7b58b379fb425eceb3a1f53c829a78a4aa4 docker2aci.sh
99cbc21bf39f58533b3f5e8a969499cf93e3dd36fb64c7f301bd2c6761125742 golang.sh 08103fd85ca1273aaf5ec2b35b75ad789d2352da6c7f6ad1aff4c5754dd133e4 golang.sh

121
golang.sh
View file

@ -2,19 +2,23 @@
set -euo pipefail set -euo pipefail
curl -sL https://raw.githubusercontent.com/Luzifer/github-publish/master/SHA256SUMS | curl -sL https://raw.githubusercontent.com/Luzifer/github-publish/master/SHA256SUMS |
grep "golang.sh" | sha256sum -c || exit 2 grep "golang.sh" | sha256sum -c || exit 2
(which zip 2>&1 1>/dev/null) || { (which zip 2>&1 1>/dev/null) || {
(which apk 2>&1 1>/dev/null) && apk add --update gawk zip (which apk 2>&1 1>/dev/null) && apk add --update gawk zip
(which apt-get 2>&1 1>/dev/null) && apt-get update && apt-get install -y zip (which apt-get 2>&1 1>/dev/null) && apt-get update && apt-get install -y zip
}
function log() {
echo "${@}" >&2
} }
function step() { function step() {
echo "===> $@..." >&2 log "===> $@..."
} }
function substep() { function substep() {
echo "======> $@..." >&2 log "======> $@..."
} }
VERSION=$(git describe --tags --always || echo "dev") VERSION=$(git describe --tags --always || echo "dev")
@ -28,29 +32,34 @@ PACKAGES=(${PACKAGES:-$(echo ${godir} | cut -d '/' -f 1-3)})
BUILD_DIR=${BUILD_DIR:-.build} BUILD_DIR=${BUILD_DIR:-.build}
DRAFT=${DRAFT:-true} DRAFT=${DRAFT:-true}
FORCE_SKIP_UPLOAD=${FORCE_SKIP_UPLOAD:-false} FORCE_SKIP_UPLOAD=${FORCE_SKIP_UPLOAD:-false}
MOD_MODE=${MOD_MODE:-} MOD_MODE=${MOD_MODE:-readonly}
NO_TESTS=${NO_TESTS:-false} NO_TESTS=${NO_TESTS:-false}
go_params=(
"-mod=${MOD_MODE}"
-modcacherw
-trimpath
)
step "Check go environment"
go version go version
step "Retrieve dependencies" step "Retrieve dependencies"
git clone "https://github.com/Luzifer/github-release.git" "${GOPATH}/src/github.com/Luzifer/github-release" tool_gopath=$(mktemp -d)
pushd "${GOPATH}/src/github.com/Luzifer/github-release" trap "rm -rf ${tool_gopath}" EXIT
GO111MODULE=on go install
popd substep "Install github-release"
GOPATH=${tool_gopath} go install \
"${go_params}" \
github.com/Luzifer/github-release@latest
if [[ $NO_TESTS == false ]]; then if [[ $NO_TESTS == false ]]; then
step "Test code" step "Test code"
go_params=() go vet "${go_params[@]}" ${PACKAGES}
go test "${go_params[@]}" ${PACKAGES}
if [[ -n ${MOD_MODE} ]]; then
go_params+=(-mod="${MOD_MODE}")
fi
go vet "${go_params[@]}" ${PACKAGES}
go test "${go_params[@]}" ${PACKAGES}
fi fi
step "Extract changelog"
changelog=$([ -f "${PWD}/History.md" ] && awk '/^#/ && ++c==2{exit}; /^#/f' "${PWD}/History.md" | tail -n +2 || echo "") changelog=$([ -f "${PWD}/History.md" ] && awk '/^#/ && ++c==2{exit}; /^#/f' "${PWD}/History.md" | tail -n +2 || echo "")
step "Cleanup build directory if present" step "Cleanup build directory if present"
@ -59,45 +68,45 @@ rm -rf ${BUILD_DIR}
step "Compile program" step "Compile program"
mkdir ${BUILD_DIR} mkdir ${BUILD_DIR}
build_params=("${go_params[@]}") build_params=(
build_params+=( -ldflags="-X main.version=${VERSION}"
-ldflags="-X main.version=${VERSION}"
) )
for package in "${PACKAGES[@]}"; do for package in "${PACKAGES[@]}"; do
for osarch in "${ARCHS[@]}"; do for osarch in "${ARCHS[@]}"; do
export GOOS=${osarch%%/*} export GOOS=${osarch%%/*}
export GOARCH=${osarch##*/} export GOARCH=${osarch##*/}
[[ ${GOOS} == "windows" ]] && suffix=".exe" || suffix="" [[ ${GOOS} == "windows" ]] && suffix=".exe" || suffix=""
outfile="${BUILD_DIR}/${package##*/}_${GOOS}_${GOARCH}${suffix}" outfile="${BUILD_DIR}/${package##*/}_${GOOS}_${GOARCH}${suffix}"
substep "Building for ${osarch} into ${outfile}" substep "Build for ${osarch} into ${outfile}"
go build \ go build \
-o "${outfile}" \ -o "${outfile}" \
"${build_params[@]}" \ "${go_params[@]}" \
"${package}" "${build_params[@]}" \
done "${package}"
done
done done
step "Generate binary SHASUMs" step "Generate binary SHASUMs"
cd ${BUILD_DIR} cd ${BUILD_DIR}
sha256sum * >SHA256SUMS sha256sum * >SHA256SUMS
step "Packing archives" step "Pack archives"
for file in *; do for file in *; do
if [ "${file}" = "SHA256SUMS" ]; then if [ "${file}" = "SHA256SUMS" ]; then
continue continue
fi fi
if [[ ${file} == *linux* ]]; then if [[ ${file} == *linux* ]]; then
tar -czf "${file%%.*}.tar.gz" "${file}" tar -czf "${file%%.*}.tar.gz" "${file}"
else else
zip "${file%%.*}.zip" "${file}" zip "${file%%.*}.zip" "${file}"
fi fi
rm "${file}" rm "${file}"
done done
step "Generate archive SHASUMs" step "Generate archive SHASUMs"
@ -105,38 +114,38 @@ sha256sum * >>SHA256SUMS
grep -v 'SHA256SUMS' SHA256SUMS >SHA256SUMS.tmp grep -v 'SHA256SUMS' SHA256SUMS >SHA256SUMS.tmp
mv SHA256SUMS.tmp SHA256SUMS mv SHA256SUMS.tmp SHA256SUMS
echo -e "\n\n=== Recorded checksums ===" log -e "\n\n=== Recorded checksums ==="
cat SHA256SUMS cat SHA256SUMS
if [[ ${FORCE_SKIP_UPLOAD} == "true" ]]; then if [[ ${FORCE_SKIP_UPLOAD} == "true" ]]; then
echo "Upload is skipped, stopping build now." log "Upload is skipped, stopping build now."
exit 0 exit 0
fi fi
step "Publish builds to Github" step "Publish builds to Github"
if ! git describe --tags --exact-match; then if ! git describe --tags --exact-match; then
echo "No tag present, stopping build now." log "No tag present, stopping build now."
exit 0 exit 0
fi fi
if [ -z "${GITHUB_TOKEN}" ]; then if [ -z "${GITHUB_TOKEN}" ]; then
echo 'Please set $GITHUB_TOKEN environment variable' log 'Please set $GITHUB_TOKEN environment variable'
exit 1 exit 1
fi fi
if [[ ${DRAFT} == "true" ]]; then if [[ ${DRAFT} == "true" ]]; then
step "Create a drafted release" step "Create a drafted release"
echo "${changelog}" | github-release release --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${DEPLOYMENT_TAG} --description - --draft || true echo "${changelog}" | ${tool_gopath}/bin/github-release release --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${DEPLOYMENT_TAG} --description - --draft || true
else else
step "Create a published release" step "Create a published release"
echo "${changelog}" | github-release release --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${DEPLOYMENT_TAG} --description - || true echo "${changelog}" | ${tool_gopath}/bin/github-release release --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${DEPLOYMENT_TAG} --description - || true
fi fi
step "Upload build assets" step "Upload build assets"
for file in *; do for file in *; do
echo "- ${file}" substep ${file}
github-release upload --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${file} --file ${file} ${tool_gopath}/bin/github-release upload --user ${GHUSER} --repo ${REPO} --tag ${DEPLOYMENT_TAG} --name ${file} --file ${file}
done done
cd - cd -