action-gh-release
action-gh-release copied to clipboard
Set semver prerelease as pre-release
trafficstars
Would like an option to to autmatically mark the file as a pre-release if versioned as a semver pre-release. (eg 1.1.0-beta.2)
Don't know whether there exists another way to accomplish this?
Something like this might work for you:
name: Create a GitHub release
on:
push:
jobs:
release:
name: Create a GitHub release
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Check if tag is a SemVer (pre-)release
run: |
# SemVer regex adapted for bash from:
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
SEMVER_REGEX="^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$"
if [[ "$GITHUB_REF_NAME" =~ $SEMVER_REGEX ]]; then # If it matches, then it's a SemVer name
prereleasePart="${BASH_REMATCH[5]}" # The 5th group is the pre-release part
else
echo "Version $GITHUB_REF_NAME is not SemVer" 1>&2
exit 1
fi
if [[ -n "$prereleasePart" ]]; then # If not empty, then it's a pre-release
echo "is_prerelease=true" >> "$GITHUB_ENV"
else
echo "is_prerelease=false" >> "$GITHUB_ENV"
fi
- name: Create a GitHub Release
uses: softprops/action-gh-release@v2
with:
prerelease: ${{ env.is_prerelease }}
Disclaimer: I manually edited this from a more complex setup, renaming a few things here and there for the purposes of leaving a comment here that would hopefully be helpful. I can't guarantee that this would actually work as-is in a GitHub Actions workflow: there might be errors, both in the YAML syntax and at runtime.
A simpler version could be:
prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}