From 36d53cdec395a0dd576c5d2093977f19237ba2c2 Mon Sep 17 00:00:00 2001 From: Wei He Date: Sat, 23 Nov 2024 13:25:55 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20Add=20docker=20deployment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/rude-beds-push.md | 5 +++ .dockerignore | 36 +++++++++++++++++++++ .env.example | 3 ++ .github/workflows/publish.yml | 59 +++++++++++++++++++++++++++++++++++ Dockerfile | 24 ++++++++++++++ docker-compose.yml | 9 ++++++ pages/_app.tsx | 2 +- 7 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 .changeset/rude-beds-push.md create mode 100644 .dockerignore create mode 100644 .github/workflows/publish.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.changeset/rude-beds-push.md b/.changeset/rude-beds-push.md new file mode 100644 index 00000000..773b50cc --- /dev/null +++ b/.changeset/rude-beds-push.md @@ -0,0 +1,5 @@ +--- +"socialify": minor +--- + +Add docker deployment diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..f0564fb8 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,36 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local +*.tsbuildinfo + +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.env +/.next +.swc + +.idea +.vercel + +/public/*.wasm + +.dockerignore +Dockerfile +.* \ No newline at end of file diff --git a/.env.example b/.env.example index 6bbd5966..383fd0cf 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,6 @@ +# GitHub token GITHUB_TOKEN= +# API URL (Use public url in edge deployments, and localhost url in non-edge deployments) PROJECT_URL=http://localhost:3000 +# Google Tag Manager ID GTM_ID= diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..08278dda --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,59 @@ +name: Publish Docker Image + +on: + workflow_dispatch: + push: + branches: + - "master" + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=schedule + type=ref,event=branch + type=ref,event=tag + type=ref,event=pr + # set latest tag for default branch + type=raw,value=latest,enable={{is_default_branch}} + + - name: Build and push Docker image + id: push + uses: docker/build-push-action@v6 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v1 + with: + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..7acda0e9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +# Source: https://github.com/vercel/next.js/blob/2161d8c012dcd98eb8690814bd275d56c45bf00a/examples/with-docker/Dockerfile + +FROM node:18-alpine AS base + +ENV GITHUB_TOKEN= +ENV PROJECT_URL=http://localhost:3000 +ENV GTM_ID= +ENV PORT=3000 +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 + +WORKDIR /app + +COPY package.json yarn.lock* ./ +COPY public ./public/ +RUN yarn --frozen-lockfile + +COPY . . + +RUN yarn build + +CMD ["yarn", "start"] + +EXPOSE 3000 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..1cb76e0f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + socialify: + restart: unless-stopped + build: + context: . + ports: + - "3000:3000" + env_file: + - .env diff --git a/pages/_app.tsx b/pages/_app.tsx index 08779c85..f2eadcb9 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -59,7 +59,7 @@ export default class MyApp extends App { />