Skip to content

Record Changeset

Record Changeset #27

name: Record Changeset
on:
workflow_dispatch:
inputs:
version_type:
description: "Change type"
required: true
type: choice
options:
- patch
- minor
- major
default: "patch"
jobs:
changeset:
name: Create Changeset
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Set BASE_BRANCH
run: |
if [[ "${GITHUB_REF#refs/heads/}" == "staging"* ]]; then
echo "BASE_BRANCH=staging" >> $GITHUB_ENV
else
echo "BASE_BRANCH=main" >> $GITHUB_ENV
fi
- name: Sync Base Branch
run: |
git fetch origin ${{ env.BASE_BRANCH }}
git merge origin/${{ env.BASE_BRANCH }} --no-edit
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Run Changeset Command
id: changeset
env:
BASE_BRANCH: ${{ env.BASE_BRANCH }}
run: |
echo "=== Starting Changeset Creation ==="
# Get and log commit message
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
echo "Commit Message: ${COMMIT_MESSAGE}"
echo "Base Branch: ${BASE_BRANCH}"
echo "=== Creating Empty Changeset ==="
# Create the changeset file first
CHANGESET_OUTPUT=$(npx changeset add --empty)
echo "Changeset Command Output: ${CHANGESET_OUTPUT}"
echo "=== Extracting Filename ==="
# Get the generated changeset filename
CHANGESET_FILE=$(echo "$CHANGESET_OUTPUT" | grep -o '[a-z0-9\-]\+\.md')
echo "Generated Changeset Filename: ${CHANGESET_FILE}"
echo "=== Checking .changeset Directory ==="
ls -la .changeset || echo "Directory not found!"
echo "=== Writing Changeset Content ==="
# Modify the changeset file to include the package
cat > ".changeset/${CHANGESET_FILE}" << EOF
---
"@avaprotocol/sdk-js": ${{ github.event.inputs.version_type }}
---
${COMMIT_MESSAGE}
EOF
echo "=== Verifying Changeset File Content ==="
if [ -f ".changeset/${CHANGESET_FILE}" ]; then
echo "Changeset file content:"
cat ".changeset/${CHANGESET_FILE}"
else
echo "Error: Changeset file was not created!"
fi
echo "=== Setting Output Variable ==="
echo "changeset_file=${CHANGESET_FILE}" >> $GITHUB_OUTPUT
echo "=== Final Directory Check ==="
# Verify changeset creation
if [ ! -d ".changeset" ]; then
echo "Error: .changeset directory not created"
exit 1
fi
echo "=== Staging and Committing Changeset ==="
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
echo "Staging changeset file..."
git add ".changeset/${CHANGESET_FILE}"
git status
echo "Committing changeset..."
git commit -m "Add changeset for: ${COMMIT_MESSAGE} [skip ci]"
echo "Verifying commit..."
git log -1 --stat
echo "=== Changeset Creation Complete ==="
# Add this line to set the commit message as an output
echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_OUTPUT
- name: Commit and Push Changes
id: commit_and_push
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# Create and checkout new branch using changeset filename
BRANCH_NAME="changeset-$(basename ${{ steps.changeset.outputs.changeset_file }} .md)"
git checkout -b "$BRANCH_NAME"
git push origin "$BRANCH_NAME"
# Set outputs for later use
echo "commit_message=${{ steps.changeset.outputs.commit_message }}" >> $GITHUB_ENV
echo "branch_name=${BRANCH_NAME}" >> $GITHUB_ENV
- name: Create Pull Request
env:
GH_TOKEN: ${{ github.token }}
COMMIT_MESSAGE: ${{ steps.changeset.outputs.commit_message }}
BRANCH_NAME: ${{ env.branch_name }}
BASE_BRANCH: ${{ env.BASE_BRANCH }}
run: |
gh pr create \
--title "Add changeset for ${COMMIT_MESSAGE}" \
--body "This pull request adds a changeset for ${COMMIT_MESSAGE}. Please review." \
--base "$BASE_BRANCH" \
--head "$BRANCH_NAME"
permissions:
contents: write
pull-requests: write