Skip to content

Putting the gun to GitHub's head #7

Putting the gun to GitHub's head

Putting the gun to GitHub's head #7

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
- 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
uses: actions/create-release@v1
with:
tag_name: ${{ steps.taginfo.outputs.tag }}
release_name: ${{ steps.taginfo.outputs.tag }}
body: ${{ steps.taginfo.outputs.body }}
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload release asset
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./artifacts/${{ needs.build.outputs.filename }}
asset_name: ${{ needs.build.outputs.filename }}
asset_content_type: application/java-archive