Update version on new release branch #53
This file contains hidden or 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
| name: Update version on new release branch | |
| on: | |
| create: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| update_version: | |
| if: github.ref_type == 'branch' && startsWith(github.ref, 'refs/heads/release-v') | |
| runs-on: ubuntu-24.04 | |
| steps: | |
| - name: Harden Runner | |
| uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0 | |
| with: | |
| egress-policy: audit | |
| - name: Checkout repository | |
| uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 | |
| - name: Enable Corepack | |
| run: corepack enable | |
| - name: Setup Node.js | |
| uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0 | |
| with: | |
| node-version-file: ".nvmrc" | |
| cache: "yarn" | |
| - name: Extract current version | |
| run: | | |
| CURRENT_VERSION=$(node -p "require('./contracts/package.json').version") | |
| echo "CURRENT_VERSION=$CURRENT_VERSION" >> "$GITHUB_ENV" | |
| - name: Extract new version number | |
| run: echo "NEW_VERSION=${GITHUB_REF#refs/heads/release-v}" >> "$GITHUB_ENV" | |
| - name: Validate new version | |
| run: | | |
| BRANCH="${GITHUB_REF#refs/heads/}" | |
| echo "Branch: $BRANCH" | |
| echo "Current version: $CURRENT_VERSION" | |
| echo "New version: $NEW_VERSION" | |
| # 1) Branch must match release-v<semver> | |
| if ! echo "$BRANCH" | grep -Eq '^release-v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$'; then | |
| echo "Error: Branch '$BRANCH' must match 'release-v<semver>' (e.g., release-v1.2.3)." >&2 | |
| exit 1 | |
| fi | |
| # 2) NEW_VERSION must be valid semver | |
| node -e "const v=process.env.NEW_VERSION; const semver=/^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?$/; if(!semver.test(v)){ console.error('Error: NEW_VERSION is not valid semver:', v); process.exit(1); }" | |
| if [ $? -ne 0 ]; then | |
| exit 1 | |
| fi | |
| # 3) NEW_VERSION must differ from CURRENT_VERSION | |
| if [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then | |
| echo "Error: NEW_VERSION equals CURRENT_VERSION ($CURRENT_VERSION). Nothing to release." >&2 | |
| exit 1 | |
| fi | |
| - name: Replace version in files | |
| env: | |
| NEW_VERSION: ${{ env.NEW_VERSION }} | |
| run: | | |
| echo "Current version: $CURRENT_VERSION" | |
| echo "New version: $NEW_VERSION" | |
| # Update package.json version field | |
| node -e ' | |
| const fs = require("fs"); | |
| const path = "./contracts/package.json"; | |
| const pkg = JSON.parse(fs.readFileSync(path, "utf8")); | |
| pkg.version = process.env.NEW_VERSION; | |
| fs.writeFileSync(path, JSON.stringify(pkg, null, 2) + "\n"); | |
| ' | |
| # Escape special characters for sed | |
| ESCAPED_CURRENT=$(printf '%s' "$CURRENT_VERSION" | sed -e 's/[\/&]/\\&/g') | |
| ESCAPED_NEW=$(printf '%s' "$NEW_VERSION" | sed -e 's/[\/&]/\\&/g') | |
| # Pattern to match version + optional prerelease (-alpha.1) and build (+build) suffixes | |
| VERSION_SUFFIX='(-[A-Za-z0-9.]+)?(\+[A-Za-z0-9.]+)?' | |
| # Replace version in contracts/src/ | |
| find ./contracts/src/ -type d -name '.*' -prune -o \ | |
| -type f -exec sed -Ei "s#${ESCAPED_CURRENT}${VERSION_SUFFIX}#$ESCAPED_NEW#g" {} + | |
| - name: Auto-commit changes | |
| uses: stefanzweifel/git-auto-commit-action@28e16e81777b558cc906c8750092100bbb34c5e3 #v7.0.0 | |
| with: | |
| commit_message: Bump version to ${{ env.NEW_VERSION }} |