111 lines
3.4 KiB
YAML
111 lines
3.4 KiB
YAML
name: Release
|
|
|
|
"on":
|
|
push:
|
|
tags:
|
|
- "**"
|
|
|
|
permissions:
|
|
contents: read
|
|
releases: write
|
|
|
|
jobs:
|
|
release:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Build changelog
|
|
id: changelog
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
|
|
current_tag="${GITHUB_REF_NAME}"
|
|
previous_tag=""
|
|
|
|
if previous_tag="$(git describe --tags --abbrev=0 "${current_tag}^" 2>/dev/null)"; then
|
|
range="${previous_tag}..${current_tag}"
|
|
{
|
|
printf '## Changelog\n\n'
|
|
printf 'Changes since `%s`.\n\n' "${previous_tag}"
|
|
git log --reverse --pretty=format:'- %h %s' "${range}"
|
|
printf '\n'
|
|
} >CHANGELOG.md
|
|
else
|
|
{
|
|
printf '## Changelog\n\n'
|
|
printf 'Initial release.\n\n'
|
|
git log --reverse --pretty=format:'- %h %s' "${current_tag}"
|
|
printf '\n'
|
|
} >CHANGELOG.md
|
|
fi
|
|
|
|
- name: Create or update release
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
|
|
current_tag="${GITHUB_REF_NAME}"
|
|
api_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases"
|
|
release_by_tag_url="${api_url}/tags/${current_tag}"
|
|
|
|
json_escape() {
|
|
sed ':a;N;$!ba;s/\\/\\\\/g;s/"/\\"/g;s/\t/\\t/g;s/\r//g;s/\n/\\n/g'
|
|
}
|
|
|
|
body="$(json_escape < CHANGELOG.md)"
|
|
payload="$(printf '{"tag_name":"%s","name":"%s","body":"%s","draft":false,"prerelease":false}' \
|
|
"${current_tag}" \
|
|
"${current_tag}" \
|
|
"${body}")"
|
|
|
|
http_code="$(
|
|
curl --silent --show-error --output release.json --write-out '%{http_code}' \
|
|
--header "Authorization: token ${GITEA_TOKEN}" \
|
|
--header 'Accept: application/json' \
|
|
"${release_by_tag_url}"
|
|
)"
|
|
|
|
case "${http_code}" in
|
|
200)
|
|
release_id="$(
|
|
tr '{,' '\n' < release.json |
|
|
sed -n 's/^[[:space:]]*"id":[[:space:]]*\([0-9][0-9]*\)$/\1/p' |
|
|
head -n1
|
|
)"
|
|
|
|
if [ -z "${release_id}" ]; then
|
|
echo "failed to parse release id from existing release response" >&2
|
|
exit 1
|
|
fi
|
|
|
|
curl --silent --show-error --fail \
|
|
--request PATCH \
|
|
--header "Authorization: token ${GITEA_TOKEN}" \
|
|
--header 'Accept: application/json' \
|
|
--header 'Content-Type: application/json' \
|
|
--data "${payload}" \
|
|
"${api_url}/${release_id}" >/dev/null
|
|
;;
|
|
404)
|
|
curl --silent --show-error --fail \
|
|
--request POST \
|
|
--header "Authorization: token ${GITEA_TOKEN}" \
|
|
--header 'Accept: application/json' \
|
|
--header 'Content-Type: application/json' \
|
|
--data "${payload}" \
|
|
"${api_url}" >/dev/null
|
|
;;
|
|
*)
|
|
echo "unexpected status code while looking up release: ${http_code}" >&2
|
|
cat release.json >&2
|
|
exit 1
|
|
;;
|
|
esac
|