2023-03-26 18:41:37 +03:00
|
|
|
#!/bin/bash
|
2023-04-01 12:12:56 +03:00
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-26 18:41:37 +03:00
|
|
|
|
2023-03-29 18:16:26 +03:00
|
|
|
set -e
|
|
|
|
|
|
|
|
if ${VERBOSE:-false}; then set -x; fi
|
2023-03-26 18:41:37 +03:00
|
|
|
|
2023-03-27 00:03:56 +03:00
|
|
|
: ${FORGEJO:=https://codeberg.org}
|
2023-03-29 18:16:26 +03:00
|
|
|
: ${REPO:=forgejo-integration/forgejo}
|
2023-03-26 18:41:37 +03:00
|
|
|
: ${RELEASE_DIR:=dist/release}
|
2023-05-26 14:03:49 +03:00
|
|
|
: ${TMP_DIR:=$(mktemp -d)}
|
|
|
|
: ${GNUPGHOME:=$TMP_DIR}
|
|
|
|
: ${BIN_DIR:=$TMP_DIR}
|
2023-03-26 18:41:37 +03:00
|
|
|
: ${TEA_VERSION:=0.9.0}
|
2024-02-12 00:40:17 +02:00
|
|
|
: ${OVERRIDE:=false}
|
2023-05-24 02:01:51 +03:00
|
|
|
: ${RETRY:=1}
|
|
|
|
: ${DELAY:=10}
|
2023-03-26 18:41:37 +03:00
|
|
|
|
2023-05-26 14:03:49 +03:00
|
|
|
export GNUPGHOME
|
|
|
|
|
2023-03-26 18:41:37 +03:00
|
|
|
setup_tea() {
|
|
|
|
if ! test -f $BIN_DIR/tea ; then
|
|
|
|
curl -sL https://dl.gitea.io/tea/$TEA_VERSION/tea-$TEA_VERSION-linux-amd64 > $BIN_DIR/tea
|
|
|
|
chmod +x $BIN_DIR/tea
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure_tag() {
|
2023-05-26 14:03:49 +03:00
|
|
|
if api GET repos/$REPO/tags/$TAG > $TMP_DIR/tag.json ; then
|
|
|
|
local sha=$(jq --raw-output .commit.sha < $TMP_DIR/tag.json)
|
2023-03-29 18:16:26 +03:00
|
|
|
if test "$sha" != "$SHA" ; then
|
2023-05-26 14:03:49 +03:00
|
|
|
cat $TMP_DIR/tag.json
|
2023-03-29 18:16:26 +03:00
|
|
|
echo "the tag SHA in the $REPO repository does not match the tag SHA that triggered the build: $SHA"
|
2023-03-26 18:41:37 +03:00
|
|
|
false
|
|
|
|
fi
|
|
|
|
else
|
2023-03-29 18:16:26 +03:00
|
|
|
api POST repos/$REPO/tags --data-raw '{"tag_name": "'$TAG'", "target": "'$SHA'"}'
|
2023-03-26 18:41:37 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:16:26 +03:00
|
|
|
upload_release() {
|
|
|
|
local assets=$(ls $RELEASE_DIR/* | sed -e 's/^/-a /')
|
|
|
|
local releasetype
|
2024-01-03 20:25:06 +02:00
|
|
|
( $PRERELEASE || echo "${TAG}" | grep -qi '\-rc' ) && export releasetype="--prerelease" && echo "Uploading as Pre-Release"
|
2023-03-29 18:16:26 +03:00
|
|
|
test ${releasetype+false} || echo "Uploading as Stable"
|
2023-03-26 18:41:37 +03:00
|
|
|
ensure_tag
|
2023-03-29 18:16:26 +03:00
|
|
|
anchor=$(echo $TAG | sed -e 's/^v//' -e 's/[^a-zA-Z0-9]/-/g')
|
2023-09-02 09:57:24 +03:00
|
|
|
if ! $BIN_DIR/tea release create $assets --repo $REPO --note "$RELEASENOTES" --tag $TAG --title $TAG --draft ${releasetype} >& $TMP_DIR/tea.log ; then
|
|
|
|
if grep --quiet 'Unknown API Error: 500' $TMP_DIR/tea.log && grep --quiet services/release/release.go:194 $TMP_DIR/tea.log ; then
|
|
|
|
echo "workaround v1.20 race condition https://codeberg.org/forgejo/forgejo/issues/1370"
|
|
|
|
sleep 10
|
|
|
|
$BIN_DIR/tea release create $assets --repo $REPO --note "$RELEASENOTES" --tag $TAG --title $TAG --draft ${releasetype}
|
|
|
|
else
|
|
|
|
cat $TMP_DIR/tea.log
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
2023-05-24 02:01:15 +03:00
|
|
|
release_draft false
|
|
|
|
}
|
|
|
|
|
|
|
|
release_draft() {
|
|
|
|
local state="$1"
|
|
|
|
|
|
|
|
local id=$(api GET repos/$REPO/releases/tags/$TAG | jq --raw-output .id)
|
|
|
|
api PATCH repos/$REPO/releases/$id --data-raw '{"draft": '$state'}'
|
2023-03-26 18:41:37 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 14:03:49 +03:00
|
|
|
sign_release() {
|
|
|
|
local passphrase
|
|
|
|
if test -s "$GPG_PASSPHRASE"; then
|
|
|
|
passphrase="--passphrase-file $GPG_PASSPHRASE"
|
|
|
|
fi
|
|
|
|
gpg --import --no-tty --pinentry-mode loopback $passphrase "$GPG_PRIVATE_KEY"
|
|
|
|
for asset in $RELEASE_DIR/* ; do
|
|
|
|
if [[ $asset =~ .sha256$ ]] ; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
gpg --armor --detach-sign --no-tty --pinentry-mode loopback $passphrase < $asset > $asset.asc
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_sign_release() {
|
|
|
|
if test -s "$GPG_PRIVATE_KEY"; then
|
|
|
|
sign_release
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-02-12 00:40:17 +02:00
|
|
|
maybe_override() {
|
|
|
|
if test "$OVERRIDE" = "false"; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
api DELETE repos/$REPO/releases/tags/$TAG >& /dev/null || true
|
|
|
|
api DELETE repos/$REPO/tags/$TAG >& /dev/null || true
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:16:26 +03:00
|
|
|
upload() {
|
2023-03-26 18:41:37 +03:00
|
|
|
setup_api
|
|
|
|
setup_tea
|
2023-08-25 12:12:23 +03:00
|
|
|
rm -f ~/.config/tea/config.yml
|
2023-03-31 01:09:26 +03:00
|
|
|
GITEA_SERVER_TOKEN=$TOKEN $BIN_DIR/tea login add --url $FORGEJO
|
2023-05-26 14:03:49 +03:00
|
|
|
maybe_sign_release
|
2024-02-12 00:40:17 +02:00
|
|
|
maybe_override
|
2023-03-29 18:16:26 +03:00
|
|
|
upload_release
|
2023-03-26 18:41:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_api() {
|
2023-03-27 00:03:56 +03:00
|
|
|
if ! which jq curl ; then
|
2023-03-30 16:22:00 +03:00
|
|
|
apt-get -qq update
|
2023-03-30 02:08:18 +03:00
|
|
|
apt-get install -y -qq jq curl wget
|
2023-03-26 18:41:37 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
api() {
|
|
|
|
method=$1
|
|
|
|
shift
|
|
|
|
path=$1
|
|
|
|
shift
|
|
|
|
|
2023-03-29 18:16:26 +03:00
|
|
|
curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $TOKEN" "$@" $FORGEJO/api/v1/$path
|
2023-03-26 18:41:37 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 02:01:51 +03:00
|
|
|
wait_release() {
|
|
|
|
local ready=false
|
|
|
|
for i in $(seq $RETRY); do
|
2023-05-26 14:03:49 +03:00
|
|
|
if api GET repos/$REPO/releases/tags/$TAG | jq --raw-output .draft > $TMP_DIR/draft; then
|
|
|
|
if test "$(cat $TMP_DIR/draft)" = "false"; then
|
2023-05-24 02:01:51 +03:00
|
|
|
ready=true
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
echo "release $TAG is still a draft"
|
|
|
|
else
|
|
|
|
echo "release $TAG does not exist yet"
|
|
|
|
fi
|
|
|
|
echo "waiting $DELAY seconds"
|
|
|
|
sleep $DELAY
|
|
|
|
done
|
|
|
|
if ! $ready ; then
|
|
|
|
echo "no release for $TAG"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:16:26 +03:00
|
|
|
download() {
|
2023-03-26 18:41:37 +03:00
|
|
|
setup_api
|
2023-05-24 02:01:51 +03:00
|
|
|
wait_release
|
2023-03-26 18:41:37 +03:00
|
|
|
(
|
|
|
|
mkdir -p $RELEASE_DIR
|
|
|
|
cd $RELEASE_DIR
|
2023-05-26 14:03:49 +03:00
|
|
|
api GET repos/$REPO/releases/tags/$TAG > $TMP_DIR/assets.json
|
|
|
|
jq --raw-output '.assets[] | "\(.name) \(.browser_download_url)"' < $TMP_DIR/assets.json | while read name url ; do
|
2023-03-26 18:41:37 +03:00
|
|
|
wget --quiet -O $name $url
|
|
|
|
done
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
missing() {
|
2023-03-29 18:16:26 +03:00
|
|
|
echo need upload or download argument got nothing
|
2023-03-26 18:41:37 +03:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
${@:-missing}
|