From 0394fdcfd572fd0b0bc2275e7a3e26b7b91c861b Mon Sep 17 00:00:00 2001 From: Duyet Le Date: Sun, 19 Nov 2023 23:45:50 +0700 Subject: [PATCH] feat: docker build --- .dockerignore | 8 ++++ .github/workflows/ci.yml | 52 ++++++++++++++++++++++++ Dockerfile | 54 +++++++++++++++++++++++++ app/page.tsx | 3 ++ components/data-table/data-table.cy.tsx | 2 +- components/reload-button.cy.tsx | 13 ------ next.config.js | 4 +- 7 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 components/reload-button.cy.tsx diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..4c7db2f3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +Dockerfile +.dockerignore +node_modules +npm-debug.log +README.md +.next +docker +.git \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 285fa621..503b75f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,3 +24,55 @@ jobs: uses: ./.github/workflows/base.yml with: job-type: test + + build-docker: + runs-on: ubuntu-latest + env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + permissions: + contents: read + packages: write + services: + clickhouse: + image: clickhouse/clickhouse-server:23.10 + ports: + - 8123:8123 + - 9000:9000 + options: >- + --health-cmd "wget --no-verbose --tries=1 --spider http://localhost:8123/?query=SELECT%201 || exit 1" + --health-interval 30s + --health-timeout 10s + --health-retries 5 + --health-start-period 30s + steps: + - name: Checkout + 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@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + push: ${{ github.ref == 'refs/heads/main' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..eaefcc5a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,54 @@ +FROM node:18-alpine AS base + +# Install dependencies only when needed +FROM base AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app + +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . +RUN yarn build + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV production +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Set the correct permission for prerender cache +RUN mkdir .next +RUN chown nextjs:nodejs .next + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 + +ENV PORT 3000 +ENV HOSTNAME "0.0.0.0" + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +CMD ["node", "server.js"] \ No newline at end of file diff --git a/app/page.tsx b/app/page.tsx index 83b036e9..91a68fb8 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -5,6 +5,9 @@ import { ChartQueryCountByUser } from '@/components/charts/query-count-by-user' import ChartTopTableSize from '@/components/charts/top-table-size' import { IntervalSelect } from '@/components/interval-select' +export const dynamic = 'force-dynamic' +export const revalidate = 5 + export default function Home() { return ( diff --git a/components/data-table/data-table.cy.tsx b/components/data-table/data-table.cy.tsx index 5d730276..3d9068ae 100644 --- a/components/data-table/data-table.cy.tsx +++ b/components/data-table/data-table.cy.tsx @@ -1,6 +1,6 @@ import React from 'react' -import { QueryConfig } from '@/app/[name]/clickhouse-queries' +import type { QueryConfig } from '@/lib/types/query-config' import { DataTable } from './data-table' diff --git a/components/reload-button.cy.tsx b/components/reload-button.cy.tsx deleted file mode 100644 index 91054764..00000000 --- a/components/reload-button.cy.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import React from 'react' - -import { ReloadButton } from './reload-button' - -describe('', () => { - context('mocking router', () => { - beforeEach(() => {}) - - it('renders', () => { - cy.mount() - }) - }) -}) diff --git a/next.config.js b/next.config.js index 767719fc..5c6d7792 100644 --- a/next.config.js +++ b/next.config.js @@ -1,4 +1,6 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {} +const nextConfig = { + output: 'standalone', +} module.exports = nextConfig