Skip to content

Release Workflow

Release Workflow #10

Workflow file for this run

name: Release Workflow
on:
workflow_dispatch:
inputs:
tag:
description: 'Tag for the release (format: v{x}.{y}.{z})'
required: true
default: ''
release_name:
description: 'Release name/title'
required: true
default: ''
jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
# Checkout repository
- name: Checkout repository
uses: actions/checkout@v3
# Set up Java environment
- name: Set up Java 21
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'
# Extract version from the provided tag
- name: Extract version from input tag
id: extract_version
run: |
TAG="${{ github.event.inputs.tag }}"
if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: Tag format must be v{x}.{y}.{z}"
exit 1
fi
VERSION="${TAG#v}"
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
echo "RELEASE_VERSION=${MAJOR}.${MINOR}.${PATCH}-1.21.3" >> $GITHUB_ENV
echo "NEXT_SNAPSHOT_VERSION=${MAJOR}.${MINOR}.$((PATCH + 1))-1.21.3-SNAPSHOT" >> $GITHUB_ENV
# Update paper-plugin.yml to release version
- name: Update version in paper-plugin.yml for release
run: |
FILE_PATH="${{ github.workspace }}/src/main/resources/paper-plugin.yml"
VERSION="${{ env.RELEASE_VERSION }}"
if [[ -f "$FILE_PATH" ]]; then
yq eval ".version = \"${VERSION}\"" -i "$FILE_PATH"
echo "Updated version in $FILE_PATH to $VERSION"
else
echo "Error: $FILE_PATH not found!"
exit 1
fi
env:
RELEASE_VERSION: ${{ env.RELEASE_VERSION }}
shell: bash
# Update pom.xml to release version
- name: Update version in pom.xml for release
run: |
mvn versions:set -DnewVersion="${{ env.RELEASE_VERSION }}"
mvn versions:commit
# Commit the updated pom.xml for release version
- name: Commit updated pom.xml
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git commit -am "Set version to ${{ env.RELEASE_VERSION }}"
# Build the project
- name: Build with Maven
run: |
mvn install
mvn package
# Create a new tag for the release version
- name: Create tag for release
run: |
git tag "v${{ env.RELEASE_VERSION }}"
# Update paper-plugin.yml to next SNAPSHOT version
- name: Update version in paper-plugin.yml for snapshot
run: |
FILE_PATH="${{ github.workspace }}/src/main/resources/paper-plugin.yml"
VERSION="${{ env.NEXT_SNAPSHOT_VERSION }}"
if [[ -f "$FILE_PATH" ]]; then
yq eval ".version = \"${VERSION}\"" -i "$FILE_PATH"
echo "Updated version in $FILE_PATH to $VERSION"
else
echo "Error: $FILE_PATH not found!"
exit 1
fi
env:
NEXT_SNAPSHOT_VERSION: ${{ env.NEXT_SNAPSHOT_VERSION }}
shell: bash
# Update pom.xml to next SNAPSHOT version
- name: Update version in pom.xml to next SNAPSHOT
run: |
mvn versions:set -DnewVersion="${{ env.NEXT_SNAPSHOT_VERSION }}"
mvn versions:commit
# Commit updated pom.xml for next SNAPSHOT version
- name: Commit next SNAPSHOT version
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git commit -am "Set version to ${{ env.NEXT_SNAPSHOT_VERSION }}"
# Create draft release
- name: Create draft release
id: create_draft
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
const tagName = context.payload.inputs.tag;
const releaseDescription = context.payload.inputs.release_name;
const release = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
name: tagName, // Title as {tag}
body: releaseDescription, // Description as {release_name}
draft: true
});
return release.data.id;
# Upload JAR files to the draft release
- name: Upload JAR files to draft release
uses: actions/github-script@v6
env:
RELEASE_ID: ${{ steps.create_draft.outputs.result }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const releaseId = process.env.RELEASE_ID;
// Directory containing JAR files
const targetDir = path.join(process.env.GITHUB_WORKSPACE, 'target');
// Upload each JAR file in the target directory
fs.readdirSync(targetDir).forEach(async file => {
if (file.endsWith('.jar')) {
const filePath = path.join(targetDir, file);
const stat = fs.statSync(filePath);
await github.rest.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: releaseId,
name: file,
data: fs.readFileSync(filePath),
headers: {
'content-type': 'application/java-archive',
'content-length': stat.size
}
});
}
});
# Push changes back to the repository
- name: Push changes
uses: ad-m/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
tags: true
# Publish the draft release
- name: Publish draft release
uses: actions/github-script@v6
env:
RELEASE_ID: ${{ steps.create_draft.outputs.result }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const releaseId = process.env.RELEASE_ID;
await github.rest.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: releaseId,
draft: false
});