Gutenberg packages update #455
This file contains 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: Gutenberg packages update | |
on: | |
# Allow for the workflow to be manually run if needed. | |
workflow_dispatch: | |
schedule: | |
# Once a day (https://crontab.guru/once-a-day) | |
- cron: '0 0 * * *' | |
permissions: | |
pull-requests: write | |
contents: write | |
# Cancel previous workflow run groups that have not completed. | |
concurrency: | |
# Group workflow runs by workflow name, along with the head branch ref of the pull request | |
# or otherwise the branch or tag ref. | |
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.ref }} | |
cancel-in-progress: true | |
jobs: | |
check-gutenberg-release: | |
name: Check for a new Gutenberg release | |
runs-on: ubuntu-latest | |
outputs: | |
latest-version: ${{ steps.latest-release.outputs.version }} | |
should-update: ${{ steps.release-status.outputs.outdated }} | |
steps: | |
- name: Get latest release version | |
id: latest-release | |
run: echo "::set-output name=version::$(gh api -X GET repos/wordpress/gutenberg/releases/latest --jq '.name')" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Get release version from last PR | |
id: last-release | |
run: | | |
PR_TITLE=$(gh api -X GET search/issues -f q='${{ env.QUERY }}' -f sort='created' -f order='desc' --jq '.items.[0].title') | |
LAST_VERSION=$(sed -r 's/.+ v(.+) .+/\1/' <<< "$PR_TITLE") | |
if ! egrep -q '^[0-9][0-9]*(\.[0-9][0-9]*)*$' <<< "$LAST_VERSION"; then | |
LAST_VERSION='0.0.0' | |
fi | |
echo "::set-output name=version::$(echo "$LAST_VERSION")" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
QUERY: 'repo:rtCamp/theme-elementary is:pr author:rtBot is:merged in:title Update Gutenberg packages after' | |
- name: Determine if package updates are needed | |
id: release-status | |
run: | | |
echo "Last version: $LAST_VER" | |
echo "Latest version: $LATEST_VER" | |
echo "::set-output name=outdated::$(php -r 'echo json_encode(version_compare($argv[1], $argv[2], ">"));' "$LATEST_VER" "$LAST_VER")" | |
env: | |
LAST_VER: ${{ steps.last-release.outputs.version }} | |
LATEST_VER: ${{ steps.latest-release.outputs.version }} | |
close-latest-pr: | |
name: Close latest open PR if one exists | |
# Run job if there is a new Gutenberg release. | |
if: needs.check-gutenberg-release.outputs.should-update == 'true' | |
runs-on: ubuntu-latest | |
needs: check-gutenberg-release | |
steps: | |
- name: Get latest open PR | |
id: latest-pr | |
run: | | |
PR_NUM=$(gh api -X GET search/issues -f q='${{ env.QUERY }}' -f sort='created' -f order='desc' --jq '.items.[0].number') | |
echo "::set-output name=num::$(echo $PR_NUM)" | |
echo "Latest PR number: ${PR_NUM}" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
QUERY: 'repo:rtCamp/theme-elementary is:pr author:rtBot is:open in:title Update Gutenberg packages after' | |
# Needed to later close PR. | |
- name: Checkout repo | |
if: steps.latest-pr.num != '' | |
uses: actions/checkout@v3 | |
- name: Close latest open PR | |
if: steps.latest-pr.num != '' | |
run: gh pr close ${{ env.PR_NUM }} --delete-branch | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR_NUM: ${{ steps.latest-pr.num }} | |
update-packages: | |
name: Update Gutenberg npm dependencies | |
# Run job if there is a new Gutenberg release. | |
if: needs.check-gutenberg-release.outputs.should-update == 'true' | |
runs-on: ubuntu-latest | |
needs: check-gutenberg-release | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
# Fetch history for all branches and tags to allow for successful merge of base branch if needed. | |
fetch-depth: 0 | |
- name: Determine branch name | |
id: branches | |
run: | | |
echo "::set-output name=base::$(echo ${GITHUB_REF#refs/heads/})" | |
echo "::set-output name=head::$(echo "update/gutenberg-v$VERSION-packages")" | |
env: | |
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }} | |
- name: Setup Node | |
uses: actions/setup-node@v3.8.1 | |
with: | |
node-version-file: '.nvmrc' | |
cache: npm | |
- name: Configure git user | |
run: | | |
git config user.name rtBot | |
git config user.email '43742164+rtBot@users.noreply.github.com' | |
- name: Check if remote branch exists | |
id: remote-branch | |
run: echo ::set-output name=exists::$([[ -z $(git ls-remote --heads origin "$HEAD_BRANCH" ) ]] && echo "0" || echo "1") | |
env: | |
HEAD_BRANCH: ${{ steps.branches.outputs.head }} | |
- name: Create branch to base pull request on | |
if: steps.remote-branch.outputs.exists == 0 | |
run: git checkout -b "$HEAD_BRANCH" | |
env: | |
HEAD_BRANCH: ${{ steps.branches.outputs.head }} | |
- name: Fetch existing branch to add commits to | |
if: steps.remote-branch.outputs.exists == 1 | |
run: | | |
git checkout "$HEAD_BRANCH" | |
git merge --no-edit "$BASE_BRANCH" | |
env: | |
BASE_BRANCH: ${{ steps.branches.outputs.base }} | |
HEAD_BRANCH: ${{ steps.branches.outputs.head }} | |
- name: Install Node dependencies | |
run: npm ci --ignore-scripts | |
env: | |
CI: true | |
- name: Check package updates | |
id: packages | |
run: | | |
# Get list of latest package versions. | |
PACKAGES=$(npm outdated --parseable | cut -d':' -f 4 | grep @wordpress | paste -s -d' ' || echo 0) | |
echo "::set-output name=list::$(echo "$PACKAGES")" | |
- name: Update packages | |
if: steps.packages.outputs.list != 0 | |
run: npm i $(echo "$PACKAGES") | |
env: | |
PACKAGES: ${{ steps.packages.outputs.list }} | |
- name: Commit and push changes | |
if: steps.packages.outputs.list != 0 | |
run: | | |
git add --all . | |
git commit -m "Update WordPress NPM packages based on Gutenberg release v$VERSION" -m "Signed-off-by: $(git config user.name) <$(git config user.email)>" | |
git push -u origin "$HEAD_BRANCH" | |
env: | |
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }} | |
HEAD_BRANCH: ${{ steps.branches.outputs.head }} | |
- name: Create a pull request | |
id: pr-create | |
if: steps.packages.outputs.list != 0 && steps.remote-branch.outputs.exists == 0 | |
run: | | |
PR_BODY=$(node .github/bin/gutenberg-packages-update-pr-body.js "${{ steps.packages.outputs.list }}") | |
PR_URL=$(gh pr create --base "$BASE_BRANCH" --title "Update Gutenberg packages after v$VERSION release" --body "$PR_BODY" --label Dependencies --label NPM | grep https://) | |
echo "::set-output name=pr-url::$PR_URL" | |
env: | |
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }} | |
GITHUB_TOKEN: ${{secrets.GH_BOT_TOKEN}} | |
BASE_BRANCH: ${{ steps.branches.outputs.base }} | |
HEAD_BRANCH: ${{ steps.branches.outputs.head }} | |
- name: Add PR reviewers | |
if: steps.pr-create.outputs.pr-url != '' | |
# Add a PR reviewer so that a notification can be sent to the concerned person. | |
run: gh pr edit "$PR_URL" --add-reviewer "$REVIEWER_USERNAME" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GH_BOT_TOKEN }} | |
PR_URL: ${{ steps.pr-create.outputs.pr-url }} | |
REVIEWER_USERNAME: thelovekesh |