Skip to content

Build and Publish mod #26

Build and Publish mod

Build and Publish mod #26

# This workflow must be manually triggered. It accepts a version and a release type.
# First it sets the version in gradle.properties to the new version and pushes it.
# Then it generates a log, based on commits.
# After that builds a jar and uploads it to curseforge/modrinth/GitHub releases
name: Build and Publish mod
on:
workflow_dispatch:
inputs:
version:
description: 'Mod version'
required: true
release-type:
description: 'Release type'
type: choice
default: 'beta'
options:
- 'release'
- 'beta'
- 'alpha'
publish-cf:
description: Publish to CF
default: true
type: boolean
publish-mr:
description: Publish to MR
default: true
type: boolean
publish-gh:
description: Publish to Github
default: true
type: boolean
publish-maven:
description: Publish to maven
default: true
type: boolean
overwrite:
description: 'Overwrite current tag (if exists)'
default: false
required: true
type: boolean
env:
CHANGELOG_LOCATION: "build/gh_changelog.md"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/[email protected]
with:
token: ${{ secrets.PAT }}
- name: Read current mod version
id: read_version
run: |
mod_version=$(grep "modVersion" gradle.properties | cut -d'=' -f2 | tr -d '[:space:]')
echo "Current mod version: $mod_version"
if [ "$mod_version" == "${{inputs.version}}" ]; then
echo "UPDATED=true" >> $GITHUB_ENV
else
echo "UPDATED=false" >> $GITHUB_ENV
fi
# Abort if a tag already exists and the mod is updated to that version and overwriting is not allowed
# If the tag does not exist force publish to gh if publishing to cf/mr (for changelog)
- name: Check if tag already exists
run: |
if git rev-parse --verify --quiet "v${{ github.event.inputs.version }}"; then
if [ "${{ inputs.publish-gh }}" == "true" ] && [ "$UPDATED" == "true" ] && [ "${{ inputs.overwrite }}" == "false" ]; then
echo "Version tag ${{ github.event.inputs.version }} already exists and the mod is updated to that version and overwriting is forbidden."
echo "Aborting workflow!"
exit 1
fi
echo "publish-gh=${{ inputs.publish-gh }}" >> $GITHUB_ENV
else
if [ "${{ inputs.publish-cf }}" == "true" ] || [ "${{ inputs.publish-mr }}" == "true" ]; then
echo "publish-gh=true" >> $GITHUB_ENV
else
echo "publish-gh=${{ inputs.publish-gh }}" >> $GITHUB_ENV
fi
fi
- name: Set version
if: env.UPDATED == 'false' # only change new version if it's not already updated
run: sed -i "s/modVersion.*=.*/modVersion = ${{ github.event.inputs.version }}/g" gradle.properties
- name: Commit and push gradle.properties
if: env.UPDATED == 'false' # only push new version if it's not already updated
uses: stefanzweifel/[email protected]
with:
commit_message: "Bump version to ${{ github.event.inputs.version }}"
commit_options: "--no-verify"
file_pattern: gradle.properties
tagging_message: "v${{ github.event.inputs.version }}"
- name: Check for publishing
run: |
if [ "$publish-gh" == "false" ] && [ "${{ inputs.publish-cf-mr }}" == "false" ] && [ "${{ inputs.publish-maven }}" == "false" ]; then
echo "Not publishing to Github, Curse, Modrinth or Maven."
exit 0
fi
- name: Setup Java
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: '8'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Setup Gradle
uses: gradle/actions/[email protected]
- name: Build Project
run: ./gradlew build
- name: Publish to GitHub
if: env.publish-gh == 'true'
uses: softprops/[email protected]
with:
tag_name: "v${{ inputs.version }}"
token: ${{ secrets.PAT }}
files: "build/libs/*.jar"
generate_release_notes: true
fail_on_unmatched_files: true
- name: Get Changelog
run: |
mkdir -p build
RELEASE_URL="https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ inputs.version }}"
RELEASE_JSON=$(curl -sSL $RELEASE_URL)
CHANGELOG="$(echo $RELEASE_JSON | jq -r '.body')"
if [ "$CHANGELOG" == "null" ]; then
echo "No changelog found" > $CHANGELOG_LOCATION
else
echo "$CHANGELOG" > $CHANGELOG_LOCATION
fi
- name: Publish to Curseforge
if: ${{ inputs.publish-cf }}
run: ./gradlew curseforge
env:
CURSEFORGE_API_KEY: ${{ secrets.CURSEFORGE_TOKEN }}
CHANGELOG_LOCATION: ${{ env.CHANGELOG_LOCATION }}
RELEASE_TYPE: ${{ inputs.release-type }}
- name: Publish to Modrinth
if: ${{ inputs.publish-mr }}
run: ./gradlew modrinth
env:
MODRINTH_API_KEY: ${{ secrets.MODRINTH_TOKEN }}
CHANGELOG_LOCATION: ${{ env.CHANGELOG_LOCATION }}
RELEASE_TYPE: ${{ inputs.release-type }}
- name: Publish to maven
if: ${{ inputs.publish-maven }}
run: ./gradlew publish
env:
MAVEN_USER: ${{ secrets.MAVEN_NAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}