From f62f76015fd70d65a2ad801daafeef01e0f5840b Mon Sep 17 00:00:00 2001 From: Qing <7880675+devrsi0n@users.noreply.github.com> Date: Sat, 1 Oct 2022 19:49:34 +0800 Subject: [PATCH] fix(docker): remove unneeded build variables --- .github/workflows/docker.yml | 30 +++++++--------- Dockerfile | 30 +++------------- apps/main/src/blocks/analytics/index.ts | 1 + apps/main/src/pages/analytics/[domain].tsx | 9 ++++- apps/main/src/pages/blog/[...slug].tsx | 11 ++++-- apps/main/src/pages/docs/[[...slug]].tsx | 11 ++++-- apps/main/src/pages/play.tsx | 11 +++++- apps/main/src/pages/theme/[domain].tsx | 11 ++++-- .../src/pages/widget/comment/[pageURL].tsx | 8 ++++- .../widget/comment/timeline/[commentId].tsx | 6 ++++ docker-compose.yml | 35 ++++++++++--------- package.json | 4 +-- 12 files changed, 93 insertions(+), 74 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index fa3298b60..f8bbd5ece 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -16,6 +16,12 @@ jobs: - name: Check out the repo uses: actions/checkout@v3 + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Extract meta id: builder-meta uses: docker/metadata-action@v4 @@ -41,20 +47,10 @@ jobs: push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.builder-meta.outputs.tags }} labels: ${{ steps.builder-meta.outputs.labels }} - build-args: - NEXT_PUBLIC_HASURA_HTTP_ORIGIN: ${{ secrets.NEXT_PUBLIC_HASURA_HTTP_ORIGIN }} - NEXT_PUBLIC_HASURA_WS_ORIGIN: ${{ secrets.NEXT_PUBLIC_HASURA_HTTP_ORIGIN }} - NEXT_PUBLIC_APP_URL: ${{ secrets.NEXT_PUBLIC_APP_URL }} - NEXT_PUBLIC_VAPID: ${{ secrets.NEXT_PUBLIC_VAPID }} - NEXT_PUBLIC_ANALYTICS_DOMAIN: ${{ secrets.NEXT_PUBLIC_ANALYTICS_DOMAIN }} - NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }} - HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET }} - HASURA_EVENT_SECRET: ${{ secrets.HASURA_EVENT_SECRET }} - HASH_ALGORITHM: ${{ secrets.HASH_ALGORITHM }} - NEXTAUTH_SECRET: ${{ secrets.NEXTAUTH_SECRET }} - PRIVATE_VAPID: ${{ secrets.PRIVATE_VAPID }} - EMAIL_API_KEY: ${{ secrets.EMAIL_API_KEY }} - GITHUB_CLIENT_ID: ${{ secrets.GITHUB_CLIENT_ID }} - GITHUB_CLIENT_SECRET: ${{ secrets.GITHUB_CLIENT_SECRET }} - TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }} - TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }} + cache-from: type=registry,ref=devrsi0n/chirpy:buildcache + cache-to: type=registry,ref=devrsi0n/chirpy:buildcache,mode=max + # Don't put any sensitive key to build-args, as they'll be exposed to build history + build-args: | + NEXT_PUBLIC_APP_URL=${{ secrets.NEXT_PUBLIC_APP_URL }} + NEXT_PUBLIC_ANALYTICS_DOMAIN=${{ secrets.NEXT_PUBLIC_ANALYTICS_DOMAIN }} + NEXTAUTH_URL=${{ secrets.NEXTAUTH_URL }} diff --git a/Dockerfile b/Dockerfile index c59281060..38472c679 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,35 +1,13 @@ +# syntax=docker/dockerfile:1 + FROM node:16-slim AS base WORKDIR /app -ARG NEXT_PUBLIC_HASURA_HTTP_ORIGIN -ENV NEXT_PUBLIC_HASURA_HTTP_ORIGIN=${NEXT_PUBLIC_HASURA_HTTP_ORIGIN} -ARG NEXT_PUBLIC_HASURA_WS_ORIGIN -ENV NEXT_PUBLIC_HASURA_WS_ORIGIN=${NEXT_PUBLIC_HASURA_WS_ORIGIN} ARG NEXT_PUBLIC_APP_URL ENV NEXT_PUBLIC_APP_URL=${NEXT_PUBLIC_APP_URL} -ARG NEXT_PUBLIC_VAPID -ENV NEXT_PUBLIC_VAPID=${NEXT_PUBLIC_VAPID} -ARG NEXT_PUBLIC_ANALYTICS_DOMAIN -ENV NEXT_PUBLIC_ANALYTICS_DOMAIN=${NEXT_PUBLIC_ANALYTICS_DOMAIN} ARG NEXTAUTH_URL ENV NEXTAUTH_URL=${NEXTAUTH_URL} -ARG HASURA_ADMIN_SECRET -ENV HASURA_ADMIN_SECRET=${HASURA_ADMIN_SECRET} -ARG HASURA_EVENT_SECRET -ENV HASURA_EVENT_SECRET=${HASURA_EVENT_SECRET} -ARG HASH_ALGORITHM -ENV HASH_ALGORITHM=${HASH_ALGORITHM} -ARG NEXTAUTH_SECRET -ENV NEXTAUTH_SECRET=${NEXTAUTH_SECRET} -ARG PRIVATE_VAPID -ENV PRIVATE_VAPID=${PRIVATE_VAPID} -ARG EMAIL_API_KEY -ENV EMAIL_API_KEY=${EMAIL_API_KEY} -ARG GITHUB_CLIENT_ID -ENV GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID} -ARG GITHUB_CLIENT_SECRET -ENV GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET} -ARG TWITTER_CONSUMER_KEY -ENV TWITTER_CONSUMER_KEY=${TWITTER_CONSUMER_KEY} +ARG NEXT_PUBLIC_ANALYTICS_DOMAIN +ENV NEXT_PUBLIC_ANALYTICS_DOMAIN=${NEXT_PUBLIC_ANALYTICS_DOMAIN} # Disable telemetry ENV NEXT_TELEMETRY_DISABLED=1 ENV DOCKER=true diff --git a/apps/main/src/blocks/analytics/index.ts b/apps/main/src/blocks/analytics/index.ts index 882529757..1e944eca9 100644 --- a/apps/main/src/blocks/analytics/index.ts +++ b/apps/main/src/blocks/analytics/index.ts @@ -1,5 +1,6 @@ import dynamic from 'next/dynamic'; +// TODO: Make it SSR friendly const AnalyticsBlock = dynamic( () => import(/* webpackChunkName: "analytics-block"*/ './analytics'), { diff --git a/apps/main/src/pages/analytics/[domain].tsx b/apps/main/src/pages/analytics/[domain].tsx index 794e178e9..2ce361dea 100644 --- a/apps/main/src/pages/analytics/[domain].tsx +++ b/apps/main/src/pages/analytics/[domain].tsx @@ -46,9 +46,16 @@ type PathParams = { }; export const getStaticPaths: GetStaticPaths = async () => { + if (process.env.DOCKER) { + return { + paths: [], + fallback: 'blocking', + }; + } + // TODO: only generated a subset of analytics pages const paths = await getAllProjectStaticPathsByDomain(); - return { paths, fallback: true }; + return { paths, fallback: 'blocking' }; }; export const getStaticProps: GetStaticProps< diff --git a/apps/main/src/pages/blog/[...slug].tsx b/apps/main/src/pages/blog/[...slug].tsx index 1c6b94d7e..6063d247a 100644 --- a/apps/main/src/pages/blog/[...slug].tsx +++ b/apps/main/src/pages/blog/[...slug].tsx @@ -48,16 +48,21 @@ type PathParam = { }; export const getStaticPaths: GetStaticPaths = async () => { + if (process.env.DOCKER) { + return { + paths: [], + fallback: 'blocking', + }; + } const fileStructures = await getAllFileStructures(CONTAINER_FOLDER); - const payload = { + return { paths: fileStructures.map((f) => ({ params: { slug: [...(f.ancestors || []), f.slug].filter(Boolean), }, })), - fallback: true, + fallback: 'blocking', }; - return payload; }; export const getStaticProps: GetStaticProps< diff --git a/apps/main/src/pages/docs/[[...slug]].tsx b/apps/main/src/pages/docs/[[...slug]].tsx index 5bc9c31d5..24dbbcc95 100644 --- a/apps/main/src/pages/docs/[[...slug]].tsx +++ b/apps/main/src/pages/docs/[[...slug]].tsx @@ -53,16 +53,21 @@ type PathParam = { }; export const getStaticPaths: GetStaticPaths = async () => { + if (process.env.DOCKER) { + return { + paths: [], + fallback: 'blocking', + }; + } const fileStructures = await getAllFileStructures(CONTAINER_FOLDER); - const payload = { + return { paths: fileStructures.map((f) => ({ params: { slug: [...(f.ancestors || []), f.slug].filter(Boolean), }, })), - fallback: true, + fallback: 'blocking', }; - return payload; }; export const getStaticProps: GetStaticProps = async ({ diff --git a/apps/main/src/pages/play.tsx b/apps/main/src/pages/play.tsx index f948890ac..95c421562 100644 --- a/apps/main/src/pages/play.tsx +++ b/apps/main/src/pages/play.tsx @@ -22,7 +22,11 @@ export default function PlayGround(): JSX.Element { setShowAlert(false)} hideDismissButton /> @@ -40,6 +44,11 @@ type StaticProps = { export const getStaticProps: GetStaticProps = async (): Promise< GetStaticPropsResult > => { + if (process.env.DOCKER) { + return { + props: {}, + }; + } const beforeDate = cpDayjs() .subtract(1, isENVDev ? 'hour' : 'day') .toISOString(); diff --git a/apps/main/src/pages/theme/[domain].tsx b/apps/main/src/pages/theme/[domain].tsx index c0b4ad2f5..981d6794c 100644 --- a/apps/main/src/pages/theme/[domain].tsx +++ b/apps/main/src/pages/theme/[domain].tsx @@ -46,11 +46,16 @@ type PathParams = { }; export const getStaticPaths: GetStaticPaths = async () => { + if (process.env.DOCKER) { + return { + paths: [], + fallback: 'blocking', + }; + } + // TODO: only generated a subset of theme pages to improve build perf const paths = await getAllProjectStaticPathsByDomain(); - // We'll pre-render only these paths at build time. - // { fallback: false } means other routes should 404. - return { paths, fallback: true }; + return { paths, fallback: 'blocking' }; }; type StaticProps = { diff --git a/apps/main/src/pages/widget/comment/[pageURL].tsx b/apps/main/src/pages/widget/comment/[pageURL].tsx index 0d467612b..bbcc20e7a 100644 --- a/apps/main/src/pages/widget/comment/[pageURL].tsx +++ b/apps/main/src/pages/widget/comment/[pageURL].tsx @@ -87,6 +87,12 @@ const client = getAdminGqlClient(); // Get all project then prerender all their page comments export const getStaticPaths: GetStaticPaths = async () => { + if (process.env.DOCKER) { + return { + paths: [], + fallback: 'blocking', + }; + } const freshPages = await query( FreshPagesDocument, { @@ -106,7 +112,7 @@ export const getStaticPaths: GetStaticPaths = async () => { // We'll pre-render only these paths at build time. // { fallback: false } means other routes should 404. - return { paths, fallback: true }; + return { paths, fallback: 'blocking' }; }; type StaticProps = PathParams & diff --git a/apps/main/src/pages/widget/comment/timeline/[commentId].tsx b/apps/main/src/pages/widget/comment/timeline/[commentId].tsx index de069e139..e988ab804 100644 --- a/apps/main/src/pages/widget/comment/timeline/[commentId].tsx +++ b/apps/main/src/pages/widget/comment/timeline/[commentId].tsx @@ -84,6 +84,12 @@ type StaticProps = PathParams & }; export const getStaticPaths: GetStaticPaths = async () => { + if (process.env.DOCKER) { + return { + paths: [], + fallback: 'blocking', + }; + } const comments = await query( CommentsDocument, { diff --git a/docker-compose.yml b/docker-compose.yml index 199bfdb05..a4832796d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,6 @@ version: '3' +# Test docker build services: chirpy-main: container_name: chirpy-main @@ -7,26 +8,28 @@ services: build: context: ./ dockerfile: Dockerfile + # Don't put any sensitive key to build-args, as they'll be exposed to build history, https://github.com/docker/build-push-action/blob/master/docs/advanced/secrets.md args: - NEXT_PUBLIC_HASURA_HTTP_ORIGIN: http://host.docker.internal:8080/v1/graphql - NEXT_PUBLIC_HASURA_WS_ORIGIN: ws://host.docker.internal:8080/v1/graphql - NEXT_PUBLIC_APP_URL: ${NEXT_PUBLIC_APP_URL} - NEXT_PUBLIC_VAPID: ${NEXT_PUBLIC_VAPID} + NEXT_PUBLIC_APP_URL: http://localhost:3000 NEXT_PUBLIC_ANALYTICS_DOMAIN: ${NEXT_PUBLIC_ANALYTICS_DOMAIN} - NEXTAUTH_URL: ${NEXTAUTH_URL} - HASURA_ADMIN_SECRET: ${HASURA_ADMIN_SECRET} - HASURA_EVENT_SECRET: ${HASURA_EVENT_SECRET} - HASH_ALGORITHM: ${HASH_ALGORITHM} - NEXTAUTH_SECRET: ${NEXTAUTH_SECRET} - PRIVATE_VAPID: ${PRIVATE_VAPID} - EMAIL_API_KEY: ${EMAIL_API_KEY} - GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID} - GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET} - TWITTER_CONSUMER_KEY: ${TWITTER_CONSUMER_KEY} - TWITTER_CONSUMER_SECRET: ${TWITTER_CONSUMER_SECRET} + NEXTAUTH_URL: http://localhost:3000 environment: NEXT_PUBLIC_HASURA_HTTP_ORIGIN: http://127.0.0.1:8080/v1/graphql NEXT_PUBLIC_HASURA_WS_ORIGIN: ws://127.0.0.1:8080/v1/graphql + NEXT_PUBLIC_APP_URL: http://localhost:3000 + NEXT_PUBLIC_VAPID: ${NEXT_PUBLIC_VAPID} + NEXT_PUBLIC_ANALYTICS_DOMAIN: ${NEXT_PUBLIC_ANALYTICS_DOMAIN} + NEXTAUTH_URL: http://localhost:3000 + HASURA_ADMIN_SECRET: ${HASURA_ADMIN_SECRET} + HASURA_EVENT_SECRET: ${HASURA_EVENT_SECRET} + HASH_ALGORITHM: ${HASH_ALGORITHM} + NEXTAUTH_SECRET: ${NEXTAUTH_SECRET} + PRIVATE_VAPID: ${PRIVATE_VAPID} + EMAIL_API_KEY: ${EMAIL_API_KEY} + GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID} + GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET} + TWITTER_CONSUMER_KEY: ${TWITTER_CONSUMER_KEY} + TWITTER_CONSUMER_SECRET: ${TWITTER_CONSUMER_SECRET} restart: always ports: - 3000:3000 @@ -35,8 +38,6 @@ services: # networks: # - my_network - # Add more containers below (nginx, postgres, etc.) - # Define a network, which allows containers to communicate # with each other, by using their container name as a hostname # networks: diff --git a/package.json b/package.json index c69f37a80..a1cf553a1 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "changeset": "changeset", "version-packages": "changeset version", "release": "turbo run build --filter=@chirpy-dev/main-app... && changeset publish", - "build:local": "docker system prune -af && dotenv -e ./apps/main/.env.local docker compose build", - "start:local": "dotenv -e ./apps/main/.env.local docker compose up", + "build:docker:local": "docker system prune -af && dotenv -e ./apps/main/.env.local docker compose build", + "start:docker:local": "dotenv -e ./apps/main/.env.local docker compose up", "debug": "docker run -it --entrypoint=/bin/bash chirpy-chirpy-main -i" }, "engines": {