Skip to content
Merged
12 changes: 12 additions & 0 deletions .github/workflows/check-branch-alias.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Check Branch Alias

on:
release:
types: [released]
workflow_dispatch:

permissions: {}

jobs:
check-branch-alias:
uses: wp-cli/.github/.github/workflows/reusable-check-branch-alias.yml@main
145 changes: 145 additions & 0 deletions .github/workflows/reusable-check-branch-alias.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
name: Check Branch Alias

on:
workflow_call:
release:
types: [released]
workflow_dispatch:

# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true

permissions:
contents: read

jobs:
check-branch-alias:
name: Check and update branch-alias
runs-on: ubuntu-latest
if: ${{ github.repository_owner == 'wp-cli' }}
permissions:
contents: write # Required for creating pull requests
pull-requests: write # Required for creating pull requests

steps:
- name: Check out source code
uses: actions/checkout@v5
with:
fetch-depth: 0 # Fetch all history for all tags
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 'latest'
tools: composer

- name: Check existence of composer.json file
id: check_composer_file
uses: andstor/file-existence-action@v3
with:
files: "composer.json"

- name: Check and update branch alias
if: steps.check_composer_file.outputs.files_exists == 'true'
id: check_alias
run: |
# Get the latest stable release tag (exclude pre-releases)
LATEST_TAG=$(git tag --list 'v[0-9]*.[0-9]*.[0-9]*' --sort=-version:refname | head -n 1)

if [ -z "$LATEST_TAG" ]; then
echo "No stable release tags found, skipping branch-alias check"
echo "needs_update=false" >> "$GITHUB_OUTPUT"
exit 0
fi

echo "Latest tag: $LATEST_TAG"

# Extract version numbers (remove 'v' prefix and any pre-release/build metadata)
VERSION="${LATEST_TAG#v}"
# Remove any pre-release or build metadata (e.g., -alpha, +build)
VERSION="${VERSION%%-*}"
VERSION="${VERSION%%+*}"

# Parse major, minor, and patch versions
IFS='.' read -r MAJOR MINOR _ <<< "$VERSION"

# Validate that MAJOR and MINOR are numeric
if ! [[ "$MAJOR" =~ ^[0-9]+$ ]] || ! [[ "$MINOR" =~ ^[0-9]+$ ]]; then
echo "Invalid version format: $VERSION"
echo "needs_update=false" >> "$GITHUB_OUTPUT"
exit 0
fi

# Calculate next minor version
NEXT_MINOR=$((MINOR + 1))
EXPECTED_ALIAS="${MAJOR}.${NEXT_MINOR}.x-dev"

echo "Expected branch-alias: $EXPECTED_ALIAS"

# Determine which branch key is being used (dev-main or dev-master)
BRANCH_KEY=""
CURRENT_ALIAS=""

# Try dev-main first
if composer config extra.branch-alias.dev-main > /dev/null 2>&1; then
BRANCH_KEY="dev-main"
CURRENT_ALIAS=$(composer config extra.branch-alias.dev-main)
# Try dev-master if dev-main doesn't exist
elif composer config extra.branch-alias.dev-master > /dev/null 2>&1; then
BRANCH_KEY="dev-master"
CURRENT_ALIAS=$(composer config extra.branch-alias.dev-master)
else
echo "No branch-alias found in composer.json, will use dev-main"
BRANCH_KEY="dev-main"
CURRENT_ALIAS=""
fi

echo "Current branch-alias ($BRANCH_KEY): $CURRENT_ALIAS"

if [ "$CURRENT_ALIAS" != "$EXPECTED_ALIAS" ]; then
echo "Branch alias needs update"
{
echo "needs_update=true"
echo "expected_alias=$EXPECTED_ALIAS"
echo "current_alias=$CURRENT_ALIAS"
echo "branch_key=$BRANCH_KEY"
echo "latest_tag=$LATEST_TAG"
} >> "$GITHUB_OUTPUT"
else
echo "Branch alias is up to date"
echo "needs_update=false" >> "$GITHUB_OUTPUT"
fi

- name: Update composer.json
if: steps.check_alias.outputs.needs_update == 'true'
run: |
BRANCH_KEY="${{ steps.check_alias.outputs.branch_key }}"
EXPECTED_ALIAS="${{ steps.check_alias.outputs.expected_alias }}"

# Update the branch-alias using composer command
composer config "extra.branch-alias.$BRANCH_KEY" "$EXPECTED_ALIAS"

- name: Create Pull Request
if: steps.check_alias.outputs.needs_update == 'true'
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "Update branch-alias to ${{ steps.check_alias.outputs.expected_alias }}"
title: "Update branch-alias to ${{ steps.check_alias.outputs.expected_alias }}"
body: |
This PR updates the Composer `branch-alias` configuration to reflect the latest release.

- Latest release: ${{ steps.check_alias.outputs.latest_tag }}
- Previous branch-alias: `${{ steps.check_alias.outputs.current_alias }}`
- Updated branch-alias: `${{ steps.check_alias.outputs.expected_alias }}`

The branch-alias should point to the next minor development version after the latest release.
branch: update-branch-alias
delete-branch: true
labels: |
automated-pr
1 change: 1 addition & 0 deletions .github/workflows/sync-workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
^.editorconfig
^.github/workflows/copilot-setup-steps.yml
^.github/workflows/regenerate-readme.yml
^.github/workflows/check-branch-alias.yml
^.github/workflows/manage-labels.yml
^AGENTS.md
TARGET_REPOS: |
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ This package cannot be used directly. It is a container to provide centralized f

See: [https://help.github.com/en/articles/creating-a-default-community-health-file-for-your-organization](https://help.github.com/en/articles/creating-a-default-community-health-file-for-your-organization)

### GitHub Actions Workflows

This repository contains reusable GitHub Actions workflows that are automatically synced to all WP-CLI repositories:

- **Code Quality Checks** (`code-quality.yml`) - Runs linting, PHPCS, PHPStan, and other code quality tools
- **Regenerate README** (`regenerate-readme.yml`) - Automatically regenerates README.md files from source
- **Check Branch Alias** (`check-branch-alias.yml`) - Monitors and updates Composer branch-alias configuration

#### Branch Alias Checker

The branch alias checker workflow automatically ensures that the Composer `branch-alias` in each repository's `composer.json` is up-to-date. It:

1. Runs weekly (every Monday at 2 AM UTC) or can be triggered manually
2. Checks the latest release tag
3. Calculates the expected branch-alias (next minor version after the latest release)
4. Compares with the current branch-alias
5. Creates a pull request if an update is needed

For example, if a repository has released version `2.12.0`, the branch-alias should be set to `2.13.x-dev` to point to the next development version.

## Installing

There's nothing to install, this package cannot be used directly.
Expand Down