-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: deploy some apps preview only when PR has label
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const fetch = require('node-fetch') | ||
|
||
const owner = process.env.VERCEL_GIT_REPO_OWNER | ||
const repo = process.env.VERCEL_GIT_REPO_SLUG | ||
const pullRequestId = process.env.VERCEL_GIT_PULL_REQUEST_ID | ||
const commitRef = process.env.VERCEL_GIT_COMMIT_REF | ||
const githubToken = process.env.GITHUB_TOKEN | ||
|
||
const APP_ARGV = '--app=' | ||
const appName = (() => { | ||
const argv = process.argv.find((arg) => arg.startsWith(APP_ARGV)) | ||
return argv ? argv.slice(APP_ARGV.length) : undefined | ||
})() | ||
|
||
const PREVIEW_IGNORE_BRANCHES = ['main', 'configuration', 'release-please--branches--main'] | ||
|
||
async function shouldSkipBuild() { | ||
if (PREVIEW_IGNORE_BRANCHES.includes(commitRef)) { | ||
console.log(`Skipping build for branch ${commitRef}.`) | ||
return true | ||
} | ||
|
||
if (!pullRequestId || !githubToken || !appName) { | ||
console.log('No PR ID or GitHub token or appName found. Proceeding with build.') | ||
return false | ||
} | ||
|
||
const url = `https://api.github.com/repos/${owner}/${repo}/issues/${pullRequestId}/labels` | ||
|
||
try { | ||
const response = await fetch(url, { | ||
headers: { | ||
Authorization: `Bearer ${githubToken}`, | ||
Accept: 'application/vnd.github.v3+json', | ||
}, | ||
}) | ||
|
||
if (!response.ok) { | ||
console.error('Failed to fetch PR labels:', response.statusText) | ||
return false // Proceed with the build in case of an error | ||
} | ||
|
||
const labels = await response.json() | ||
console.log( | ||
'PR Labels:', | ||
labels.map((label) => label.name), | ||
) | ||
const hasAppLabel = labels.some((label) => label.name === appName) | ||
|
||
// Skip the build if the PR doesn't have the app label | ||
return !hasAppLabel | ||
} catch (error) { | ||
console.error('Error fetching PR labels:', error) | ||
return false // Proceed with the build in case of an error | ||
} | ||
} | ||
|
||
shouldSkipBuild().then((skip) => { | ||
if (skip) { | ||
console.log('Skipping build.') | ||
process.exit(0) | ||
} else { | ||
console.log('Proceeding with build.') | ||
process.exit(1) | ||
} | ||
}) |