Skip to content

Commit

Permalink
feat(dev): Added publish-external-prerelease just task (#74)
Browse files Browse the repository at this point in the history
* Added publish-external-prerelease just task. Will convert an existing release draft to a prerelease.

* Update scripts/c8y-publish-release.sh

Co-authored-by: Reuben Miller <[email protected]>

* Update justfile

Co-authored-by: Reuben Miller <[email protected]>

* Introduce --pre-release flag and default argument parsing.

* use wait helper to wait for github release to be released

---------

Co-authored-by: Reuben Miller <[email protected]>
  • Loading branch information
Marco Stoffel and reubenmiller authored Feb 16, 2024
1 parent 1cb5584 commit fecd816
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ publish:
publish-external tag *args="":
cd {{justfile_directory()}} && ./scripts/c8y-publish-release.sh {{tag}} {{args}}

# Publish a given github release to Cumulocity (using external urls) but convert an existing draft to a prerelease beforehand
publish-external-prerelease tag:
cd {{justfile_directory()}} && ./scripts/c8y-publish-release.sh {{tag}} --pre-release

# Trigger a release (by creating a tag)
release:
git tag -a "{{VERSION}}" -m "{{VERSION}}"
Expand Down
91 changes: 84 additions & 7 deletions scripts/c8y-publish-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,59 @@
# * gh
# * go-c8y-cli
#

set -e

help() {
cat << EOT
Publish github releases
USAGE
$0 <TAG> [OPTIONS]
FLAGS
--pre-release Publish release as a pre-release (only if it set to draft)
EXAMPLES
$0 1.0.0 --pre-release
EOT
}

# Defaults
PRE_RELEASE=0

# Parse arguments
REST_ARGS=()
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
help
exit 0
;;

--pre-release)
PRE_RELEASE=1
;;

*)
REST_ARGS+=("$1")
;;
esac
shift
done

# Only set if rest arguments are defined
if [ ${#REST_ARGS[@]} -gt 0 ]; then
set -- "${REST_ARGS[@]}"
fi

TAG=
if [ $# -eq 0 ]; then
echo "missing required positional argument: TAG" >&2
echo
echo "Usage:"
echo
echo " $0 <TAG>"
echo
echo "Missing required argument" >&2
help
exit 1
fi

TAG="$1"

publish_version() {
Expand Down Expand Up @@ -48,6 +91,40 @@ publish_version() {
fi
}

wait_for_released() {
#
# Wait for a Github release to transition away from the draft state (e.g. either prerelease or release)
#
tag="$1"
retries=5
attempt=0

# Note: using exit/return code convention, 1=not ok, 0=ok
success=1
while [ "$attempt" -lt "$retries" ]; do
if [ "$(gh release view "$tag" --json isDraft --template "{{.isDraft}}")" = "false" ]; then
success=0
break
fi
attempt=$((attempt + 1))
echo "Waiting for release to be released..." >&2
sleep 3
done
return "$success"
}

# Set from draft to pre-release
if [ "$PRE_RELEASE" = 1 ]; then
IS_DRAFT=$(gh release view "$TAG" --json isDraft --template "{{.isDraft}}")
if [ "$IS_DRAFT" = "true" ]; then
echo "Update $TAG to prerelease."
gh release edit --draft=false --prerelease "$TAG"
if ! wait_for_released "$TAG"; then
echo "Could not update $TAG to prerelease. Exiting"
exit 5
fi
fi
fi
# Get assets from given tag
FILES=$(gh release view "$TAG" --json assets --jq '.assets[].url' | grep ".xz")

Expand Down

0 comments on commit fecd816

Please sign in to comment.