Skip to content

update yaml

update yaml #7

name: Create GitHub Release
on:
push:
branches:
- master # Or main, depending on your branch name
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Important to fetch all tags
- name: Get Version from mod.json
id: get_version
run: |
VERSION=$(jq -r .version mod.json)
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
- name: Calculate Release Version and Tag
id: calculate_release_version
run: |
VERSION="${{ steps.get_version.outputs.VERSION }}"
# Extract the parts of the version number
MAJOR=$(echo "$VERSION" | cut -d'.' -f1)
MINOR=$(echo "$VERSION" | cut -d'.' -f2)
PATCH=$(echo "$VERSION" | cut -d'.' -f3)
# Calculate the RELEASE version (one less than current version in mod.json)
if [[ "$PATCH" -gt 0 ]]; then
RELEASE_VERSION="$MAJOR.$MINOR.$((PATCH - 1))"
elif [[ "$MINOR" -gt 0 ]]; then
RELEASE_VERSION="$MAJOR.$((MINOR - 1)).99" # Assuming 99 is the last patch version
elif [[ "$MAJOR" -gt 0 ]]; then
RELEASE_VERSION="$((MAJOR - 1)).99.99" # Assuming 99 is the last minor version
else
RELEASE_VERSION="0.0.0" # Or whatever makes sense for your project
fi
RELEASE_TAG="v$RELEASE_VERSION"
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_OUTPUT
echo "RELEASE_TAG=$RELEASE_TAG" >> $GITHUB_OUTPUT
- name: Check if Tag Exists
id: check_tag_exists
run: |
RELEASE_TAG="${{ steps.calculate_release_version.outputs.RELEASE_TAG }}"
git fetch --tags
if git rev-parse --verify "refs/tags/$RELEASE_TAG" >/dev/null 2>&1; then
echo "TAG_EXISTS=true" >> $GITHUB_OUTPUT
echo "Tag $RELEASE_TAG already exists. Skipping release creation."
else
echo "TAG_EXISTS=false" >> $GITHUB_OUTPUT
echo "Tag $RELEASE_TAG does not exist. Proceeding with release creation."
fi
- name: Generate Release Notes
id: generate_release_notes
if: steps.check_tag_exists.outputs.TAG_EXISTS == 'false'
run: |
VERSION="${{ steps.get_version.outputs.VERSION }}"
RELEASE_TAG="${{ steps.calculate_release_version.outputs.RELEASE_TAG }}"
# Using git log to get commits since the calculated tag
if [[ -z $(git tag -l "$RELEASE_TAG") ]]; then
RELEASE_NOTES="Initial release"
else
RELEASE_NOTES=$(git log --pretty=format:"- %s" "$RELEASE_TAG"..HEAD)
fi
echo "RELEASE_NOTES<<EOF" >> $GITHUB_OUTPUT
echo "$RELEASE_NOTES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Release
uses: actions/create-release@v1
if: steps.check_tag_exists.outputs.TAG_EXISTS == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.calculate_release_version.outputs.RELEASE_TAG }}
release_name: Release ${{ steps.calculate_release_version.outputs.RELEASE_TAG }}
body: ${{ steps.generate_release_notes.outputs.RELEASE_NOTES }}
draft: false
prerelease: false
- name: Create Tag (if it doesn't exist)
if: steps.check_tag_exists.outputs.TAG_EXISTS == 'false'
run: |
VERSION="${{ steps.get_version.outputs.VERSION }}"
RELEASE_TAG="${{ steps.calculate_release_version.outputs.RELEASE_TAG }}"
git tag $RELEASE_TAG
git push origin $RELEASE_TAG
echo "Created tag $RELEASE_TAG"