From e40fc6ef848fb501d3e523b6604b540f96e18ecc Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Mon, 1 Jan 2024 17:05:41 +0100 Subject: [PATCH] Add `publish-hangar` 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 Hangar Markdown and sends it to Hangar 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, `HANGAR_TOKEN`, which is just an API key for the Hangar API. The token was created in the Hangar profile settings (API Keys), and its only permission is `create_version`. In order to properly upload a new build to Hangar, we need to construct a somewhat complex JSON object. This is because the Hangar API allows for publishing releases on multiple platforms and for multiple versions, which makes the simple use case for MobArena's single file upload look a bit overcomplicated. Unlike the CurseForge API, the Hangar API supports "normal" platform version strings, so we don't need to map anything. It also supports patch version wildcards, so we can get away with `1.18.x`, `1.19.x`, etc. for each version supported. The API only uses the API key for authentication, which means we need to grab a JWT and use that for the actual upload request. Note that the `pluginDependencies` property is currently required, but it can be left empty. The workflow can be invoked directly via the `workflow_dispatch` event, which might come in handy if something in the pipeline breaks. The Hangar base URL and project slug 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. --- .github/workflows/publish-hangar.yml | 79 ++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/publish-hangar.yml diff --git a/.github/workflows/publish-hangar.yml b/.github/workflows/publish-hangar.yml new file mode 100644 index 0000000..753419e --- /dev/null +++ b/.github/workflows/publish-hangar.yml @@ -0,0 +1,79 @@ +name: publish-hangar + +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 Hangar + run: | + echo 'Extract release notes' + changelog=$(scripts/extract-release-notes -f hangar "${TAG_NAME}") + + echo 'Create version upload file' + cat << EOF > version-upload.jq + { + version: \$version, + channel: "Release", + description: \$changelog, + platformDependencies: { + "PAPER": [ + "1.13.x", + "1.14.x", + "1.15.x", + "1.16.x", + "1.17.x", + "1.18.x", + "1.19.x", + "1.20.x" + ] + }, + pluginDependencies: {}, + files: [ + { platforms: ["PAPER"] } + ] + } + EOF + + jq -c -n \ + --arg version "${TAG_NAME}" \ + --arg changelog "${changelog}" \ + -f version-upload.jq \ + > version-upload.json + + echo 'Authenticate with Hangar' + base_url='https://hangar.papermc.io/api/v1' + key=${{ secrets.HANGAR_TOKEN }} + jwt=$(curl -s -X POST "${base_url}/authenticate?apiKey=${key}" | jq -r '.token') + + echo 'Publish build to Hangar' + project_slug='MobArena' + curl -s -X POST "${base_url}/projects/${project_slug}/upload" \ + -H "Authorization: ${jwt}" \ + -F 'versionUpload=