-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cd: create GH Actions workflow to prepare release on dispatch
- Loading branch information
1 parent
4a18372
commit b872756
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/python3 | ||
import re | ||
import subprocess | ||
import sys | ||
|
||
|
||
def get_arg(arg_idx) -> str: | ||
return sys.argv[arg_idx] | ||
|
||
|
||
def get_current_version() -> str: | ||
current_version_cmd = subprocess.run( | ||
"mvn help:evaluate -Dexpression=project.version -q -DforceStdout", | ||
shell=True, capture_output=True, text=True) | ||
return current_version_cmd.stdout | ||
|
||
|
||
def get_release_version(release_type: str, current_version: str) -> str: | ||
major, minor, patch = determine_new_version(current_version, release_type) | ||
return str(major) + "." + str(minor) + "." + str(patch) | ||
|
||
|
||
def get_snapshot_version(release_type: str, current_version: str) -> str: | ||
major, minor, patch = determine_new_version(current_version, release_type) | ||
patch += 1 | ||
return str(major) + "." + str(minor) + "." + str(patch) + "-SNAPSHOT" | ||
|
||
|
||
def get_version_tag(release_type: str, current_version: str) -> str: | ||
major, minor, patch = determine_new_version(current_version, release_type) | ||
return "v" + str(major) + "." + str(minor) + "." + str(patch) | ||
|
||
|
||
def determine_new_version(current_version, release_type): | ||
major, minor, patch = dissect_version(current_version) | ||
|
||
match release_type: | ||
case "MAJOR": | ||
major, minor, patch = get_major_release_version(major) | ||
case "MINOR": | ||
major, minor, patch = get_minor_release_version(major, minor) | ||
case "PATCH": | ||
major, minor, patch = get_patch_release_version(major, minor, patch) | ||
case _: | ||
print("Second arg has to be `MAJOR`, `MINOR` or `PATCH`") | ||
sys.exit() | ||
|
||
return major, minor, patch | ||
|
||
|
||
def dissect_version(current_version) -> (int, int, int): | ||
version_regex = re.compile( | ||
r'^(?P<major>[0-9]+)\.(?P<minor>[0-9+])\.(?P<patch>[0-9+])(-SNAPSHOT)*') | ||
regex_match = version_regex.search(current_version) | ||
major: int = int(regex_match.groupdict().get("major")) | ||
minor: int = int(regex_match.groupdict().get("minor")) | ||
patch: int = int(regex_match.groupdict().get("patch")) | ||
return major, minor, patch | ||
|
||
|
||
def get_major_release_version(major): | ||
major += 1 | ||
minor = 0 | ||
patch = 0 | ||
return major, minor, patch | ||
|
||
|
||
def get_minor_release_version(major, minor): | ||
minor += 1 | ||
patch = 0 | ||
return major, minor, patch | ||
|
||
|
||
def get_patch_release_version(major, minor, patch): | ||
# Leave values as is because current version without `-SNAPSHOT` is new patch version | ||
return major, minor, patch | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
version_type = get_arg(1) | ||
release_type = get_arg(2) | ||
|
||
current_version = get_current_version() | ||
|
||
match version_type: | ||
case "release-version": | ||
new_version = get_release_version(release_type, current_version) | ||
case "version-tag": | ||
new_version = get_version_tag(release_type, current_version) | ||
case "snapshot-version": | ||
new_version = get_snapshot_version(release_type, current_version) | ||
case _: | ||
print( | ||
"First arg has to be `release-version`, `version-tag` or `snapshot-version`.") | ||
sys.exit() | ||
|
||
print(new_version) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: prepare-release | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
release: | ||
description: Type of release | ||
required: true | ||
type: choice | ||
options: | ||
- PATCH | ||
- MINOR | ||
- MAJOR | ||
default: PATCH | ||
|
||
jobs: | ||
prepare: | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-java@v3 | ||
with: | ||
java-version: 11 | ||
distribution: corretto | ||
cache: maven | ||
|
||
- name: Determine new versions | ||
run: | | ||
echo "release_version=$(./.github/workflows/maven-version-determiner.py release-version $release_type)" >> "$GITHUB_ENV" | ||
echo "snapshot_version=$(./.github/workflows/maven-version-determiner.py snapshot-version $release_type)" >> "$GITHUB_ENV" | ||
echo "version_tag=$(./.github/workflows/maven-version-determiner.py version-tag $release_type)" >> "$GITHUB_ENV" | ||
env: | ||
release_type: ${{ inputs.release }} | ||
|
||
- name: Configure Git user | ||
run: | | ||
git config user.email "actions@users.noreply.github.com" | ||
git config user.name "GitHub Actions" | ||
- name: Prepare with Maven release plugin | ||
run: > | ||
mvn | ||
--batch-mode | ||
-Dresume=false | ||
-Drelease-version=$release_version | ||
-Dtag=$version_tag | ||
-DdevelopmentVersion=$snapshot_version | ||
release:prepare |