-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from vorsterk/release
fix: release.yaml
- Loading branch information
Showing
1 changed file
with
210 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,220 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Please do not attempt to edit this flow without the direct consent from the Tazama team. This file is managed centrally. | ||
|
||
# This workflow should only be run when while on the main branch | ||
# Create a tag locally using "git tag vx.x.x" supposed to match latest version in changelog.md | ||
# Then push the release tag created above by running "git push origin vx.x.x" | ||
# The tag will be set on the release page on github with changes made | ||
|
||
name: Create a New Release | ||
name: Release Workflow | ||
|
||
on: | ||
push: | ||
tags: | ||
- "v*" | ||
|
||
permissions: | ||
contents: write | ||
|
||
repository_dispatch: | ||
types: [release] | ||
properties: | ||
milestone_number: | ||
type: string | ||
jobs: | ||
release: | ||
name: Release pushed tag | ||
runs-on: ubuntu-22.04 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Create release | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
with: | ||
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.PAT }} | ||
|
||
- uses: actions-ecosystem/action-release-label@v1 | ||
id: release-label | ||
if: ${{ steps.get-merged-pull-request.outputs.title != null }} | ||
with: | ||
github_token: ${{ secrets.PAT }} | ||
#labels: ${{ steps.get-merged-pull-request.outputs.labels }} | ||
|
||
# Get the latest tag in the repository | ||
- uses: actions-ecosystem/action-get-latest-tag@v1 | ||
id: get-latest-tag | ||
if: ${{ steps.release-label.outputs.level != null }} | ||
with: | ||
semver_only: true | ||
|
||
- name: Setup Node.js (.npmrc) | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16.x | ||
registry-url: https://npm.pkg.github.com/ | ||
# Defaults to the user or organization that owns the workflow file | ||
scope: '@frmscoe' | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
env: | ||
GH_TOKEN: '${{ secrets.PAT }}' | ||
|
||
- name: Run Tests | ||
run: npm test | ||
env: | ||
STARTUP_TYPE: 'nats' | ||
|
||
# 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 (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 }} | ||
tag: ${{ github.ref_name }} | ||
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 | ||
|
||
# Update the CHANGELOG.md file in the repository | ||
- name: Update CHANGELOG.md | ||
run: | | ||
gh release create "$tag" \ | ||
--repo="$GITHUB_REPOSITORY" \ | ||
--title="${tag#v}" \ | ||
--generate-notes | ||
# Usage (while on main branch) | ||
# git tag v1.0.0 | ||
# git push origin v1.0.0 | ||
NEW_VERSION=${{ steps.bump_version.outputs.new_version }} | ||
CHANGELOG_CONTENTS=$(cat /home/runner/work/changelog.txt) | ||
echo -e "## $NEW_VERSION\n\n$CHANGELOG_CONTENTS\n\n$(cat CHANGELOG.md)" > CHANGELOG.md | ||
git config --global user.name "github-actions[bot]" | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
git add CHANGELOG.md | ||
git commit -m "Update CHANGELOG.md for $NEW_VERSION" | ||
git push origin HEAD:main | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |