diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..76add87 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +dist \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9994407..8fa1e26 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,36 +10,34 @@ on: pull_request: branches: [ "main" ] +env: + REGISTRY: ghcr.io + IMAGE_NAME: home + IMAGE_NAME_FULL: ghcr.io/nbtca/home + jobs: - build-and-deploy: + build: runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v3 + # 切换分支 + - name: Checkout + uses: actions/checkout@master - - name: Set up Node - uses: actions/setup-node@v4 - with: - node-version: 20 + - name: Login DockerHub + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - - uses: pnpm/action-setup@v3 - with: - version: 8 - - - name: Install Dependent Node Packages - run: pnpm install - - - - name: build - run: pnpm run build --base ${{ github.event.repository.name }} - - - name: Deploy # 部署 - uses: JamesIves/github-pages-deploy-action@v4.5.0 - with: - branch: gh-pages # 部署后提交到那个分支 - token: ${{ secrets.GITHUB_TOKEN }} - folder: dist - clean: true - single-commit: true - git-config-email: github-actions[bot]@users.noreply.github.com - git-config-name: github-actions[bot] \ No newline at end of file + - name: Build and Push to DockerHub + uses: docker/build-push-action@v3 + with: + context: . + file: ./Dockerfile + no-cache: true + # target: deploy + push: true + platforms: linux/amd64 + tags: | + ${{ env.IMAGE_NAME_FULL }}:latest diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..77084ec --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM node:20-slim AS base +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable +COPY . /app +WORKDIR /app + +FROM base AS build +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile +RUN pnpm run build + +FROM nginx:alpine as deploy +COPY nginx.conf /etc/nginx/nginx.conf +COPY --from=build /app/dist /usr/share/nginx/html diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..7eb11e5 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,31 @@ +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + server { + listen 80; + server_name _; + + root /usr/share/nginx/html; + index index.html index.htm; + include /etc/nginx/mime.types; + + gzip on; + gzip_min_length 1000; + gzip_proxied expired no-cache no-store private auth; + gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript; + + error_page 404 /404.html; + location = /404.html { + root /usr/share/nginx/html; + internal; + } + + location / { + try_files $uri $uri/index.html =404; + } + } +} \ No newline at end of file