Skip to content

Commit

Permalink
Rebuilding the release workflow (#68)
Browse files Browse the repository at this point in the history
* Introducing a refactor the the built release workflow that does not create orphan releases

* Change job name

* Debug and adding checkout

* Adding debugging

* Adding more if checks

* Moving logic in brackets

* Addt changes

* Remove quote

* More cleanup

* Always check for front-end assets

* Simplify command if syntax

* Include sha in the commit

* Pass the target branch to gh release, allow github to create the tag

* Increase fetch depth for tag fetching

* Remove debug code

* Remove verify-tag

* Better commit message for releases

* Using different commit message for built

* Extract the previous tag version

* Wrapping up debug code

* Remove comment

* Adding a note to the readme

* Re-adjust the readme to reference the proper header

* Update README.md

Co-authored-by: Greg Marshall <[email protected]>

---------

Co-authored-by: Greg Marshall <[email protected]>
  • Loading branch information
srtfisher and mogmarsh authored Nov 30, 2023
1 parent 60a17b2 commit 0ee6279
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 45 deletions.
121 changes: 85 additions & 36 deletions .github/workflows/built-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ on:
composer_install:
description: 'Whether or not to run composer install'
required: false
default: false
default: true
type: boolean
cmd:
description: 'The npm command to run that will build the assets'
required: false
default: 'build'
type: string
php:
default: "8.1"
required: false
Expand All @@ -26,15 +31,18 @@ jobs:
extract-version:
runs-on: ubuntu-latest
outputs:
branch: ${{ steps.branch-name.outputs.branch }}
has_node_assets: ${{ steps.has_node_assets.outputs.exists }}
is_built_branch: ${{ steps.is-built-branch.outputs.is_built_branch }}
package_version: ${{ steps.extract-version.outputs.package_version }}
previous_tag_version: ${{ steps.previous-tag-version.outputs.previous_tag_version }}
tag_exists: ${{ steps.check-tag.outputs.tag_exists }}
tag_name: ${{ steps.tag-name.outputs.tag_name }}
if: ${{ github.repository != 'alleyinteractive/create-wordpress-plugin' }}
if: github.repository != 'alleyinteractive/create-wordpress-plugin'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
fetch-depth: 0

- name: Extract version
id: extract-version
Expand Down Expand Up @@ -81,49 +89,77 @@ jobs:
echo "package_version=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
fi
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
id: branch-name

- name: Check if this is a built branch
id: is-built-branch
shell: bash
run: |
if [[ ${{ steps.branch-name.outputs.branch }} =~ -built$ ]]; then
echo "is_built_branch=true" >> $GITHUB_OUTPUT
else
echo "is_built_branch=false" >> $GITHUB_OUTPUT
fi
- name: Compile the tag name from the version
id: tag-name
if: ${{ steps.extract-version.outputs.package_version }} != 'false'
if: steps.extract-version.outputs.package_version != 'false'
run: echo "tag_name=v${{ steps.extract-version.outputs.package_version }}" >> $GITHUB_OUTPUT

- name: Check if the version already exists as a tag
id: check-tag
if: ${{ steps.tag-name.outputs.tag_name }}
if: steps.extract-version.outputs.package_version != 'false' && steps.tag-name.outputs.tag_name
run: |
# Check if the tag exists
if [ -z "$(git tag -l ${{ steps.tag-name.outputs.tag_name }})" ]; then
echo "tag_exists=false" >> $GITHUB_OUTPUT
else
echo "tag_exists=true" >> $GITHUB_OUTPUT
fi
- name: Check if the plugin has front-end assets
- name: Extract previous tag version
id: previous-tag-version
if: steps.check-tag.outputs.tag_exists == 'false' && steps.extract-version.outputs.package_version != 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Get the release tag for comparison using the gh release command
PREVIOUS_TAG=$(gh release list --limit 1 | grep -Eo "v[0-9]+\.[0-9]+\.[0-9]+" | head -1)
# If there is no previous tag, then we can't compare
if [ -z "$PREVIOUS_TAG" ]; then
echo "previous_tag_version=false" >> $GITHUB_OUTPUT
else
echo "previous_tag_version=$PREVIOUS_TAG" >> $GITHUB_OUTPUT
fi
- name: Check if the project has front-end assets
shell: bash
id: has_node_assets
run: |
[[ -f package.json ]] && echo "exists=true" >> $GITHUB_OUTPUT || echo "exists=false" >> $GITHUB_OUTPUT
build-and-release:
build-branch-and-release:
name: Build assets and push to built branch
needs: extract-version
if : ${{ needs.extract-version.outputs.tag_exists }} && ${{ needs.extract-version.outputs.package_version }} != 'false' && ${{ needs.extract-version.outputs.has_node_assets }} == 'true' && ${{ github.repository != 'alleyinteractive/create-wordpress-plugin' }}
if: needs.extract-version.outputs.is_built_branch == 'false' && needs.extract-version.outputs.has_node_assets == 'true' && needs.extract-version.outputs.branch != 'false' && github.repository != 'alleyinteractive/create-wordpress-plugin'
runs-on: ubuntu-latest
env:
RELEASE_BRANCH: "release/${{ needs.extract-version.outputs.tag_name }}-${{ github.run_number }}"
VERSION_NAME: ${{ needs.extract-version.outputs.package_version }}
VERSION_TAG: ${{ needs.extract-version.outputs.tag_name }}
BUILT_BRANCH: '${{ needs.extract-version.outputs.branch }}-built'
CREATING_RELEASE: ${{ needs.extract-version.outputs.package_version != 'false' && needs.extract-version.outputs.tag_exists == 'false' && 'true' }}
CURRENT_BRANCH: '${{ needs.extract-version.outputs.branch }}'
PREVIOUS_TAG_VERSION: '${{ needs.extract-version.outputs.previous_tag_version }}'
VERSION_NAME: '${{ needs.extract-version.outputs.package_version }}'
VERSION_TAG: '${{ needs.extract-version.outputs.tag_name }}'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Debug variables
run: |
echo "RELEASE_BRANCH=$RELEASE_BRANCH"
echo "VERSION_NAME=$VERSION_NAME"
echo "VERSION_TAG=$VERSION_TAG"
fetch-depth: 0

- name: Install dependencies
if: ${{ inputs.composer_install }}
if: inputs.composer_install
uses: php-actions/composer@v6
with:
php_version: ${{ inputs.php }}
Expand All @@ -139,20 +175,20 @@ jobs:
run: |
[ -f package-lock.json ] && npm ci || npm i
- name: Run npm build
run: npm run build
- name: Run npm command
run: npm run ${{ inputs.cmd }}

- name: Push to release branch
- name: Push to ${{ needs.extract-version.outputs.branch }}-built branch
shell: bash
env:
DRAFT_RELEASE: ${{ inputs.draft }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --global user.email "[email protected]"
git config --global user.name "$GITHUB_ACTOR"
rm -rf .gitignore docker_tag output.log .github
# Clear out the .gitignore file if .deployignore exists
if [[ -e "$GITHUB_WORKSPACE/.deployignore" ]]; then
mv .deployignore .gitignore
elif [[ -e "$GITHUB_WORKSPACE/.distignore" ]]; then
Expand All @@ -161,21 +197,34 @@ jobs:
git ls-files -i -c --exclude-standard | xargs git rm --cached
git checkout -b $RELEASE_BRANCH
git checkout -b $BUILT_BRANCH
git add -A && git commit -m "Built changes for $VERSION_TAG"
git push --force -u origin "${RELEASE_BRANCH}"
git add -A
# Create a tag from the release branch
git tag -a $VERSION_TAG -m "Release $VERSION_TAG"
git push origin $VERSION_TAG
if [[ $CREATING_RELEASE == "true" ]]; then
git commit -m "Built changes for $VERSION_TAG from $CURRENT_BRANCH $GITHUB_SHA"
else
git commit -m "Built changes from $CURRENT_BRANCH $GITHUB_SHA"
fi
# Delete the release branch
git push origin --delete $RELEASE_BRANCH
git push --force -u origin "${BUILT_BRANCH}"
# Create the GitHub release
if [[ $DRAFT_RELEASE == "true" ]]; then
gh release create $VERSION_TAG -t "$VERSION_TAG" --generate-notes -d --verify-tag --latest
- name: Create release for built branch
if: needs.extract-version.outputs.package_version != 'false' && needs.extract-version.outputs.tag_exists == 'false'
shell: bash
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ "$PREVIOUS_TAG_VERSION" == "false" ]; then
if [[ $DRAFT_RELEASE == "true" ]]; then
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes -d --latest
else
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes --latest
fi
else
gh release create $VERSION_TAG -t "$VERSION_TAG" --generate-notes --verify-tag --latest
if [[ $DRAFT_RELEASE == "true" ]]; then
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes -d --latest --notes-start-tag "$PREVIOUS_TAG_VERSION"
else
gh release create $VERSION_TAG --target "$BUILT_BRANCH" -t "$VERSION_TAG" --generate-notes --latest --notes-start-tag "$PREVIOUS_TAG_VERSION"
fi
fi
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-approve.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
dependabot:
runs-on: ubuntu-latest
timeout-minutes: 10
if: ${{ github.actor == 'dependabot[bot]' }}
if: github.actor == 'dependabot[bot]'
steps:
- name: Dependabot metadata
id: metadata
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
dependabot:
runs-on: ubuntu-latest
timeout-minutes: 10
if: ${{ github.actor == 'dependabot[bot]' }}
if: github.actor == 'dependabot[bot]'
steps:
- name: Dependabot metadata
id: metadata
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/node-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
timeout-minutes: 10
steps:
- name: Cancel previous runs of this workflow (pull requests only)
if: ${{ github.event_name == 'pull_request' }}
if: github.event_name == 'pull_request'
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:

steps:
- name: Cancel previous runs of this workflow (pull requests only)
if: ${{ github.event_name == 'pull_request' }}
if: github.event_name == 'pull_request'
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,25 @@ jobs:
### Built Releases
Create built releases of a project based on the WordPress plugin `Stable Tag`
header and falling back to the `version` property in either `composer.json` or
`package.json`. When the version is updated in either file, the action
will build the project, push a new tag up with the version, and create a
release. Optionally, the release can be drafted or published.
Create built releases of a project based on the WordPress plugin `Version`
header in your main plugin file and then falling back to the `version` property in
either `composer.json` or `package.json`. When the version is updated in either
file, the action will build the project, push a new tag up with the version, and
create a release. Optionally, the release can be drafted or published.

The most common use of this workflow is for WordPress plugins or other packages
that require built assets (such as ones from Webpack or Gulp) to be included to
work but we don't want to include those assets in version control.

When the plugin's version is incremented on
`alleyinteractive/create-wordpress-plugin`-based plugins via `npm run release`,
the action will push a built version of the plugin to the `*-built` branch and
then create a release with the built assets. If the plugin's version was not
incremented, the action will still push the latest changes to the `*-built`
branch but will not create a release. This does mirror the
[Built Branch](#built-branch) workflow but is more flexible and allows for
publishing releases.

#### Inputs

> Specify using `with` keyword.
Expand Down

0 comments on commit 0ee6279

Please sign in to comment.