Skip to content

Commit

Permalink
fix lifecycle release script
Browse files Browse the repository at this point in the history
  • Loading branch information
jancajthaml authored Jul 30, 2020
1 parent c33cac8 commit b2d4e81
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ release:
@docker-compose \
run \
--rm release \
--version $(VERSION)+$(META) \
--version $(VERSION) \
--token ${GITHUB_RELEASE_TOKEN}

.PHONY: bbtest
Expand Down
107 changes: 66 additions & 41 deletions dev/lifecycle/release
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

set -eu
trap exit INT TERM
Expand All @@ -18,12 +18,12 @@ done

################################################################################

if [ ! "${VERSION}" ] ; then
if [ -z "${VERSION}" ] ; then
(>&2 echo "[error] version not provided")
exit 1
fi

if [ ! "${ACCESS_TOKEN}" ] ; then
if [ -z "${ACCESS_TOKEN}" ] ; then
(>&2 echo "[error] github access token not provided")
exit 1
fi
Expand All @@ -42,25 +42,57 @@ REPO=${REPO}

################################################################################

echo "[info] start release of ${REPO}:${VERSION}"
lifecycle::release::info() {
wget \
-O - \
-o /dev/null \
--header "Authorization: token ${ACCESS_TOKEN}" \
"https://api.github.com/repos/${REPO}/releases/tags/${VERSION}" \
|| echo '{ "id": null, "upload_url": "" }'
}

RELEASE_INFO="https://api.github.com/repos/${REPO}/releases/tags/${VERSION}"
response=$(
curl \
--silent \
-H "Authorization: token ${ACCESS_TOKEN}" \
${RELEASE_INFO} \
)
lifecycle::release::upload_uri() {
UPLOAD_URL=$(lifecycle::release::info | jq -r .upload_url | sed -e "s/{?name,label}//")
if [ -z "${UPLOAD_URL}" ] ; then
(>&2 echo "[error] failed to obtain upload_url")
exit 1
fi
echo "${UPLOAD_URL}"
}

RELEASE_ID=$(echo "${response}" | jq -r .id)
RELEASE_URI="https://api.github.com/repos/${REPO}/releases"
lifecycle::release::upload_asset() {
local uri="$1"
if [ -z ${uri} ] ; then
return
fi
local file="$2"
if [ -z ${file} -o ! -f ${file} ] ; then
return
fi
if [ ${file: -4} != ".deb" -a ! -x ${file} ] ; then
return
fi

echo "[info] uploading $(basename ${file}) to Github release ${VERSION}"

wget \
-O /dev/null \
-o /dev/null \
--method POST \
--body-file "${file}" \
--header "Authorization: token ${ACCESS_TOKEN}" \
--header "Content-Type: application/binary" \
"${uri}?name=$(basename ${file})"
}

if [ "${RELEASE_ID}" != "null" ] ; then
(>&2 echo "[error] release ${VERSION} already exists")
exit 1
else
echo "[info] creating new release ${VERSION}"
fi
lifecycle::release::assert_release() {
RELEASE_ID=$(lifecycle::release::info | jq -r .id)

if [ "${RELEASE_ID}" != "null" ] ; then
return
fi

echo "[info] creating new release ${VERSION} at Github"

PAYLOAD=$(cat <<-END
{
Expand All @@ -74,30 +106,23 @@ PAYLOAD=$(cat <<-END
END
)

response=$(
curl \
-X POST \
-H "Authorization: token ${ACCESS_TOKEN}" \
--fail \
--silent \
--data "$(echo ${PAYLOAD})" \
${RELEASE_URI}
)
wget \
-O /dev/null \
-o /dev/null \
--method POST \
--header "Authorization: token ${ACCESS_TOKEN}" \
--header="Content-Type: application/json" \
--body-data="$(echo ${PAYLOAD})" \
"https://api.github.com/repos/${REPO}/releases"
}

################################################################################

lifecycle::release::assert_release

UPLOAD_URL=$(echo ${response} | jq -r .upload_url | sed -e "s/{?name,label}//")
upload_uri=$(lifecycle::release::upload_uri)

for asset in $(ls -d -1 $(pwd)/packaging/bin/*) ; do
echo "[info] uploading ${asset}"

curl \
--fail \
-X POST \
-# \
--output /dev/null \
--data-binary "@${asset}" \
-H "Authorization: token $ACCESS_TOKEN" \
-H "Content-Type: application/binary" \
"${UPLOAD_URL}?name=$(basename ${asset})"
lifecycle::release::upload_asset ${upload_uri} ${asset}
done

echo "[info] released ${VERSION}"

0 comments on commit b2d4e81

Please sign in to comment.