From 2b9807bd899d1d5b29d3d5621d19a3f7caace3a5 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Sat, 31 Aug 2024 13:41:16 +0800 Subject: [PATCH 1/2] feat: add Docker support and enhance environment configuration - Introduce .dockerignore for Docker build optimization - Add Dockerfile for containerization setup - Update .env.example to clear Sentry variables and rename HOST to TELEGRAM_HOST - Modify astro.config.mjs to conditionally exclude SSR dependencies in Docker builds - Adjust postinstall script to safely handle missing .git directory - Refactor telegram host retrieval for clarity and consistency --- .dockerignore | 6 ++++++ .env.example | 8 ++++---- Dockerfile | 27 +++++++++++++++++++++++++++ astro.config.mjs | 1 + package.json | 6 +++--- pnpm-lock.yaml | 14 +++++++------- src/lib/telegram/index.js | 2 +- 7 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..9295ed9b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.DS_Store +node_modules +dist +.git +.gitignore +*.md diff --git a/.env.example b/.env.example index 387cd4b6..1fb25d04 100644 --- a/.env.example +++ b/.env.example @@ -17,9 +17,9 @@ HEADER_INJECT=HEADER_INJECT NO_FOLLOW=false NO_INDEX=false -SENTRY_AUTH_TOKEN=SENTRY_AUTH_TOKEN -SENTRY_DSN=SENTRY_DSN -SENTRY_PROJECT=SENTRY_PROJECT +SENTRY_AUTH_TOKEN= +SENTRY_DSN= +SENTRY_PROJECT= -HOST=telegram.dog +TELEGRAM_HOST=telegram.dog STATIC_PROXY="" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..11c0a9d6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM node:lts-alpine AS base + +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable + +WORKDIR /app +COPY package.json pnpm-lock.yaml ./ + +# FROM base AS prod-deps +# RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile + +FROM base AS build-deps +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile + +FROM build-deps AS build +COPY . . +RUN export $(cat .env.example) && export DOCKER=true && pnpm run build + +FROM base AS runtime +# COPY --from=prod-deps /app/node_modules ./node_modules +COPY --from=build /app/dist ./dist + +ENV HOST=0.0.0.0 +ENV PORT=4321 +EXPOSE 4321 +CMD node ./dist/server/entry.mjs diff --git a/astro.config.mjs b/astro.config.mjs index 8baea61a..f2bbbadb 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -48,6 +48,7 @@ export default defineConfig({ ], vite: { ssr: { + noExternal: process.env.DOCKER ? !!process.env.DOCKER : undefined, external: [ ...adapterProvider === 'cloudflare_pages' ? [ diff --git a/package.json b/package.json index a8cbfdbb..83f7d6c4 100644 --- a/package.json +++ b/package.json @@ -12,13 +12,11 @@ "astro": "astro", "lint": "eslint .", "lint:fix": "eslint . --fix", - "postinstall": "simple-git-hooks" + "postinstall": "test -d .git && simple-git-hooks || true" }, "dependencies": { "@astrojs/rss": "^4.0.7", "@sentry/astro": "^8.26.0", - "astro": "^4.14.5", - "astro-seo": "^0.8.4", "cheerio": "1.0.0-rc.12", "dayjs": "^1.11.13", "flourite": "^1.3.0", @@ -35,7 +33,9 @@ "@astrojs/node": "^8.3.3", "@astrojs/vercel": "^7.8.0", "@types/prismjs": "^1.26.4", + "astro": "^4.14.5", "astro-eslint-parser": "^1.0.2", + "astro-seo": "^0.8.4", "autoprefixer": "^10.4.20", "cssnano": "^7.0.5", "eslint": "9.5.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4eb614af..74ca9064 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,12 +14,6 @@ importers: '@sentry/astro': specifier: ^8.26.0 version: 8.26.0(astro@4.14.5(@types/node@22.0.0)(rollup@4.21.0)(typescript@5.5.4)) - astro: - specifier: ^4.14.5 - version: 4.14.5(@types/node@22.0.0)(rollup@4.21.0)(typescript@5.5.4) - astro-seo: - specifier: ^0.8.4 - version: 0.8.4(prettier-plugin-astro@0.14.1)(prettier@3.3.3)(typescript@5.5.4) cheerio: specifier: 1.0.0-rc.12 version: 1.0.0-rc.12 @@ -63,9 +57,15 @@ importers: '@types/prismjs': specifier: ^1.26.4 version: 1.26.4 + astro: + specifier: ^4.14.5 + version: 4.14.5(@types/node@22.0.0)(rollup@4.21.0)(typescript@5.5.4) astro-eslint-parser: specifier: ^1.0.2 version: 1.0.2(typescript@5.5.4) + astro-seo: + specifier: ^0.8.4 + version: 0.8.4(prettier-plugin-astro@0.14.1)(prettier@3.3.3)(typescript@5.5.4) autoprefixer: specifier: ^10.4.20 version: 10.4.20(postcss@8.4.41) @@ -3251,7 +3251,7 @@ packages: hasBin: true mime@3.0.0: - resolution: {integrity: sha512-jSCU7/VB1loIWBZe14aEYHU/+1UMEHoaO7qxCOVJOw9GgH72VAWppxNcjU+x9a2k3GSIBXNKxXQFqRvvZ7vr3A==} + resolution: {integrity: sha1-s3RVDco6DBhEOwyVCmpY8ZMc96c=} engines: {node: '>=10.0.0'} hasBin: true diff --git a/src/lib/telegram/index.js b/src/lib/telegram/index.js index b1271ed1..7904617e 100644 --- a/src/lib/telegram/index.js +++ b/src/lib/telegram/index.js @@ -183,7 +183,7 @@ export async function getChannelInfo(Astro, { before = '', after = '', q = '', t } // Where t.me can also be telegram.me, telegram.dog - const host = getEnv(import.meta.env, Astro, 'HOST') ?? 't.me' + const host = getEnv(import.meta.env, Astro, 'TELEGRAM_HOST') ?? 't.me' const channel = getEnv(import.meta.env, Astro, 'CHANNEL') const staticProxy = getEnv(import.meta.env, Astro, 'STATIC_PROXY') ?? '/static/' From d2b6324aa45f65b525e0f44ee5f7d8dcb37d5ad7 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Sat, 31 Aug 2024 14:01:04 +0800 Subject: [PATCH 2/2] feat: Add Docker image build and push workflow Automate Docker image creation and publishing to GitHub Packages, enhancing deployment efficiency and supply chain security through artifact attestations. --- .github/workflows/docker.yml | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 00000000..69499ab2 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,57 @@ +# Docker build and push workflow +name: Create and publish a Docker image + +# Triggered by workflow_dispatch +on: + workflow_dispatch: + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + attestations: write + id-token: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + id: push + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + # This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)." + - 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