Skip to content

nusr is releasing app πŸš€ #36

nusr is releasing app πŸš€

nusr is releasing app πŸš€ #36

name: Create Release PR
run-name: ${{ github.actor }} is releasing app πŸš€
on:
workflow_dispatch:
pull_request:
types:
- closed
branches:
- "main"
jobs:
create-pre-release-pull-request:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: main
fetch-depth: 1
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "22.x"
- name: Install Yarn
run: npm install -g yarn
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Install dependencies and execute release
run: |
yarn
yarn release:patch
- name: Get modified files and validate
id: validate-files
run: |
echo "πŸ” Checking staged files..."
modified_files=$(git ls-files -m -o --exclude-standard)
if [ -z "$modified_files" ]; then
echo "❌ No modified files found"
exit 1
fi
# Extract package.json and CHANGELOG.md paths
package_json_path=$(echo "$modified_files" | grep "package.json" | head -n 1)
changelog_path=$(echo "$modified_files" | grep "CHANGELOG.md" | head -n 1)
if [ -z "$package_json_path" ]; then
echo "❌ package.json not found among modified files"
exit 1
fi
if [ -z "$changelog_path" ]; then
echo "❌ CHANGELOG.md not found among modified files"
exit 1
fi
# Extract changelog content
latest_changelog=$(git diff --no-color "$changelog_path" | grep -E '^(\+[^+]|-)' | grep -Ev '^--- a/' | sed 's/^+//;s/^-//')
if [ -z "$latest_changelog" ]; then
echo "❌ CHANGELOG.md has no changes"
exit 1
fi
# Save to temp file
changelog_temp_file=$(mktemp)
echo "$latest_changelog" > "$changelog_temp_file"
# Extract package info
package_name=$(jq -r '.name' "$package_json_path")
package_name="${package_name//@/}"
package_version=$(jq -r '.version' "$package_json_path")
echo "πŸ“¦ Package: $package_name"
echo "🏷️ Version: $package_version"
# Set outputs
echo "package_name=$package_name" >> $GITHUB_OUTPUT
echo "package_version=$package_version" >> $GITHUB_OUTPUT
echo "changelog_temp_file=$changelog_temp_file" >> $GITHUB_OUTPUT
echo "branchName=pre-release/$package_name/$package_version" >> $GITHUB_OUTPUT
- name: Check branch and create commit
run: |
branchName="${{ steps.validate-files.outputs.branchName }}"
echo "🌿 Branch: $branchName"
if git ls-remote --exit-code --heads origin "$branchName"; then
echo "❌ Branch already exists"
exit 1
fi
git checkout -B "$branchName"
git add -A
git commit -m "chore(RELEASE): ${{ steps.validate-files.outputs.package_version }}"
git push --set-upstream origin "$branchName"
echo "βœ… Branch and commit created"
- name: Create PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
changelog_content=$(cat "${{ steps.validate-files.outputs.changelog_temp_file }}")
default_branch=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
echo "πŸ”— Creating PR..."
pr_url=$(gh pr create \
--title "chore(RELEASE): ${{ steps.validate-files.outputs.package_version }}" \
--body "$changelog_content" \
--base "$default_branch" \
--head "${{ steps.validate-files.outputs.branchName }}")
echo "βœ… PR created: $pr_url"
# Cleanup
rm -f "${{ steps.validate-files.outputs.changelog_temp_file }}"
release:
runs-on: ubuntu-latest
if: github.event.pull_request.merged && startsWith(github.event.pull_request.head.ref, 'pre-release/excel-collab/')
permissions:
contents: write
pull-requests: read
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: "main"
fetch-depth: 1
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "22.x"
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Fetch PR metadata and create release
id: release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "πŸ” Fetching PR metadata..."
PR_NUMBER=${{ github.event.pull_request.number }}
PR_META=$(gh pr view "$PR_NUMBER" --json number,title,body,mergeCommit)
PR_TITLE=$(echo "$PR_META" | jq -r '.title')
PR_BODY=$(echo "$PR_META" | jq -r '.body')
TAG_NAME=$(echo "$PR_TITLE" | sed -e 's/^chore(RELEASE): *//' -e 's/ *$//')
COMMIT_ID=$(echo "$PR_META" | jq -r '.mergeCommit.oid')
if [ -z "$COMMIT_ID" ]; then
echo "❌ No merge commit found"
exit 1
fi
# Validate version format
if [[ ! "$TAG_NAME" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-hotfix-[0-9]+)?$ ]]; then
echo "❌ Invalid version format: $TAG_NAME"
exit 1
fi
echo "πŸš€ Creating release: $TAG_NAME"
# Create git tag and push it to trigger publish.yml via push: tags
git tag "$TAG_NAME" "$COMMIT_ID"
git push origin "$TAG_NAME"
# Create release using gh CLI
gh release create "$TAG_NAME" \
--repo "$GITHUB_REPOSITORY" \
--title "$TAG_NAME" \
--target "$COMMIT_ID" \
--notes "$PR_BODY"
echo "βœ… Release created successfully: $TAG_NAME"