Skip to content

This should be it

This should be it #10

name: Build & Release
on:
push:
branches: [ main ]
tags:
- 'v*'
workflow_dispatch:
permissions:
contents: write
jobs:
build:
runs-on: ubuntu-latest
name: Mod Building
outputs:
version: ${{ steps.version.outputs.version }}
filename: ${{ steps.build.outputs.filename }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build project
id: build
run: |
./gradlew build
JAR_FILE=$(ls build/libs/*.jar | grep -v sources | head -1)
echo "filename=$(basename $JAR_FILE)" >> $GITHUB_OUTPUT
- name: Get version from gradle.properties
id: version
run: |
VERSION=$(grep "^mod_version=" gradle.properties | cut -d'=' -f2)
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: mod-build
path: build/libs/
release:
needs: build
runs-on: ubuntu-latest
name: Create Release
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Checkout (fetch tags)
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine tag and annotated message
id: taginfo
run: |
TAG=${GITHUB_REF##*/}
echo "tag=$TAG" >> $GITHUB_OUTPUT
# Try to read annotated tag contents
BODY=$(git for-each-ref --format='%(contents)' refs/tags/$TAG 2>/dev/null || true)
if [ -z "$BODY" ]; then
BODY=$(git tag -l --format='%(contents)' "$TAG" 2>/dev/null || true)
fi
# Fallback to commit message if no tag message
if [ -z "$BODY" ]; then
BODY=$(git log -1 --pretty=%B)
fi
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "$BODY" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: mod-build
path: ./artifacts
- name: Create GitHub Release
id: create_release
run: |
RESPONSE=$(curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/releases \
-d "{\"tag_name\":\"${{ steps.taginfo.outputs.tag }}\",\"name\":\"${{ steps.taginfo.outputs.tag }}\",\"body\":$(echo '${{ steps.taginfo.outputs.body }}' | jq -Rs .),\"draft\":false,\"prerelease\":false}")
UPLOAD_URL=$(echo $RESPONSE | jq -r '.upload_url' | sed 's/{?name,label}//')
echo "upload_url=$UPLOAD_URL" >> $GITHUB_OUTPUT
- name: Upload release asset
run: |
JAR_FILE=$(ls artifacts/*.jar | grep -v sources | head -1)
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/octet-stream" \
--data-binary @"$JAR_FILE" \
"${{ steps.create_release.outputs.upload_url }}?name=$(basename $JAR_FILE)"