2023-03-26 15:41:37 +00:00
#!/bin/bash
2023-04-01 09:12:56 +00:00
# SPDX-License-Identifier: MIT
2023-03-26 15:41:37 +00:00
2023-03-29 15:16:26 +00:00
set -e
if ${ VERBOSE :- false } ; then set -x; fi
2023-03-26 15:41:37 +00:00
2023-03-26 21:03:56 +00:00
: ${ FORGEJO : =https : //codeberg.org }
2023-03-29 15:16:26 +00:00
: ${ REPO : =forgejo-integration/forgejo }
2024-07-27 12:06:55 +00:00
: ${ TITLE : = $TAG }
2023-03-26 15:41:37 +00:00
: ${ RELEASE_DIR : =dist/release }
2024-07-08 18:49:13 +00:00
: ${ DOWNLOAD_LATEST : =false }
2023-05-26 11:03:49 +00:00
: ${ TMP_DIR : = $( mktemp -d) }
: ${ GNUPGHOME : = $TMP_DIR }
: ${ BIN_DIR : = $TMP_DIR }
2023-03-26 15:41:37 +00:00
: ${ TEA_VERSION : =0.9.0 }
2024-02-11 22:40:17 +00:00
: ${ OVERRIDE : =false }
2024-11-19 15:38:34 +00:00
: ${ HIDE_ARCHIVE_LINK : =false }
2023-05-23 23:01:51 +00:00
: ${ RETRY : =1 }
: ${ DELAY : =10 }
2023-03-26 15:41:37 +00:00
2023-05-26 11:03:49 +00:00
export GNUPGHOME
2023-03-26 15:41:37 +00:00
setup_tea( ) {
2024-12-09 05:38:35 +00:00
if ! test -f " $BIN_DIR " /tea ; then
ARCH = $( dpkg --print-architecture)
curl -sL https://dl.gitea.io/tea/$TEA_VERSION /tea-$TEA_VERSION -linux-" $ARCH " > " $BIN_DIR " /tea
chmod +x " $BIN_DIR " /tea
2023-03-26 15:41:37 +00:00
fi
}
ensure_tag( ) {
2024-12-09 05:38:35 +00:00
if api GET repos/$REPO /tags/" $TAG " > " $TMP_DIR " /tag.json ; then
local sha = $( jq --raw-output .commit.sha < " $TMP_DIR " /tag.json)
if test " $sha " != " $SHA " ; then
cat " $TMP_DIR " /tag.json
echo " the tag SHA in the $REPO repository does not match the tag SHA that triggered the build: $SHA "
false
fi
2023-03-26 15:41:37 +00:00
else
2024-12-09 05:38:35 +00:00
api POST repos/$REPO /tags --data-raw '{"tag_name": "' " $TAG " '", "target": "' " $SHA " '"}'
2023-03-26 15:41:37 +00:00
fi
}
2023-03-29 15:16:26 +00:00
upload_release( ) {
2024-12-09 05:38:35 +00:00
# assets is defined as a list of arguments, where values may contain whitespace and need to be quoted like this -a "my file.txt" -a "file.txt".
# It is expanded using "${assets[@]}" which preserves the separation of arguments and not split whitespace containing values.
# For reference, see https://github.com/koalaman/shellcheck/wiki/SC2086#exceptions
local assets = ( )
for file in " $RELEASE_DIR " /*; do
assets = ( " ${ assets [@] } " -a " $file " )
done
2024-10-29 16:44:34 +00:00
if $PRERELEASE || echo " ${ TAG } " | grep -qi '\-rc' ; then
2024-12-09 05:38:35 +00:00
releaseType = "--prerelease"
2024-10-29 16:44:34 +00:00
echo "Uploading as Pre-Release"
else
echo "Uploading as Stable"
fi
2023-03-26 15:41:37 +00:00
ensure_tag
2024-12-09 05:38:35 +00:00
if ! " $BIN_DIR " /tea release create " ${ assets [@] } " --repo $REPO --note " $RELEASENOTES " --tag " $TAG " --title " $TITLE " --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
2024-10-29 15:08:41 +00:00
echo "workaround v1.20 race condition https://codeberg.org/forgejo/forgejo/issues/1370"
sleep 10
2024-12-09 05:38:35 +00:00
" $BIN_DIR " /tea release create " ${ assets [@] } " --repo $REPO --note " $RELEASENOTES " --tag " $TAG " --title " $TITLE " --draft ${ releaseType }
2024-10-29 15:08:41 +00:00
else
2024-12-09 05:38:35 +00:00
cat " $TMP_DIR " /tea.log
2024-10-29 15:08:41 +00:00
return 1
fi
2023-09-02 06:57:24 +00:00
fi
2024-10-29 15:08:41 +00:00
maybe_use_release_note_assistant
2023-05-23 23:01:15 +00:00
release_draft false
}
release_draft( ) {
local state = " $1 "
2024-12-09 05:38:35 +00:00
local id = $( api GET repos/$REPO /releases/tags/" $TAG " | jq --raw-output .id)
2024-11-19 15:38:34 +00:00
2024-12-09 05:38:35 +00:00
api PATCH repos/$REPO /releases/" $id " --data-raw '{"draft": ' " $state " ', "hide_archive_links": ' $HIDE_ARCHIVE_LINK '}'
2023-03-26 15:41:37 +00:00
}
2024-10-29 15:08:41 +00:00
maybe_use_release_note_assistant( ) {
if " $RELEASE_NOTES_ASSISTANT " ; then
curl --fail -s -S -o rna https://code.forgejo.org/forgejo/release-notes-assistant/releases/download/v1.2.3/release-notes-assistant
chmod +x ./rna
2024-12-09 05:38:35 +00:00
./rna --storage release --storage-location " $TAG " --forgejo-url " $SCHEME " ://placeholder:" $TOKEN " @" $HOST " --repository $REPO --token " $TOKEN " release " $TAG "
2024-10-29 15:08:41 +00:00
fi
}
2023-05-26 11:03:49 +00:00
sign_release( ) {
local passphrase
if test -s " $GPG_PASSPHRASE " ; then
2024-12-09 05:38:35 +00:00
passphrase = " --passphrase-file $GPG_PASSPHRASE "
2023-05-26 11:03:49 +00:00
fi
gpg --import --no-tty --pinentry-mode loopback $passphrase " $GPG_PRIVATE_KEY "
2024-12-09 05:38:35 +00:00
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
2023-05-26 11:03:49 +00:00
done
}
maybe_sign_release( ) {
if test -s " $GPG_PRIVATE_KEY " ; then
2024-12-09 05:38:35 +00:00
sign_release
2023-05-26 11:03:49 +00:00
fi
}
2024-02-11 22:40:17 +00:00
maybe_override( ) {
if test " $OVERRIDE " = "false" ; then
2024-12-09 05:38:35 +00:00
return
2024-02-11 22:40:17 +00:00
fi
2024-12-09 05:38:35 +00:00
api DELETE repos/$REPO /releases/tags/" $TAG " >& /dev/null || true
api DELETE repos/$REPO /tags/" $TAG " >& /dev/null || true
2024-02-11 22:40:17 +00:00
}
2023-03-29 15:16:26 +00:00
upload( ) {
2023-03-26 15:41:37 +00:00
setup_api
setup_tea
2023-08-25 09:12:23 +00:00
rm -f ~/.config/tea/config.yml
2024-12-09 05:38:35 +00:00
GITEA_SERVER_TOKEN = $TOKEN " $BIN_DIR " /tea login add --url $FORGEJO
2023-05-26 11:03:49 +00:00
maybe_sign_release
2024-02-11 22:40:17 +00:00
maybe_override
2023-03-29 15:16:26 +00:00
upload_release
2023-03-26 15:41:37 +00:00
}
setup_api( ) {
2023-03-26 21:03:56 +00:00
if ! which jq curl ; then
2024-12-09 05:38:35 +00:00
apt-get -qq update
apt-get install -y -qq jq curl
2023-03-26 15:41:37 +00:00
fi
}
api( ) {
method = $1
shift
path = $1
shift
2024-12-09 05:38:35 +00:00
curl --fail -X " $method " -sS -H "Content-Type: application/json" -H " Authorization: token $TOKEN " " $@ " $FORGEJO /api/v1/" $path "
2023-03-26 15:41:37 +00:00
}
2023-05-23 23:01:51 +00:00
wait_release( ) {
local ready = false
for i in $( seq $RETRY ) ; do
2024-12-09 05:38:35 +00: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
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
2023-05-23 23:01:51 +00:00
done
if ! $ready ; then
2024-12-09 05:38:35 +00:00
echo " no release for $TAG "
return 1
2023-05-23 23:01:51 +00:00
fi
}
2023-03-29 15:16:26 +00:00
download( ) {
2023-03-26 15:41:37 +00:00
setup_api
(
2024-12-09 05:38:35 +00:00
mkdir -p $RELEASE_DIR
cd $RELEASE_DIR
2024-07-08 18:49:13 +00:00
if [ [ ${ DOWNLOAD_LATEST } = = "true" ] ] ; then
echo "Downloading the latest release"
2024-12-09 05:38:35 +00:00
api GET repos/$REPO /releases/latest > " $TMP_DIR " /assets.json
2024-07-08 18:49:13 +00:00
elif [ [ ${ DOWNLOAD_LATEST } = = "false" ] ] ; then
wait_release
echo " Downloading tagged release ${ TAG } "
2024-12-09 05:38:35 +00:00
api GET repos/$REPO /releases/tags/" $TAG " > " $TMP_DIR " /assets.json
2024-07-08 18:49:13 +00:00
fi
2024-12-09 05:38:35 +00:00
jq --raw-output '.assets[] | "\(.browser_download_url) \(.name)"' < " $TMP_DIR " /assets.json | while read url name ; do # `name` may contain whitespace, therefore, it must be last
curl --fail -H " Authorization: token $TOKEN " -o " $name " -L " $url "
done
2023-03-26 15:41:37 +00:00
)
}
missing( ) {
2023-03-29 15:16:26 +00:00
echo need upload or download argument got nothing
2023-03-26 15:41:37 +00:00
exit 1
}
${ @ :- missing }