Skip to content

release

release #1

Workflow file for this run

# SPDX-License-Identifier: Apache-2.0
name: Release Workflow
on:
repository_dispatch:
types: [release]
properties:
milestone_number:
type: string
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
with:
ref: main
fetch-depth: 0 # Fetch all tags
# Fetch merged pull request and determine release labels
- uses: actions-ecosystem/action-get-merged-pull-request@v1
id: get-merged-pull-request
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions-ecosystem/action-release-label@v1
id: release-label
if: ${{ steps.get-merged-pull-request.outputs.title != null }}
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
#labels: ${{ steps.get-merged-pull-request.outputs.labels }}
# Get the latest tag in the repositorys
- uses: actions-ecosystem/action-get-latest-tag@v1
id: get-latest-tag
if: ${{ steps.release-label.outputs.level != null }}
with:
semver_only: true
# Determine the release type (major, minor, patch) based on commit messages
- name: Determine Release Type
id: determine_release
run: |
export PREV_VERSION=$(git describe --abbrev=0 --tags)
echo "Previous Version: $PREV_VERSION"
export COMMIT_MESSAGES=$(git log $PREV_VERSION^..HEAD --format=%B)
echo "Commit Messages: $COMMIT_MESSAGES"
# Determine release type based on commit messages and labels
RELEASE_TYPE="patch" # Default to patch
if echo "$COMMIT_MESSAGES" | grep -q -e "BREAKING CHANGE:"; then
RELEASE_TYPE="major"
elif echo "$COMMIT_MESSAGES" | grep -q -e "feat:"; then
RELEASE_TYPE="minor"
elif echo "$COMMIT_MESSAGES" | grep -q -e "feat:" && (echo "$COMMIT_MESSAGES" | grep -q -e "fix:" || echo "$COMMIT_MESSAGES" | grep -q -e "enhancement:" || echo "$COMMIT_MESSAGES" | grep -q -e "docs:" || echo "$COMMIT_MESSAGES" | grep -q -e "refactor:" || echo "$COMMIT_MESSAGES" | grep -q -e "chore:"); then
RELEASE_TYPE="minor"
elif echo "$COMMIT_MESSAGES" | grep -q -e "fix:" -e "enhancement:" -e "docs:" -e "refactor:" -e "chore:"; then
RELEASE_TYPE="patch"
fi
echo "Release Type: $RELEASE_TYPE"
echo "::set-output name=release_type::$RELEASE_TYPE"
# Bump the version based on the determined release type
- name: Bump Version
id: bump_version
run: |
export PREV_VERSION=$(git describe --abbrev=0 --tags)
echo "Previous Version: $PREV_VERSION"
export RELEASE_TYPE=${{ steps.determine_release.outputs.release_type }}
echo "Release Type: $RELEASE_TYPE"
export VERSION_PARTS=($(echo $PREV_VERSION | tr '.' '\n'))
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
if [[ $RELEASE_TYPE == "major" ]]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif [[ $RELEASE_TYPE == "minor" ]]; then
MINOR=$((MINOR + 1))
PATCH=0
else
PATCH=$((PATCH + 1))
fi
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "New Version: $NEW_VERSION"
echo "::set-output name=new_version::$NEW_VERSION"
# Get the milestone details
- name: Get Milestone Details
id: get_milestone
run: |
# Retrieve the milestone ID from the workflow input
MILESTONE_ID=${{ github.event.client_payload.milestone_number }}
MILESTONE_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/milestones/${MILESTONE_ID}")
MILESTONE_TITLE=$(echo "$MILESTONE_RESPONSE" | jq -r '.title')
MILESTONE_DESCRIPTION=$(echo "$MILESTONE_RESPONSE" | jq -r '.description')
MILESTONE_DATE=$(echo "$MILESTONE_RESPONSE" | jq -r '.due_on')
echo "::set-output name=milestone_title::$MILESTONE_TITLE"
echo "::set-output name=milestone_description::$MILESTONE_DESCRIPTION"
echo "::set-output name=milestone_date::$MILESTONE_DATE"
# Generate the changelog based on commit messages and labels
- name: Generate Changelog
id: generate_changelog
run: |
# Generate Changelog Script
# Constants
CHANGELOG_FILE="/home/runner/work/changelog.txt"
LABEL_BUG="fix:"
LABEL_FEATURE="feat:"
LABEL_ENHANCEMENT="enhancement:"
LABEL_DOCS="docs:"
LABEL_REFACTOR="refactor:"
LABEL_CHORE="chore:"
LABEL_BREAKING_CHANGE="BREAKING CHANGE:"
# Get the last release tag
LAST_RELEASE_TAG=$(git describe --abbrev=0 --tags)
# Get the milestone details from the output of the previous step
MILESTONE_TITLE="${{ steps.get_milestone.outputs.milestone_title }}"
MILESTONE_DESCRIPTION="${{ steps.get_milestone.outputs.milestone_description }}"
MILESTONE_DATE="${{ steps.get_milestone.outputs.milestone_date }}"
# Append the milestone details to the changelog file
echo "## Milestone: $MILESTONE_TITLE" >> "$CHANGELOG_FILE"
echo "Date: $MILESTONE_DATE" >> "$CHANGELOG_FILE"
echo "Description: $MILESTONE_DESCRIPTION" >> "$CHANGELOG_FILE"
echo "" >> "$CHANGELOG_FILE"
# Function to append section to the changelog file
append_section() {
local section_title="$1"
local section_label="$2"
local section_icon="$3"
# Get the commit messages with the specified label between the last release and the current release
local commit_messages=$(git log --pretty=format:"- %s (Milestone: %b, Linked Issues: %C(yellow)%H%Creset)" "$LAST_RELEASE_TAG..HEAD" --grep="$section_label" --no-merges --decorate --decorate-refs=refs/issues)
# If there are commit messages, append the section to the changelog file
if [ -n "$commit_messages" ]; then
echo "### $section_icon $section_title" >> "$CHANGELOG_FILE"
echo "" >> "$CHANGELOG_FILE"
echo "$commit_messages" >> "$CHANGELOG_FILE"
echo "" >> "$CHANGELOG_FILE"
fi
}
# Append sections to the changelog file based on labels
append_section "Bug Fixes" "$LABEL_BUG" "🐞"
append_section "New Features" "$LABEL_FEATURE" "⭐️"
append_section "Enhancements" "$LABEL_ENHANCEMENT" "✨"
append_section "Documentation" "$LABEL_DOCS" "📚"
append_section "Refactorings" "$LABEL_REFACTOR" "🔨"
append_section "Chores" "$LABEL_CHORE" "⚙️"
append_section "Breaking Changes" "$LABEL_BREAKING_CHANGE" "💥"
echo "::set-output name=changelog_file::$CHANGELOG_FILE"
# Read changelog contents into a variable
- name: Read Changelog Contents
id: read_changelog
run: |
echo "::set-output name=changelog_contents::$(cat /home/runner/work/changelog.txt)"
# Display changelog
- name: Display Changelog
run: cat /home/runner/work/changelog.txt
# Attach changelog as an artifact
- name: Attach Changelog to Release
uses: actions/upload-artifact@v2
with:
name: Changelog
path: /home/runner/work/changelog.txt
# Create release while including the changelog
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.bump_version.outputs.new_version }}
release_name: ${{ steps.bump_version.outputs.new_version }}
body_path: /home/runner/work/changelog.txt
draft: false
prerelease: false