diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 00000000..65e13ce6 --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,8 @@ +# .github/release.yml + +changelog: + exclude: + labels: + - ignore-for-release + authors: + - octocat diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..b25980a9 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,76 @@ +name: "release" +# This workflow handles two different scenarios: +# 1. A new version tag is pushed to the repository, resulting in the publishing of a new release. +# 2. A pull is merged into main, resulting in a new draft release. + +on: + push: + branches: [main] + tags: + - "v*.*.*" + +jobs: + update_release: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 # Fetch all history for tags + + - name: Next tag + run: | + # Get the tag that triggered the workflow + tag=${GITHUB_REF#refs/tags/} + # Test if the tag is a version tag + if [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + next_tag=$tag + else + next_tag=$(scripts/next_tag.sh) + fi + + echo "Next tag: $next_tag" + echo "NEXT_TAG=$next_tag" >> $GITHUB_ENV + + - name: Optionally delete the existing draft release + run: | + # Get existing draft release (if any) + response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/${{ github.repository }}/releases") + + # Check if there is a draft release + draft_release=$(echo "$response" | jq '.[] | select(.draft == true)') + if [[ -n "$draft_release" ]]; then + # Delete the existing draft release + draft_release_id=$(echo "$draft_release" | jq -r '.id') + echo "Deleting draft release: ${draft_release_id}" + curl -s -X DELETE \ + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github+json" \ + "https://api.github.com/repos/${{ github.repository }}/releases/${draft_release_id}" + fi + + - name: Create the release + run: | + # If the tag ends in -rc.*, then its a draft release + if [[ "${{ env.NEXT_TAG }}" =~ -rc\.[0-9]+$ ]]; then + echo "Creating a draft release" + draft=true + else + echo "Creating a published release" + draft=false + fi + # Create a new release + curl -s -X POST \ + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github+json" \ + -d @- "https://api.github.com/repos/${{ github.repository }}/releases" </dev/null || echo "") +# Count the number of commits between the latest tag and HEAD +commits=$(git rev-list --count $latest_tag..HEAD --) +# Get latest tag for the current year, or default to v0.0.0 +latest_tag_for_year=$(git describe --tags --match "v${year}.*" --abbrev=0 $(git rev-list --tags --max-count=1) 2>/dev/null || echo "v0.0.0") +# Get the next feature version +next_feature_ver=$(($(echo $latest_tag_for_year | cut -d '.' -f 2) + 1)) +echo "v${year}.${next_feature_ver}.0-rc.${commits}"