Skip to content

Commit 0e91549

Browse files
authored
chore(ci): automatically deploy selected examples (#47130)
fix NEXT-822 ([link](https://linear.app/vercel/issue/NEXT-822)) ([NEXT-822](https://linear.app/vercel/issue/NEXT-822)) ---------
1 parent 4c3f59f commit 0e91549

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

.github/workflows/build_test_deploy.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,29 @@ jobs:
952952
- run: ./scripts/publish-native.js
953953
- run: ./scripts/publish-release.js
954954

955+
deployExamples:
956+
name: Deploy examples
957+
runs-on: ubuntu-latest
958+
needs: [publishRelease]
959+
steps:
960+
- uses: actions/checkout@v3
961+
with:
962+
fetch-depth: 25
963+
- name: Install Vercel CLI
964+
run: npm i -g [email protected]
965+
- name: Deploy preview examples
966+
if: ${{ needs.build.outputs.isRelease != 'true' }}
967+
run: ./scripts/deploy-examples.sh
968+
env:
969+
VERCEL_API_TOKEN: ${{ secrets.VERCEL_API_TOKEN }}
970+
DEPLOY_ENVIRONMENT: preview
971+
- name: Deploy production examples
972+
if: ${{ needs.build.outputs.isRelease == 'true' }}
973+
run: ./scripts/deploy-examples.sh
974+
env:
975+
VERCEL_API_TOKEN: ${{ secrets.VERCEL_API_TOKEN }}
976+
DEPLOY_ENVIRONMENT: production
977+
955978
testDeployE2E:
956979
name: E2E (deploy)
957980
runs-on: ubuntu-latest

examples/image-component/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"react-dom": "^18.2.0"
1212
},
1313
"devDependencies": {
14-
"@types/node": "18.7.14",
15-
"@types/react": "16.9.17",
16-
"typescript": "4.8.2"
14+
"@types/node": "18.15.3",
15+
"@types/react": "18.0.28",
16+
"typescript": "4.9.5"
1717
}
1818
}

scripts/deploy-examples.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
CHANGED_EXAMPLES=$(node scripts/run-for-change.js --type deploy-examples --listChangedDirectories)
5+
PROD=""
6+
if [ "$DEPLOY_ENVIRONMENT" = "production" ]; then
7+
PROD="--prod"
8+
fi
9+
10+
for CWD in $CHANGED_EXAMPLES ; do
11+
HYPHENS=$(echo "$CWD" | tr '/' '-')
12+
PROJECT="nextjs-$HYPHENS"
13+
echo "Deploying directory $CWD as $PROJECT to Vercel..."
14+
vercel link --cwd "$CWD" --scope vercel --project "$PROJECT" --token "$VERCEL_API_TOKEN" --yes
15+
vercel deploy --cwd "$CWD" --token "$VERCEL_API_TOKEN" $PROD
16+
done;

scripts/run-for-change.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const CHANGE_ITEM_GROUPS = {
3434
'packages/font/README.md',
3535
'packages/next-env/README.md',
3636
],
37+
'deploy-examples': ['examples/image-component'],
3738
cna: ['packages/create-next-app', 'test/integration/create-next-app'],
3839
'next-codemod': ['packages/next-codemod'],
3940
'next-swc': [
@@ -94,15 +95,20 @@ async function main() {
9495
)
9596
}
9697
const execArgIndex = process.argv.indexOf('--exec')
98+
const listChangedDirectories = process.argv.includes(
99+
'--listChangedDirectories'
100+
)
97101

98-
if (execArgIndex < 0) {
99-
throw new Error('no "--exec" flag provided')
102+
if (execArgIndex < 0 && !listChangedDirectories) {
103+
throw new Error(
104+
'Invalid: must provide either "--exec" or "--listChangedDirectories" flag'
105+
)
100106
}
101107
let hasMatchingChange = false
102108
const changeItems = CHANGE_ITEM_GROUPS[type]
103109
const execArgs = process.argv.slice(execArgIndex + 1)
104110

105-
if (execArgs.length < 1) {
111+
if (execArgs.length < 1 && !listChangedDirectories) {
106112
throw new Error('Missing exec arguments after "--exec"')
107113
}
108114

@@ -114,6 +120,7 @@ async function main() {
114120
)
115121
}
116122
let changedFilesCount = 0
123+
let changedDirectories = []
117124

118125
// always run for canary if flag is enabled
119126
if (alwaysCanary && branchName === 'canary') {
@@ -130,7 +137,13 @@ async function main() {
130137
// if --not flag is provided we execute for any file changed
131138
// not included in the change items otherwise we only execute
132139
// if a change item is changed
133-
const matchesItem = changeItems.some((item) => file.startsWith(item))
140+
const matchesItem = changeItems.some((item) => {
141+
const found = file.startsWith(item)
142+
if (found) {
143+
changedDirectories.push(item)
144+
}
145+
return found
146+
})
134147

135148
if (!matchesItem && isNegated) {
136149
hasMatchingChange = true
@@ -151,6 +164,10 @@ async function main() {
151164
}
152165

153166
if (hasMatchingChange) {
167+
if (listChangedDirectories) {
168+
console.log(changedDirectories.join('\n'))
169+
return
170+
}
154171
const cmd = spawn(execArgs[0], execArgs.slice(1))
155172
cmd.stdout.pipe(process.stdout)
156173
cmd.stderr.pipe(process.stderr)

0 commit comments

Comments
 (0)