Skip to content

Update PrintVersion on release branch creation #1

Update PrintVersion on release branch creation

Update PrintVersion on release branch creation #1

name: Update PrintVersion on release branch creation
on:
create
jobs:
update-version:
if: ${{ github.ref_type == 'branch' && startsWith(github.ref_name, 'release/') }}
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.ref }}
token: ${{ github.token }}
- name: Extract version from branch name
run: |
BRANCH_NAME="${GITHUB_REF_NAME}"
VERSION="${BRANCH_NAME#release/}"
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "RELEASE_BRANCH=$BRANCH_NAME" >> $GITHUB_ENV
echo "UPDATE_BRANCH=updateversion/$VERSION" >> $GITHUB_ENV
- name: Create updateversion branch
run: |
git checkout -b $UPDATE_BRANCH $RELEASE_BRANCH
- name: Update PrintVersion.swift
id: update_version
run: |
FILE=Sources/swift-format/PrintVersion.swift
if grep -q "print(\"$VERSION\")" "$FILE"; then
echo "Version already $VERSION; skipping update."
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
sed -i "s/print(\".*\")/print(\"$VERSION\")/" "$FILE"
echo "changed=true" >> "$GITHUB_OUTPUT"
- name: Commit and push changes
if: steps.update_version.outputs.changed == 'true'
run: |
git config user.name "swift-ci"
git config user.email "[email protected]"
git add Sources/swift-format/PrintVersion.swift
git commit -m "Update version to $VERSION"
git push origin $UPDATE_BRANCH
- name: Create Pull Request
if: steps.update_version.outputs.changed == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
gh pr create \
--base "${{ env.RELEASE_BRANCH }}" \
--head "${{ env.UPDATE_BRANCH }}" \
--title "[${{ env.VERSION }}] Update version to ${{ env.VERSION }}" \
--body "This PR was automatically opened by a GitHub action on creation of a release branch (\`${{ env.RELEASE_BRANCH }}\`).
Review the version update and merge if correct, otherwise update the version manually." \
--draft