Release Workflow #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 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@v6 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
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 | ||
}); | ||
// Export release_id to the environment for the next steps | ||
console.log(`RELEASE_ID=${release.data.id}`); | ||
env: | ||
RELEASE_ID: ${{ steps.create_draft.outputs.release_id }} | ||
# Upload JAR files to the draft release | ||
- name: Upload JAR files to draft release | ||
uses: actions/github-script@v6 | ||
env: | ||
RELEASE_ID: ${{ env.RELEASE_ID }} | ||
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: ${{ env.RELEASE_ID }} | ||
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 | ||
}); |