chore: support docker

This commit is contained in:
Ou
2024-10-24 02:17:47 +08:00
parent e3c1053eca
commit aa0c6138c2
6 changed files with 124 additions and 20 deletions
+12
View File
@@ -0,0 +1,12 @@
node_modules/
dist/
.vercel
.output
.vinxi
.cache
.data
.wrangler
.env
.env.*
dev-dist
*.tsbuildinfo
+49
View File
@@ -0,0 +1,49 @@
name: Publish Docker image
on:
push:
tags:
- 'v*'
workflow_dispatch:
jobs:
build-docker:
name: Push Docker image to multiple registries
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
platforms: |
linux/amd64
linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
+13
View File
@@ -0,0 +1,13 @@
FROM node:20.12.2-alpine as builder
WORKDIR /usr/src
COPY . .
RUN corepack enable
RUN pnpm install
RUN pnpm run build
FROM node:20.12.2-alpine
WORKDIR /usr/app
COPY --from=builder /usr/src/dist/output ./output
ENV HOST=0.0.0.0 PORT=4444 NODE_ENV=production
EXPOSE $PORT
CMD ["node", "output/server/index.mjs"]
+14
View File
@@ -0,0 +1,14 @@
version: '3'
services:
newsnow:
image: ourongxing/newsnow:latest
container_name: newsnow
restart: always
ports:
- '4444:4444'
environment:
- G_CLIENT_ID=
- G_CLIENT_SECRET=
- JWT_SECRET=
- INIT_TABLE=true
+1 -1
View File
@@ -16,7 +16,7 @@
"build": "vite build",
"lint": "eslint",
"favicon": "tsx ./scripts/favicon.ts",
"start": "PORT=4444 node --env-file .env.server dist/output/server/index.mjs",
"start": "node --env-file .env.server dist/output/server/index.mjs",
"preview": "CF_PAGES=1 pnpm run build && wrangler pages dev",
"deploy": "CF_PAGES=1 pnpm run build && wrangler pages deploy",
"typecheck": "tsc --noEmit -p tsconfig.node.json && tsc --noEmit -p tsconfig.app.json",
+35 -19
View File
@@ -12,6 +12,7 @@ import { VitePWA } from "vite-plugin-pwa"
import { projectDir } from "./shared/dir"
const isCF = process.env.CF_PAGES
const isVercel = process.env.VERCEL
dotenv.config({
path: join(projectDir, ".env.server"),
@@ -57,6 +58,39 @@ const pwaOption: Partial<VitePWAOptions> = {
}
const nitroOption: Parameters<typeof nitro>[0] = {
experimental: {
database: true,
},
sourceMap: false,
database: {
default: {
connector: "sqlite",
},
},
alias: {
"@shared": join(projectDir, "shared"),
"#": join(projectDir, "server"),
},
preset: "node-server",
}
if (isVercel) {
nitroOption.preset = "vercel-edge"
// You can use other online database, do it yourself. For more info: https://db0.unjs.io/connectors
nitroOption.database = undefined
} else if (isCF) {
nitroOption.preset = "cloudflare-workers"
nitroOption.database = {
default: {
connector: "cloudflare-d1",
options: {
bindingName: "NEWSNOW_DB",
},
},
}
}
export default defineConfig({
define: {
__G_CLIENT_ID__: `"${process.env.G_CLIENT_ID}"`,
@@ -70,24 +104,6 @@ export default defineConfig({
unocss(),
react(),
VitePWA(pwaOption),
nitro({
experimental: {
database: true,
},
sourceMap: false,
database: {
default: {
connector: isCF ? "cloudflare-d1" : "sqlite",
options: {
bindingName: "NEWSNOW_DB",
},
},
},
alias: {
"@shared": join(projectDir, "shared"),
"#": join(projectDir, "server"),
},
preset: isCF ? "cloudflare-pages" : "node-server",
}),
nitro(nitroOption),
],
})