Add `publish-curseforge` GitHub Actions workflow.

Introduces a new workflow that runs when a new build has been published
on GitHub Releases. It converts the release notes to CurseForge HTML and
sends it to CurseForge along with the jar-file.

Note: The workflow currently relies on the version string being appended
to the filename of the jar-file. Without it, the file reference in the
`curl` request that uploads the build would need to change.

The workflow references a new secret, `CURSEFORGE_TOKEN`, which is just
an API key for the CurseForge API. The token was created on CurseForge
under profile settings (My API Tokens).

In order to properly upload a new build to CurseForge, we need a list of
"game version IDs", which isn't completely trivial. The API gives us a
means of looking up _all_ Minecraft game version IDs, but we then have
to manually filter out the ones that don't apply to Bukkit plugins, as
there are duplicate entries for each Minecraft version, and only some of
them work for Bukkit plugins (which turns out to be the ones with game
version type ID 1). The structure of the `metadata` field combined with
how incredibly difficult bash can be to work with has resulted in some
gnarly text processing trying to filter the JSON response and turning it
into a list for use in the `jq` template, but it gets the job done.

The CurseForge base URL and project ID are both hardcoded, and things
would probably be cleaner if they were made into variables, but we don't
need this workflow anywhere else, so it's fine for now.

The workflow can be invoked directly via the `workflow_dispatch` event,
which might come in handy if something in the pipeline breaks.

Lots of inspiration was found in the probably really great GitHub Action
`curseforge-upload` [1]. We could have probably just used that, but it's
nice to have full control of the process. At any rate, thanks to itsmeow
and NotMyFault for publishing their work.

---

[1] https://github.com/itsmeow/curseforge-upload
This commit is contained in:
Andreas Troelsen 2024-01-01 16:46:38 +01:00
parent b881943656
commit be6fd85a6d
1 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,77 @@
name: publish-curseforge
on:
release:
types:
- 'released'
workflow_dispatch:
inputs:
tag_name:
description: 'The tag name of the release to publish'
required: true
type: string
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
env:
TAG_NAME: ${{ github.event.release.tag_name || inputs.tag_name }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download release assets
run: gh release download "${TAG_NAME}"
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Publish to CurseForge
run: |
echo 'Extract release notes'
changelog=$(scripts/extract-release-notes -f curse "${TAG_NAME}")
echo 'Look up game version IDs'
game_version_type_id=1
game_version_names='"1.20","1.19","1.18","1.17","1.16","1.15","1.14","1.13"'
type_condition="(.gameVersionTypeID == ${game_version_type_id})"
name_condition="(.name | startswith(${game_version_names}))"
game_version_ids=$(
curl -s -X GET 'https://minecraft.curseforge.com/api/game/versions' \
-H "X-Api-Token: ${{ secrets.CURSEFORGE_TOKEN }}" \
| jq -c ".[] | select(${type_condition} and ${name_condition}) | .id" \
| paste -sd, - \
)
echo 'Create metadata file'
cat << EOF > metadata.jq
{
changelog: \$changelog,
changelogType: "html",
displayName: \$displayName,
gameVersions: \$gameVersions,
releaseType: "beta"
}
EOF
jq -c -n \
--arg changelog "${changelog}" \
--arg displayName "MobArena v${TAG_NAME}" \
--argjson gameVersions "[${game_version_ids}]" \
-f metadata.jq \
> metadata.json
echo 'Publish build to CurseForge'
base_url='https://minecraft.curseforge.com'
project_id=31265
curl -s -X POST "${base_url}/api/projects/${project_id}/upload-file" \
-H "X-Api-Token: ${{ secrets.CURSEFORGE_TOKEN }}" \
-F 'metadata=<metadata.json' \
-F "file=@MobArena-${TAG_NAME}.jar"