monotux.tech

Gitea package upload script

gitea, qmk, bash

I needed a quick way to upload QMK build artefacts to my gitea/forgejo instance, and since I’m lazy I want the script to bump the version number as well.

Luckily, gitea/forgejo has an API so it was fairly straight forward – just remember to:

  • Install jq and curl, and
  • add below to your ~/.bash_profile after replacing what needs replacing, and
  • create a ~/.netrc file containing your credentials
function gitea-upload {
    # Change these variables
    fname="${1%.*}"
    owner="GITEAOWNERHERE"
    url="https://gitea.example.com"

    # Extract the latest version and bump it
    # Updated 2024-12-26 to fix a bug caused by trying to sort the list
    # returned from forgejo/gitea.  The list is already sorted with the
    # latest version first.
    # Updated 2025-03-02, use search on the owner instead.  That will find all
    # packages if you have more than a few already.
    nver=$(curl -s --netrc-file ~/.netrc \
                "${url}/api/v1/packages/${owner}?q=${fname}" \
               | jq --arg fname "$fname" -c '.[] | select(.name == $fname) | .version' \
               | head -n 1 \
               | tr -d '"' \
               | awk -F. -v OFS=. '{$NF += 1 ; print}' )

    # Fall back to 1.0.0 if nver is empty
    version="${nver:-1.0.0}"

    echo "version: ${version}"
    curl -s --netrc-file ~/.netrc \
         --upload-file "$1" \
         "${url}/api/packages/${owner}/generic/${fname}/${version}/${fname}"
}

# Usage:
# gitea-upload filename.bin

You might want to tune the package name, I’m just removing the file extention from the files which works for my use case.

In case you need an example for the ~/.netrc file:

machine gitea.example.com login YOUR-USERNAME-HERE password YOUR-PASSWORD-HERE