Skip to content

Commit 5ac0e99

Browse files
committed
[actions] add workflow to update nodejs.org nvm version
Ref: nodejs/nodejs.org#8628
1 parent 62387b8 commit 5ac0e99

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

.github/workflows/nodejs-org.yml

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
name: 'Update nodejs.org'
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
workflow_dispatch:
8+
inputs:
9+
version:
10+
description: 'nvm version tag (e.g., v0.40.4). Defaults to latest release.'
11+
required: false
12+
default: ''
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
update-nodejs-org:
19+
if: github.repository == 'nvm-sh/nvm' && github.actor == 'ljharb'
20+
permissions:
21+
contents: none
22+
name: 'Create PR to nodejs/nodejs.org'
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Harden Runner
26+
uses: step-security/harden-runner@v2
27+
with:
28+
allowed-endpoints:
29+
github.com:443
30+
api.github.com:443
31+
32+
- name: Extract and validate version
33+
id: version
34+
run: |
35+
set -euo pipefail
36+
37+
INPUT_VERSION="${{ inputs.version }}"
38+
39+
if [ -n "${INPUT_VERSION}" ]; then
40+
TAG="${INPUT_VERSION}"
41+
elif [ "${GITHUB_REF_TYPE}" = "tag" ]; then
42+
TAG="${GITHUB_REF#refs/tags/}"
43+
else
44+
TAG="$(gh api "repos/${GITHUB_REPOSITORY}/releases/latest" --jq '.tag_name')"
45+
fi
46+
47+
if ! printf '%s\n' "${TAG}" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
48+
echo "::notice::Tag '${TAG}' does not match expected format vX.Y.Z, skipping"
49+
exit 0
50+
fi
51+
52+
printf 'tag=%s\n' "${TAG}" >> "${GITHUB_OUTPUT}"
53+
env:
54+
GH_TOKEN: ${{ github.token }}
55+
56+
- name: Set up fork and clone
57+
if: steps.version.outputs.tag
58+
id: fork
59+
run: |
60+
set -euo pipefail
61+
62+
gh repo fork nodejs/nodejs.org --clone=false 2>&1 || true
63+
64+
FORK_OWNER="$(gh api user --jq '.login')"
65+
printf 'fork_owner=%s\n' "${FORK_OWNER}" >> "${GITHUB_OUTPUT}"
66+
67+
git clone --depth 1 https://github.com/nodejs/nodejs.org.git nodejs.org
68+
git -C nodejs.org remote add fork "https://x-access-token:${GH_TOKEN}@github.com/${FORK_OWNER}/nodejs.org.git"
69+
env:
70+
GH_TOKEN: ${{ secrets.NODEJS_ORG_TOKEN }}
71+
72+
- name: Update nvm version in English snippet
73+
if: steps.version.outputs.tag
74+
run: |
75+
set -euo pipefail
76+
77+
NEW_VERSION="${{ steps.version.outputs.tag }}"
78+
FILE="nodejs.org/apps/site/snippets/en/download/nvm.bash"
79+
PATTERN='nvm-sh/nvm/v[0-9]+\.[0-9]+\.[0-9]+/install\.sh'
80+
REPLACEMENT="nvm-sh/nvm/${NEW_VERSION}/install.sh"
81+
82+
if [ ! -f "${FILE}" ]; then
83+
echo "::error::English snippet not found at ${FILE}"
84+
exit 1
85+
fi
86+
87+
MATCH_COUNT="$(grep -cE "${PATTERN}" "${FILE}" || true)"
88+
89+
if [ "${MATCH_COUNT}" -eq 0 ]; then
90+
echo "::error::No nvm version pattern found in ${FILE}"
91+
exit 1
92+
fi
93+
94+
if [ "${MATCH_COUNT}" -ne 1 ]; then
95+
echo "::error::Expected exactly 1 nvm version match in ${FILE}, found ${MATCH_COUNT}"
96+
exit 1
97+
fi
98+
99+
sed -i -E "s|${PATTERN}|${REPLACEMENT}|g" "${FILE}"
100+
101+
if ! grep -qF "${REPLACEMENT}" "${FILE}"; then
102+
echo "::error::Replacement verification failed in ${FILE}"
103+
exit 1
104+
fi
105+
106+
echo "Updated: ${FILE}"
107+
108+
- name: Create pull request
109+
if: steps.version.outputs.tag
110+
run: |
111+
set -euo pipefail
112+
113+
NEW_VERSION="${{ steps.version.outputs.tag }}"
114+
BRANCH="nvm-${NEW_VERSION}"
115+
FORK_OWNER="${{ steps.fork.outputs.fork_owner }}"
116+
117+
cd nodejs.org
118+
119+
git checkout -b "${BRANCH}"
120+
git add -A
121+
122+
if git diff --cached --quiet; then
123+
echo "::notice::English snippet already has version ${NEW_VERSION}"
124+
exit 0
125+
fi
126+
127+
git config user.name "github-actions[bot]"
128+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
129+
git commit -m "meta: bump nvm to ${NEW_VERSION}"
130+
131+
git push fork "${BRANCH}"
132+
133+
BODY="Updates the English nvm install snippet to [\`${NEW_VERSION}\`](https://github.com/nvm-sh/nvm/releases/tag/${NEW_VERSION}). The translation system handles other locales.
134+
135+
Ref: https://github.com/nodejs/nodejs.org/issues/8628"
136+
137+
gh pr create \
138+
--repo nodejs/nodejs.org \
139+
--head "${FORK_OWNER}:${BRANCH}" \
140+
--title "meta: bump nvm to ${NEW_VERSION}" \
141+
--body "${BODY}"
142+
env:
143+
GH_TOKEN: ${{ secrets.NODEJS_ORG_TOKEN }}

0 commit comments

Comments
 (0)