From e8648fb7a80bdea60ac1bf6fd488cdbde45944bb Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 25 Feb 2025 10:41:51 -0500 Subject: [PATCH] ci: build --- .dockerignore | 40 ++++++++++++++ .github/workflows/docker-build.yml | 89 ++++++++++++++++++++++++++++++ Dockerfile | 44 +++++++++++++++ next.config.js | 1 + 4 files changed, 174 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker-build.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1d65dfd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,40 @@ +# Dependencies +node_modules +npm-debug.log +yarn-debug.log +yarn-error.log + +# Next.js build output +.next +out + +# Git and GitHub related +.git +.github +.gitignore + +# Environment variables +.env +.env.local +.env.development.local +.env.test.local +.env.production.local + +# Editor directories and files +.idea +.vscode +*.swp +*.swo + +# OS files +.DS_Store +Thumbs.db + +# Testing +coverage +.nyc_output + +# Misc +README.md +LICENSE +*.md \ No newline at end of file diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 0000000..a235341 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,89 @@ +name: Build and Push Docker Image + +on: + push: + branches: [ main ] + paths-ignore: + - '**.md' + pull_request: + branches: [ main ] + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '18' + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Run tests + run: npm test || true + + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Container Registry + uses: docker/login-action@v2 + with: + registry: ${{ github.server_url }}/${{ github.repository }}/packages + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ github.server_url }}/${{ github.repository }}/personal-website + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=ref,event=branch + type=ref,event=pr + type=sha + + - name: Determine version + id: semver + uses: mathieudutour/github-tag-action@v6.1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + release_branches: main + default_bump: patch + dry_run: true + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: | + ${{ github.server_url }}/${{ github.repository }}/personal-website:latest + ${{ github.server_url }}/${{ github.repository }}/personal-website:${{ steps.semver.outputs.new_tag }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Create GitHub Release + if: github.event_name != 'pull_request' + uses: mathieudutour/github-tag-action@v6.1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + release_branches: main + default_bump: patch + create_annotated_tag: true + tag_prefix: v \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..97561d6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,44 @@ +# Build stage +FROM node:18-alpine AS builder +WORKDIR /app + +# Copy package files and install dependencies +COPY package*.json ./ +RUN npm ci + +# Copy the rest of the application code +COPY . . + +# Build the Next.js application +RUN npm run build + +# Production stage +FROM node:18-alpine AS runner +WORKDIR /app + +ENV NODE_ENV production + +# Create a non-root user to run the application +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +# Copy necessary files from the build stage +COPY --from=builder /app/public ./public +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static + +# Set the correct permissions +RUN chown -R nextjs:nodejs /app + +# Switch to the non-root user +USER nextjs + +# Expose the port the app will run on +EXPOSE 3000 + +# Set the environment variable for the application to listen on all interfaces +ENV PORT 3000 +ENV HOSTNAME "0.0.0.0" + +# Start the application +CMD ["node", "server.js"] \ No newline at end of file diff --git a/next.config.js b/next.config.js index 7143e54..eb62ba1 100644 --- a/next.config.js +++ b/next.config.js @@ -2,6 +2,7 @@ const nextConfig = { reactStrictMode: true, swcMinify: true, + output: 'standalone', experimental: { instrumentationHook: true },