|
| 1 | +name: Prepare WP-SSO Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: 'Release version without the v prefix (for example 1.2.0)' |
| 8 | + required: true |
| 9 | + notes: |
| 10 | + description: 'Optional short release summary for CHANGELOG.md' |
| 11 | + required: false |
| 12 | + default: '' |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + prepare: |
| 20 | + name: Prepare release pull request |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout repository |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Validate version input |
| 30 | + shell: bash |
| 31 | + run: | |
| 32 | + VERSION="${{ inputs.version }}" |
| 33 | + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 34 | + echo "Version must use semantic versioning, for example 1.2.0." |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + if git ls-remote --exit-code --tags origin "refs/tags/v$VERSION" >/dev/null 2>&1; then |
| 38 | + echo "Tag v$VERSION already exists." |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +
|
| 42 | + - name: Update version metadata |
| 43 | + shell: python |
| 44 | + run: | |
| 45 | + from pathlib import Path |
| 46 | + import re |
| 47 | +
|
| 48 | + version = "${{ inputs.version }}" |
| 49 | +
|
| 50 | + plugin = Path("wp-sso/wp-sso.php") |
| 51 | + text = plugin.read_text(encoding="utf-8") |
| 52 | + text = re.sub(r"(?m)^ \* Version: .*?$", f" * Version: {version}", text, count=1) |
| 53 | + text = re.sub(r"const VERSION = '[^']+';", f"const VERSION = '{version}';", text, count=1) |
| 54 | + plugin.write_text(text, encoding="utf-8") |
| 55 | +
|
| 56 | + readme = Path("wp-sso/readme.txt") |
| 57 | + text = readme.read_text(encoding="utf-8") |
| 58 | + text = re.sub(r"(?m)^Stable tag: .*?$", f"Stable tag: {version}", text, count=1) |
| 59 | + readme.write_text(text, encoding="utf-8") |
| 60 | +
|
| 61 | + - name: Update changelog |
| 62 | + shell: python |
| 63 | + run: | |
| 64 | + from pathlib import Path |
| 65 | + from datetime import date |
| 66 | +
|
| 67 | + version = "${{ inputs.version }}" |
| 68 | + notes = """${{ inputs.notes }}""".strip() |
| 69 | + changelog = Path("CHANGELOG.md") |
| 70 | + text = changelog.read_text(encoding="utf-8") |
| 71 | +
|
| 72 | + header = f"## [{version}] - {date.today().isoformat()}\n\n" |
| 73 | + if notes: |
| 74 | + section = header + "### Changed\n\n- " + notes.replace("\n", "\n- ") + "\n\n" |
| 75 | + else: |
| 76 | + section = header + "### Changed\n\n- Prepare WP-SSO release " + version + ".\n\n" |
| 77 | +
|
| 78 | + marker = "## [Unreleased]" |
| 79 | + index = text.find(marker) |
| 80 | + if index == -1: |
| 81 | + raise SystemExit("CHANGELOG.md does not contain an [Unreleased] section.") |
| 82 | +
|
| 83 | + next_section = text.find("\n## [", index + len(marker)) |
| 84 | + if next_section == -1: |
| 85 | + text = text.rstrip() + "\n\n" + section |
| 86 | + else: |
| 87 | + text = text[:next_section+1] + "\n" + section + text[next_section+1:] |
| 88 | +
|
| 89 | + compare_line = f"[{version}]: https://github.com/drnecrotix/WP-SSO/compare/v{version.rsplit('.', 1)[0]}.0...v{version}" |
| 90 | + if compare_line not in text: |
| 91 | + text = text.rstrip() + "\n" + compare_line + "\n" |
| 92 | +
|
| 93 | + changelog.write_text(text, encoding="utf-8") |
| 94 | +
|
| 95 | + - name: Validate updated files |
| 96 | + shell: bash |
| 97 | + run: | |
| 98 | + VERSION="${{ inputs.version }}" |
| 99 | + grep -q "Version: $VERSION" wp-sso/wp-sso.php |
| 100 | + grep -q "const VERSION = '$VERSION'" wp-sso/wp-sso.php |
| 101 | + grep -q "Stable tag: $VERSION" wp-sso/readme.txt |
| 102 | + php -l wp-sso/wp-sso.php |
| 103 | +
|
| 104 | + - name: Create release branch |
| 105 | + id: branch |
| 106 | + shell: bash |
| 107 | + run: | |
| 108 | + VERSION="${{ inputs.version }}" |
| 109 | + BRANCH="release/v$VERSION" |
| 110 | + echo "branch=$BRANCH" >> "$GITHUB_OUTPUT" |
| 111 | + git checkout -b "$BRANCH" |
| 112 | +
|
| 113 | + - name: Commit release preparation |
| 114 | + shell: bash |
| 115 | + run: | |
| 116 | + VERSION="${{ inputs.version }}" |
| 117 | + git config user.name "github-actions[bot]" |
| 118 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 119 | + git add wp-sso/wp-sso.php wp-sso/readme.txt CHANGELOG.md |
| 120 | + git commit -m "chore: prepare v$VERSION" |
| 121 | + git push --set-upstream origin "${{ steps.branch.outputs.branch }}" |
| 122 | +
|
| 123 | + - name: Open pull request |
| 124 | + env: |
| 125 | + GH_TOKEN: ${{ github.token }} |
| 126 | + shell: bash |
| 127 | + run: | |
| 128 | + VERSION="${{ inputs.version }}" |
| 129 | + gh pr create \ |
| 130 | + --base main \ |
| 131 | + --head "${{ steps.branch.outputs.branch }}" \ |
| 132 | + --title "chore: prepare v$VERSION" \ |
| 133 | + --body "Automated release preparation for WP-SSO v$VERSION.\n\nThis PR updates the plugin header version, internal VERSION constant, WordPress stable tag, and CHANGELOG.md. After required checks pass and this PR is merged, run the Release WP-SSO workflow with version $VERSION." |
0 commit comments