Skip to content

Commit ae083b6

Browse files
Create a GH action to prepare a new release (#744)
* Create namespace updater script * Set up workflow logic * Update constant values * Add new changelog heading * Update workflow * Adjust commands * Job name * Install ruby & sass * Specify Ruby version * Try installing Sass with npm * Setup WP CLI * Try disabling xdebug * Set upstream to * Just set upstream * Try passing in as variable * Env£ * Validate branch doesn't exist & add GH token * Tweak branch validation logic * Remove branch validation for now * Include body * New validation logic * Adjust logic again! * Remove unnecessary permission * Make the PR a draft * Remove unnecessary colon Co-authored-by: Drew Jaynes <[email protected]> --------- Co-authored-by: Drew Jaynes <[email protected]>
1 parent eb38562 commit ae083b6

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
set -Eeuo pipefail
3+
4+
SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
5+
if ! command -v jq >/dev/null; then
6+
echo "Please install jq"
7+
exit 1
8+
fi
9+
10+
COMPOSER_FILE="${SCRIPT_DIR}/../../composer.json"
11+
12+
OLD_VERSION=$(jq -e -r .version "${COMPOSER_FILE}")
13+
echo "Old version: ${OLD_VERSION}"
14+
echo "New version: ${NEW_VERSION}"
15+
16+
# replace `.` with `_` to build our namespace string
17+
OLD_VERSION_WITH_UNDERSCORES=${OLD_VERSION//./_}
18+
OLD_NAMESPACE_STRING="v${OLD_VERSION_WITH_UNDERSCORES}"
19+
20+
NEW_VERSION_WITH_UNDERSCORES=${NEW_VERSION//./_}
21+
NEW_NAMESPACE_STRING="v${NEW_VERSION_WITH_UNDERSCORES}"
22+
23+
# replace version in `composer.json`
24+
echo "Updating composer.json version"
25+
UPDATED_JSON="$(jq --arg NEW_VERSION "${NEW_VERSION}" '.version = $NEW_VERSION' "${COMPOSER_FILE}")"
26+
27+
# replace autoload namespaces in `composer.json`
28+
echo "Updating autoload namespaces in composer.json"
29+
AUTOLOAD_PSR4="$(echo "${UPDATED_JSON}" | jq -e '.autoload["psr-4"]')"
30+
AUTOLOAD_PSR4="$(echo "${AUTOLOAD_PSR4}" | sed "s/${OLD_NAMESPACE_STRING}/${NEW_NAMESPACE_STRING}/g")"
31+
32+
AUTOLOAD_DEV_PSR4="$(echo "${UPDATED_JSON}" | jq -e '.["autoload-dev"]["psr-4"]')"
33+
AUTOLOAD_DEV_PSR4="$(echo "${AUTOLOAD_DEV_PSR4}" | sed "s/${OLD_NAMESPACE_STRING}/${NEW_NAMESPACE_STRING}/g")"
34+
35+
UPDATED_JSON="$(echo "${UPDATED_JSON}" | jq --argjson AUTOLOAD_PSR4 "${AUTOLOAD_PSR4}" --argjson AUTOLOAD_DEV_PSR4 "${AUTOLOAD_DEV_PSR4}" '.autoload["psr-4"] = $AUTOLOAD_PSR4 | .["autoload-dev"]["psr-4"] = $AUTOLOAD_DEV_PSR4')"
36+
echo "${UPDATED_JSON}" > "${COMPOSER_FILE}"
37+
38+
# replace namespace in `./woocommerce/` directory; we're looking in PHP and JS files only
39+
echo "Replacing instances of ${OLD_NAMESPACE_STRING} with ${NEW_NAMESPACE_STRING} in ./woocommerce/"
40+
find "${SCRIPT_DIR}/../../woocommerce/" -type f \( -name '*.php' -o -name '*.js' \) -exec sed -i "s/${OLD_NAMESPACE_STRING}/${NEW_NAMESPACE_STRING}/g" {} \;
41+
42+
# replace namespace in `./tests/` directory
43+
echo "Replacing instances of ${OLD_NAMESPACE_STRING} with ${NEW_NAMESPACE_STRING} in ./tests/"
44+
find "${SCRIPT_DIR}/../../tests/" -type f -name '*.php' -exec sed -i "s/${OLD_NAMESPACE_STRING}/${NEW_NAMESPACE_STRING}/g" {} \;
45+
46+
# replace version number in `./woocommerce/class-sv-wc-plugin.php` file
47+
echo "Replacing VERSION constant value in ./woocommerce/class-sv-wc-plugin.php file"
48+
sed -i -e "s/public const VERSION = '${OLD_VERSION}';/public const VERSION = '${NEW_VERSION}';/g" "${SCRIPT_DIR}/../../woocommerce/class-sv-wc-plugin.php"
49+
50+
# replace version number in `./woocommerce-framework-plugin-loader-sample.php` file
51+
echo "Replacing FRAMEWORK_VERSION constant value in ./woocommerce-framework-plugin-loader-sample.php file"
52+
sed -i -e "s/public const FRAMEWORK_VERSION = '${OLD_VERSION}';/public const FRAMEWORK_VERSION = '${NEW_VERSION}';/g" "${SCRIPT_DIR}/../../woocommerce-framework-plugin-loader-sample.php"
53+
54+
# add new changelog heading on line 3
55+
echo "Adding changelog heading for new version"
56+
CHANGELOG_HEADING="$(date +%Y).nn.nn - version ${NEW_VERSION}\n"
57+
sed -i "3i ${CHANGELOG_HEADING}" "${SCRIPT_DIR}/../../woocommerce/changelog.txt"

.github/workflows/prep-release.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,79 @@ name: Prepare Release
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
new-version:
7+
type: string
8+
description: "New version number"
9+
required: true
10+
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
15+
jobs:
16+
prepare-release:
17+
name: Prepare Release v${{ inputs.new-version }}
18+
runs-on: ubuntu-latest
19+
env:
20+
NEW_VERSION: ${{ inputs.new-version }}
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Validate release branch
26+
run: |
27+
BRANCH="release/${NEW_VERSION}"
28+
BRANCH_EXISTS=$(git ls-remote --heads origin $BRANCH | wc -l)
29+
30+
if [[ $BRANCH_EXISTS == '1' ]]; then
31+
echo The release/"${NEW_VERSION}" branch already exists. Please delete it to proceed.
32+
exit 1
33+
else
34+
echo The release/"${NEW_VERSION}" branch does not exist... proceeding!
35+
fi
36+
37+
- name: Create new release branch
38+
run: git checkout -b release/"${NEW_VERSION}"
39+
40+
- name: Replace namespaces and versions through entire codebase
41+
run: ./.github/scripts/update-namespace.sh
42+
43+
- name: Install Ruby
44+
uses: ruby/setup-ruby@v1
45+
with:
46+
ruby-version: '3.3'
47+
48+
- name: Install sass
49+
run: npm install -g sass
50+
51+
# We disable Xdebug because when enabled it makes the WP pot generation fail :shrug:
52+
- name: Disable Xdebug
53+
run: sudo phpdismod xdebug
54+
55+
- name: Setup WP-CLI
56+
uses: godaddy-wordpress/setup-wp-cli@1
57+
58+
- name: Install packages
59+
run: npm install
60+
61+
- name: Bump package.json version
62+
run: npm version --no-git-tag-version --allow-same-version "${NEW_VERSION}"
63+
64+
- name: Build assets
65+
run: npm run build
66+
67+
- name: Commit
68+
run: |
69+
git config user.name "github-actions[bot]"
70+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
71+
git add .
72+
git commit -m "Version ${NEW_VERSION}"
73+
git push --set-upstream origin release/"${NEW_VERSION}"
74+
75+
- name: Create pull request
76+
run: |
77+
gh pr create -B "${BASE_BRANCH}" -H release/"${NEW_VERSION}" --title "Release v${NEW_VERSION}" --body "Release v${NEW_VERSION}" --draft
78+
env:
79+
BASE_BRANCH: ${{ github.ref_name }}
80+
GH_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)