Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: build hooks in Cloudflare CI #40

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ export default defineNuxtModule<ModuleOptions>({
remoteArg = (remoteArg === '' ? 'true' : remoteArg)
const runtimeConfig = nuxt.options.runtimeConfig
const hub = runtimeConfig.hub = defu(runtimeConfig.hub || {}, options, {
url: process.env.NUXT_HUB_URL || 'https://admin.hub.nuxt.com',
projectKey: process.env.NUXT_HUB_PROJECT_KEY || '',
// Self-hosted project
projectUrl: process.env.NUXT_HUB_PROJECT_URL || '',
projectSecretKey: process.env.NUXT_HUB_PROJECT_SECRET_KEY || '',
// Deployed on NuxtHub
url: process.env.NUXT_HUB_URL || 'https://admin.hub.nuxt.com',
projectKey: process.env.NUXT_HUB_PROJECT_KEY || '',
userToken: process.env.NUXT_HUB_USER_TOKEN || '',
// Remote storage
remote: remoteArg || process.env.NUXT_HUB_REMOTE,
// Other options
version,
env: process.env.NUXT_HUB_ENV || 'production',
openapi: nuxt.options.nitro.experimental?.openAPI === true
Expand Down Expand Up @@ -114,6 +118,31 @@ export default defineNuxtModule<ModuleOptions>({
return
}

// Within CF Pages CI/CD to notice NuxtHub about the build and hub config
if (!nuxt.options.dev && process.env.CF_PAGES && process.env.NUXT_HUB_PROJECT_DEPLOY_TOKEN && process.env.NUXT_HUB_PROJECT_KEY && process.env.NUXT_HUB_ENV) {
nuxt.hook('build:before', async () => {
await $fetch(`/api/projects/${process.env.NUXT_HUB_PROJECT_KEY}/build/${process.env.NUXT_HUB_ENV}/before`, {
baseURL: hub.url,
method: 'POST',
headers: {
authorization: `Bearer ${process.env.NUXT_HUB_PROJECT_DEPLOY_TOKEN}`
},
body: {},
}).catch(() => {})
})

nuxt.hook('build:done', async () => {
await $fetch(`/api/projects/${process.env.NUXT_HUB_PROJECT_KEY}/build/${process.env.NUXT_HUB_ENV}/done`, {
baseURL: hub.url,
method: 'POST',
headers: {
authorization: `Bearer ${process.env.NUXT_HUB_PROJECT_DEPLOY_TOKEN}`
},
body: {},
}).catch(() => {})
})
}

if (hub.remote) {
// Can either use projectKey or projectUrl
if (hub.projectKey && hub.projectUrl) {
Expand Down
Loading