Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set semver prerelease as pre-release #390

Open
Lombra opened this issue Sep 23, 2023 · 1 comment
Open

Set semver prerelease as pre-release #390

Lombra opened this issue Sep 23, 2023 · 1 comment

Comments

@Lombra
Copy link

Lombra commented Sep 23, 2023

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?

@erikhuizinga
Copy link

erikhuizinga commented Oct 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants