-
Notifications
You must be signed in to change notification settings - Fork 381
170 lines (150 loc) · 6.4 KB
/
gutenberg-packages-update.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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 * * *'
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:ampproject/amp-wp is:pr author:app/github-actions 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:ampproject/amp-wp is:pr author:app/github-actions is:open in:title Update Gutenberg packages after'
# Needed to later close PR.
- name: Checkout repo
if: steps.latest-pr.num != ''
uses: actions/checkout@v2
- 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@v2
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: Read .nvmrc
run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
id: nvm
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: ${{ steps.nvm.outputs.NVMRC }}
cache: npm
- name: Configure git user
run: |
git config user.email "pierregordon@protonmail.com"
git config user.name "Pierre Gordon"
- 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
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 Gutenberg package dependencies"
git push origin "$HEAD_BRANCH"
env:
HEAD_BRANCH: ${{ steps.branches.outputs.head }}
- name: Create pull request
if: steps.packages.outputs.list != 0 && steps.remote-branch.outputs.exists == 0
run: |
git push -u origin "$HEAD_BRANCH"
PR_URL=$(gh pr create --base "$BASE_BRANCH" --title "Update Gutenberg packages after v$VERSION release" --body "" --label dependencies | grep https://)
gh pr merge --auto --merge "$PR_URL"
env:
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }}
GITHUB_TOKEN: ${{ secrets.GUTENBERG_PACKAGES_UPDATE_TOKEN }}
BASE_BRANCH: ${{ steps.branches.outputs.base }}
HEAD_BRANCH: ${{ steps.branches.outputs.head }}