Repository navigation
258 lines (230 loc) · 11.3 KB
/
Copy pathrelease-php.yml
File metadata and controls
258 lines (230 loc) · 11.3 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
name: Release Composer Packages
# Splits composer-packages/* out to their standalone mirror repos so Packagist
# can serve rtcamp/wp-phpcs and rtcamp/wp-phpstan from their own repositories.
# Fired by the tag that `vendor/bin/monorepo-builder release` pushes.
#
# splitsh.json is the single source of truth for mirror repo -> subdirectory.
# To add a package, add it there and list its deploy-key secret in the split
# step's env below (secrets are referenced by name so the job only receives
# the deploy keys, never the whole secrets context).
#
# Each mirror is pushed with its own SSH deploy key (repo secret
# SPLIT_KEY_<NAME>, "Allow write access" required) rather than a PAT bound to
# one human account. `git subtree split` output is deterministic, so releases
# fast-forward and a diverged mirror fails loudly instead of being silently
# rewritten.
on:
push:
tags:
- "v*"
# Smoke-test the split without cutting a real release tag.
workflow_dispatch:
inputs:
test_tag:
description: "Throwaway tag to push to the mirrors (e.g. v0.0.0-test)"
required: true
default: "v0.0.0-test"
force_push:
description: "Force-push the mirrors (bootstrap an empty mirror, or re-push the same test tag)"
type: boolean
required: false
default: false
resync_branch:
description: "Only force the mirror branch back onto the split (repairs a diverged mirror); pushes no tag"
type: boolean
required: false
default: false
# Pushes to the mirrors must not race each other.
concurrency:
group: split-composer-packages
cancel-in-progress: false
permissions:
contents: read
jobs:
build-matrix:
name: Build split matrix from config
runs-on: ubuntu-latest
env:
CONFIG_FILE: splitsh.json
outputs:
matrix: ${{ steps.parse.outputs.matrix }}
organization: ${{ steps.parse.outputs.organization }}
branch: ${{ steps.parse.outputs.branch }}
tag: ${{ steps.parse.outputs.tag }}
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- name: Parse config into a matrix
id: parse
shell: bash
env:
# workflow_dispatch runs from a branch, so the input has to win or the
# mirrors get tagged "main"; on a tag push ref_name *is* the tag.
TAG: ${{ github.event_name == 'workflow_dispatch' && inputs.test_tag || github.ref_name }}
run: |
set -euo pipefail
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "::error::Config file '$CONFIG_FILE' not found (path is relative to the repo root)."
exit 1
fi
if ! jq empty "$CONFIG_FILE" 2>/dev/null; then
echo "::error::Config file '$CONFIG_FILE' is not valid JSON."
exit 1
fi
# Accepts both the shorthand string form ("composer-packages/foo") and
# the object form ({ "prefixes": [{ "from": "..." }] }), and derives
# the deploy-key secret: wp-phpcs -> SPLIT_KEY_PHPCS. A "secret" key
# on the object form replaces the derived suffix; the gsub keeps a
# future wp-foo-bar from producing an invalid secret name.
if ! matrix=$(jq -c '{
include: [
(.subtrees // {}) | to_entries[] | {
mirror_repo: .key,
package_directory: (
if (.value | type) == "string" then .value
elif ((.value | type) == "object")
and ((.value.prefixes | type) == "array")
and ((.value.prefixes | length) > 0)
and ((.value.prefixes[0].from | type) == "string") then .value.prefixes[0].from
else error("invalid subtrees entry for \"" + .key + "\": expected a string path or { prefixes: [{ from: \"...\" }] }")
end
),
secret_name: (
"SPLIT_KEY_" + (
(if (.value | type) == "object" then (.value.secret // empty) else empty end)
// (.key | sub("^wp-"; "") | ascii_upcase | gsub("[^A-Z0-9]"; "_"))
)
)
}
]
}' "$CONFIG_FILE"); then
echo "::error::Invalid 'subtrees' entry in '$CONFIG_FILE' — each value must be a string path or { prefixes: [{ from: \"...\" }] } (see jq error above)."
exit 1
fi
count=$(jq -r '.include | length' <<<"$matrix")
if [[ "$count" -eq 0 ]]; then
echo "::error::No subtrees defined in '$CONFIG_FILE'. Nothing to split."
exit 1
fi
# Guards a dispatch with an emptied-out test_tag, which would otherwise
# fall through to a branch name and tag the mirrors with it.
if [[ "$TAG" != v* ]]; then
echo "::error::Refusing to push '$TAG' to the mirrors — the tag must start with 'v'."
exit 1
fi
# defaults.user_name / user_email are deliberately not required:
# git subtree split keeps each commit's original author, date and
# message, so there is no bot identity to configure.
organization=$(jq -r '.organization // empty' "$CONFIG_FILE")
branch=$(jq -r '.defaults.branch // empty' "$CONFIG_FILE")
if [[ -z "$organization" || -z "$branch" ]]; then
echo "::error::Missing required keys in '$CONFIG_FILE' — organization and defaults.branch must both be set."
exit 1
fi
{
echo "matrix=$matrix"
echo "organization=$organization"
echo "branch=$branch"
echo "tag=$TAG"
} >> "$GITHUB_OUTPUT"
- name: Echo resolved matrix
env:
MATRIX: ${{ steps.parse.outputs.matrix }}
run: jq . <<<"$MATRIX"
split:
name: Split ${{ matrix.mirror_repo }}
needs: build-matrix
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.build-matrix.outputs.matrix) }}
steps:
- name: Checkout monorepo (full history)
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0 # the split walks every commit that touched the subtree
persist-credentials: false # this job pushes with a deploy key, nothing else
- name: Split ${{ matrix.package_directory }} → ${{ matrix.mirror_repo }}
shell: bash
env:
# Named explicitly: a dynamic secrets[...] lookup would hand the job the
# whole secrets context. A new mirror's key has to be listed here.
SPLIT_KEY_PHPCS: ${{ secrets.SPLIT_KEY_PHPCS }}
SPLIT_KEY_PHPSTAN: ${{ secrets.SPLIT_KEY_PHPSTAN }}
SECRET_NAME: ${{ matrix.secret_name }}
PKG_PATH: ${{ matrix.package_directory }}
ORG: ${{ needs.build-matrix.outputs.organization }}
REPO: ${{ matrix.mirror_repo }}
BRANCH: ${{ needs.build-matrix.outputs.branch }}
TAG: ${{ needs.build-matrix.outputs.tag }}
FORCE_PUSH: ${{ github.event_name == 'workflow_dispatch' && inputs.force_push }}
RESYNC_BRANCH: ${{ github.event_name == 'workflow_dispatch' && inputs.resync_branch }}
run: |
set -euo pipefail
# The name comes from splitsh.json, so check its shape before the
# indirect expansion reads a variable by that name.
if [[ ! "$SECRET_NAME" =~ ^SPLIT_KEY_[A-Z0-9_]+$ ]]; then
echo "::error::Refusing deploy-key secret name '$SECRET_NAME': it must look like SPLIT_KEY_<NAME>."
exit 1
fi
SSH_KEY="${!SECRET_NAME:-}"
if [[ -z "$SSH_KEY" ]]; then
echo "::error::No deploy key in $SECRET_NAME. Add the private half of ${ORG}/${REPO}'s write-enabled deploy key as that repository secret, and list it in this step's env."
exit 1
fi
if [[ ! -x "$(git --exec-path)/git-subtree" ]]; then
echo "::error::git subtree is unavailable on this runner (looked in $(git --exec-path))."
exit 1
fi
if [[ ! -d "$PKG_PATH" ]]; then
echo "::error::Package directory '$PKG_PATH' does not exist at ${GITHUB_SHA}. Fix 'subtrees' in splitsh.json."
exit 1
fi
# The key stays in a 0600 file, never an env var git could echo.
# IdentitiesOnly ignores anything the runner's ssh-agent holds, so the
# wrong mirror's key can never be offered.
install -m 700 -d "$HOME/.ssh"
printf '%s\n' "$SSH_KEY" > "$HOME/.ssh/split_key"
chmod 600 "$HOME/.ssh/split_key"
ssh-keyscan -t ed25519 github.com >> "$HOME/.ssh/known_hosts" 2>/dev/null
if ! grep -q '^github.com ' "$HOME/.ssh/known_hosts"; then
echo "::error::ssh-keyscan returned no host key for github.com; refusing to push without host verification."
exit 1
fi
export GIT_SSH_COMMAND="ssh -i $HOME/.ssh/split_key -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes"
split_sha=$(git subtree split -q --prefix="$PKG_PATH" HEAD | tail -n 1)
if [[ ! "$split_sha" =~ ^[0-9a-f]{40}$ ]]; then
echo "::error::git subtree split --prefix='$PKG_PATH' did not return a commit SHA (got '$split_sha')."
exit 1
fi
echo "Split '$PKG_PATH' at ${GITHUB_SHA} -> $split_sha"
# A release must fast-forward. force_push is dispatch-only: it
# bootstraps a mirror whose initial commit is not an ancestor of the
# split history, and re-pushes a throwaway smoke-test tag.
push=(git push)
if [[ "$FORCE_PUSH" == "true" ]]; then
push+=(--force)
echo "::warning::force_push is on — $ORG/$REPO will be rewritten to match this split."
fi
remote="git@github.com:$ORG/$REPO.git"
# Repair mode: put the mirror branch back onto the split and stop, so a
# mirror that something else committed to (an org bot PR, a hand edit)
# can be fixed without minting a tag that Packagist would publish.
if [[ "$RESYNC_BRANCH" == "true" ]]; then
echo "::warning::resync_branch is on: $ORG/$REPO $BRANCH is being forced onto the split; no tag is pushed."
git push --force "$remote" "$split_sha:refs/heads/$BRANCH"
echo "- **$ORG/$REPO**: \`$BRANCH\` resynced to \`$split_sha\` (no tag)" >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
if ! "${push[@]}" "$remote" "$split_sha:refs/heads/$BRANCH"; then
echo "::error::$ORG/$REPO $BRANCH did not fast-forward: something other than this workflow committed to the mirror. Inspect what it holds, then run this workflow by dispatch on the $TAG tag with resync_branch enabled, and re-run this release run."
exit 1
fi
# Packagist versions a package from its tags, so this push is not optional.
"${push[@]}" "$remote" "$split_sha:refs/tags/$TAG"
echo "- **$ORG/$REPO** — \`$PKG_PATH\` → \`$split_sha\` on \`$BRANCH\`, tagged \`$TAG\`" >> "$GITHUB_STEP_SUMMARY"
- name: Remove deploy key
if: always()
run: rm -f "$HOME/.ssh/split_key"