Skip to content

Commit

Permalink
feat: build hooks in Cloudflare CI (#40)
Browse files Browse the repository at this point in the history
* feat: build hooks in Cloudflare Ci

* chore: update api name

* chore: up

---------

Co-authored-by: Sébastien Chopin <seb@nuxt.com>
  • Loading branch information
farnabaz and atinux authored Apr 3, 2024
1 parent d4fca40 commit 2e8c99e
Showing 1 changed file with 31 additions and 2 deletions.
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

0 comments on commit 2e8c99e

Please sign in to comment.