Skip to content

Commit 443bf66

Browse files
authored
Refactor GitHub Actions workflows (#764)
* Fix incorrect context for GitHub Actions workflow * Refactor GitHub Actions workflows ...to actually make it runnable * Guess it is in secrets * Use vars instead of secrets
1 parent b744cec commit 443bf66

File tree

3 files changed

+87
-47
lines changed

3 files changed

+87
-47
lines changed

.github/scripts/notes.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
1-
module.exports = async ({ core, fetch }) => {
2-
const { VERSION } = process.env
3-
const slug = `version-${VERSION.replaceAll('.', '-')}`
1+
module.exports = async ({ github, core, fetch }) => {
2+
const { TAGS_MATRIX, META_PACKAGE } = process.env
3+
const tags = JSON.parse(TAGS_MATRIX)
44

5-
try {
6-
const res = await fetch('https://wordpress.org/documentation/wp-json/wp/v2/wordpress-versions?per_page=50')
7-
const data = await res.json()
5+
return Promise.allSettled(
6+
tags.map((tag_name) =>
7+
core.group(tag_name, async (tag_name) => {
8+
const slug = `version-${tag_name.replaceAll('.', '-')}`
89

9-
const release = data.find((tag) => tag.slug === slug)
10-
if (!release) {
11-
throw Error('Release not found')
12-
}
10+
try {
11+
const res = await fetch(
12+
'https://wordpress.org/documentation/wp-json/wp/v2/wordpress-versions?per_page=50'
13+
)
14+
const data = await res.json()
1315

14-
const { link } = release
15-
const body = release.content?.rendered?.split('<h2', 4)[2]
16-
if (!body) {
17-
throw Error('Release body is empty or unexpected')
18-
}
16+
const release = data.find((tag) => tag.slug === slug)
17+
if (!release) {
18+
throw Error('Release not found')
19+
}
1920

20-
return `_Sourced from [WordPress.org Documentation](${link})._\n\n<h2${body}`
21-
} catch (e) {
22-
core.warning(e)
21+
const { link } = release
22+
const body = release.content?.rendered?.split('<h2', 4)[2]
23+
if (!body) {
24+
throw Error('Release body is empty or unexpected')
25+
}
2326

24-
return `_Version notes available on [WordPress.org Documentation](https://wordpress.org/documentation/wordpress-version/${slug}/)._`
25-
}
27+
const body = `_Sourced from [WordPress.org Documentation](${link})._\n\n<h2${body}`
28+
} catch (e) {
29+
core.error(e)
30+
31+
const body = `_Version notes available on [WordPress.org Documentation](https://wordpress.org/documentation/wordpress-version/${slug}/)._`
32+
}
33+
34+
core.info('Publishing')
35+
return github.rest.repos.createRelease({
36+
owner: context.repo.owner,
37+
repo: META_PACKAGE.substring(META_PACKAGE.indexOf('/') + 1),
38+
tag_name,
39+
body,
40+
name: `Version ${tag_name}`,
41+
make_latest: 'legacy',
42+
})
43+
})
44+
)
45+
)
2646
}

.github/workflows/meta-package.yml

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,40 @@ name: Meta-package
33
on:
44
workflow_call:
55
workflow_dispatch:
6+
pull_request:
7+
branches:
8+
- main
69

710
jobs:
8-
sync:
9-
name: Sync
11+
package:
12+
name: Package
1013
runs-on: ubuntu-latest
1114
outputs:
12-
tags-matrix: ${{ steps.tags-matrix.outputs.result }}
15+
package-name: ${{ steps.package.outputs.package-name }}
1316
steps:
1417
- uses: actions/checkout@v3
1518
with:
16-
repository: ${{ secrets.META_PACKAGE }}
19+
repository: ${{ vars.META_PACKAGE }}
1720

1821
- name: Get upstream package name
1922
id: package
2023
run: echo "package-name=$(jq -r '.require | map_values(select(. == "self.version")) | keys[0]' composer.json)" >> $GITHUB_OUTPUT
2124

25+
sync:
26+
name: Sync
27+
runs-on: ubuntu-latest
28+
needs:
29+
- package
30+
outputs:
31+
tags-matrix: ${{ steps.tags-matrix.outputs.result }}
32+
steps:
33+
- uses: actions/checkout@v3
2234
- name: Generate matrix from versions arrays
2335
id: tags-matrix
2436
uses: actions/github-script@v6
2537
env:
26-
PACKAGE: ${{ steps.package.outputs.package-name }}
27-
META: ${{ secrets.META_PACKAGE }}
38+
PACKAGE: ${{ needs.package.outputs.package-name }}
39+
META: ${{ vars.META_PACKAGE }}
2840
with:
2941
script: |
3042
const tags = require('${{ github.workspace }}/.github/scripts/tags.js')
@@ -36,6 +48,7 @@ jobs:
3648
needs:
3749
- sync
3850
if: needs.sync.outputs.tags-matrix != '[]'
51+
continue-on-error: true
3952
strategy:
4053
fail-fast: false
4154
matrix:
@@ -50,20 +63,9 @@ jobs:
5063

5164
- uses: actions/checkout@v3
5265
with:
53-
repository: ${{ secrets.META_PACKAGE }}
66+
repository: ${{ vars.META_PACKAGE }}
5467
token: ${{ steps.generate-token.outputs.token }}
5568

56-
- name: Retrieve version notes
57-
id: notes
58-
uses: actions/github-script@v6
59-
env:
60-
VERSION: ${{ matrix.tag }}
61-
with:
62-
result-encoding: string
63-
script: |
64-
const notes = require('${{ github.workspace }}/.github/scripts/notes.js')
65-
return await notes({ core, fetch })
66-
6769
- name: Push tag
6870
env:
6971
TAG: ${{ matrix.tag }}
@@ -73,11 +75,29 @@ jobs:
7375
git tag -a "${TAG}" -m "${TAG}"
7476
git push origin "${TAG}"
7577
76-
- name: Publish release
77-
uses: softprops/action-gh-release@v1
78+
releases:
79+
name: Releases
80+
runs-on: ubuntu-latest
81+
needs:
82+
- sync
83+
- tags
84+
steps:
85+
- name: Generate token
86+
uses: tibdex/github-app-token@v1
87+
id: generate-token
7888
with:
79-
repository: ${{ secrets.META_PACKAGE }}
80-
token: ${{ steps.generate-token.outputs.token }}
81-
body: ${{ steps.notes.outputs.result }}
82-
name: Version ${{ matrix.tag }}
83-
tag_name: ${{ matrix.tag }}
89+
app_id: ${{ secrets.BOT_APP_ID }}
90+
private_key: ${{ secrets.BOT_PRIVATE_KEY }}
91+
92+
- uses: actions/checkout@v3
93+
94+
- name: Retrieve version notes
95+
uses: actions/github-script@v6
96+
env:
97+
TAGS_MATRIX: ${{ needs.sync.outputs.tags-matrix }}
98+
META_PACKAGE: ${{ vars.META_PACKAGE }}
99+
with:
100+
github-token: ${{ steps.generate-token.outputs.token }}
101+
script: |
102+
const notes = require('${{ github.workspace }}/.github/scripts/notes.js')
103+
await notes({ github, core, fetch })

.github/workflows/run.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ jobs:
5050
- name: Run
5151
run: composer run build -- $REMOTE $PACKAGE --type=$TYPE --unstable
5252
env:
53-
REMOTE: https://${{ github.actor }}:${{ steps.generate-token.outputs.token }}@github.com/${{ github.repository_owner }}/${{ secrets.PACKAGE_PREFIX }}${{ matrix.release-type }}.git
54-
PACKAGE: ${{ github.repository_owner }}/${{ secrets.PACKAGE_PREFIX }}${{ matrix.release-type }}
53+
REMOTE: https://${{ github.actor }}:${{ steps.generate-token.outputs.token }}@github.com/${{ github.repository_owner }}/${{ vars.PACKAGE_PREFIX }}${{ matrix.release-type }}.git
54+
PACKAGE: ${{ github.repository_owner }}/${{ vars.PACKAGE_PREFIX }}${{ matrix.release-type }}
5555
TYPE: ${{ matrix.release-type }}
5656

5757
meta-package:

0 commit comments

Comments
 (0)