Tutorial: Automatically building and releasing your Java Plugin with GitHub actions!
Do you find yourself continually compiling your latest custom plugin for your friends continually and wish there was a simple way of automating this solution?
Using GitHub actions and a few fun changes you can quickly develop and tag your versions which are automatically built and released on GitHub for all your friends to get. The best part it gives you an automatically generated changelog through Convential Commits.
Prerequisites
- Your project uses Convential Commits
- You have access to a decent command line (this might work on Windows but I have only tested it on macOS)
- In your
plugin.ymlyou haveversion: ${project.version}
Tutorial
Creating the GitHub Action
Create the following file: .github/workflows/release.yml with the following content:
name: Release
on:
push:
tags:
- 'v*'
jobs:
release:
name: Publish a release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: 17
distribution: 'adopt'
- name: Build with Maven
run: mvn --batch-mode --update-snapshots clean package
- name: Get Version from POM
run: echo "::set-output name=version::$(mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec)"
id: version
- name: Create Release
uses: actions/create-release@v1
id: create_release
with:
draft: false
prerelease: false
release_name: ${{ github.ref }}
tag_name: ${{ github.ref }}
body_path: CHANGELOG.md
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Upload Java File
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: "target/<project-name>-${{ steps.version.outputs.version }}.jar"
asset_name: <project-name>-${{ steps.version.outputs.version }}.jar
asset_content_type: application/java-archive
It is recommended you read these actions as they can be potentially destructive, I have cleaned this up as much as I can. Remember to replace <project-name> with your project name.
Releasing your first version
We will use a modified version of standard-version since we would like it to automatically update our pom.xml file.
npx dwmkerr/standard-version --packageFiles pom.xml --bumpFiles pom.xml --first-release
This will release your very first version, you can then run git push --follow-tags to push the new release to GitHub which will trigger the build process
Normal releasing
It assumes you are using Semantic Versioning - which is well worth a read however the tl;dr version is:
You have three types of releases: major.minor.patch each changing the version number by one. You would run npx dwmkerr/standard-version --packageFiles pom.xml --bumpFiles pom.xml --release-as <major|minor|patch> when you are ready to change that specific version.
Future
This could easily be adapted for multiple projects, and you could probably setup some kind of file copying to automatically upload the latest Jar file to your server.
Let me know if you have any questions and I'll do my best to answer them.
Posted Jan 14, 2022, 8:40 PM UTC · edited