Skip to content

Commit

Permalink
feat(nuxt3): manage env vars on cloud server
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed May 30, 2023
1 parent 4c92e3a commit 687fe63
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/deploy-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ jobs:
contents: ${{ github.event.client_payload.outConfig }}
write-mode: overwrite

- name: prewrite
if: ${{ !(github.event.client_payload.prewrite.content == '') }}
uses: DamianReeves/write-file-action@master
with:
path: ${{ github.event.client_payload.prewrite.file }}
contents: ${{ github.event.client_payload.prewrite.content }}
write-mode: overwrite

- name: prebuild
if: ${{ !(github.event.client_payload.prebuild == '') }}
run: ${{ github.event.client_payload.prebuild }}
working-directory: server/nuxt3
env:
CLOUDFLARE_API_TOKEN: ${{ github.event.client_payload.clitoken }}
NETLIFY_AUTH_TOKEN: ${{ github.event.client_payload.clitoken }}
NETLIFY_SITE_ID: ${{ github.event.client_payload.siteid }}
KV_REST_API_URL: ${{ github.event.client_payload.kvurl }}
KV_REST_API_TOKEN: ${{ github.event.client_payload.kvtoken }}

- name: deploy server
run: ${{ github.event.client_payload.build }}
working-directory: server/nuxt3
Expand Down
5 changes: 5 additions & 0 deletions server/nuxt3/composables/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ export const serverConfig: BasicConfig<ModelConfig> = {
value: '',
require: true,
},
siteid: {
label: 'Worker Name',
value: '',
require: true,
},
},
output: 'Server',
},
Expand Down
2 changes: 1 addition & 1 deletion server/nuxt3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev": "nuxi dev",
"dev:cloudflare": "wrangler dev .output/server/index.mjs --site .output/public --local",
"deploy:cloudflare": "wrangler2 publish",
"deploy:netlify": "netlify deploy",
"deploy:netlify": "netlify deploy --prod",
"deploy:vercel": "wrangler2 publish",
"start": "node .output/server/index.mjs",
"typecheck": "vue-tsc --noEmit",
Expand Down
23 changes: 23 additions & 0 deletions server/nuxt3/server/_api/config.product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import type { OutputConfig, ServerConfig } from '../../composables/config'
export default eventHandler(async (event) => {
try {
const { outConfig } = await readBody<{ outConfig: ServerConfig<OutputConfig> }>(event)
let prebuild = ''
const prewrite = {
file: '',
content: '',
}
if (outConfig.server.select === 'netlify') {
prebuild = `netlify env:set KV_REST_API_URL ${outConfig.kvStorage.config.KV_REST_API_URL} && netlify env:set KV_REST_API_TOKEN ${outConfig.kvStorage.config.KV_REST_API_TOKEN}`
}
else if (outConfig.server.select === 'cloudflare') {
prewrite.file = 'server/nuxt3/wrangler.toml'
prewrite.content = `name = "${outConfig.server.config.siteid}"
send_metrics = false
compatibility_date = "2023-03-04"
main = "./.output/server/index.mjs"
workers_dev = true
[vars]
KV_REST_API_URL=${outConfig.kvStorage.config.KV_REST_API_URL}
KV_REST_API_TOKEN=${outConfig.kvStorage.config.KV_REST_API_TOKEN}`
}

const res = await $fetch(`${process.env.GITHUB_REPO_DISPATCH_URL}`, {
method: 'POST',
headers: {
Expand All @@ -16,6 +37,8 @@ export default eventHandler(async (event) => {
event_type: 'deploy',
client_payload: {
outConfig: JSON.stringify(outConfig, null, 2),
prebuild,
prewrite,
build: `pnpm build:${outConfig.server.select} && pnpm deploy:${outConfig.server.select}`,
clitoken: outConfig.server.config.token,
siteid: outConfig.server.config.siteid,
Expand Down
6 changes: 0 additions & 6 deletions server/nuxt3/wrangler-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,5 @@ main = "./.output/server/index.mjs"
workers_dev = true

[vars]
SUPABASE_URL=
SUPABASE_STORAGE_BUCKET=
SUPABASE_ANON_KEY=
KV_URL=
KV_REST_API_URL=
KV_REST_API_TOKEN=
KV_REST_API_READ_ONLY_TOKEN=

1 comment on commit 687fe63

@vercel
Copy link

@vercel vercel bot commented on 687fe63 May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

star-nexus – ./

star-nexus.vercel.app
star-nexus-larchliu.vercel.app
star-nexus-git-main-larchliu.vercel.app

Please sign in to comment.