簡體   English   中英

Fly.io 部署 - 秘密環境變量未定義

[英]Fly.io deployment - secret env variables undefined

我對編碼還很陌生(8 月開始),我會盡力提供所有必要的信息!

我正在為 Next.js 的一位朋友開發博客,該博客有一個供她使用的幽靈后端(動態部署。io 使用現成的 docker 圖像運行得非常好)和一個額外的前端頁面。 我們需要后者,因為她希望博客文章以特定方式出現。 所以我們不能使用ghost提供的模板。

前端網站就是那個,我還不能部署。 它在構建階段失敗並顯示以下錯誤:

#10 46.29 Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
#10 46.29 TypeError: Failed to parse URL from undefined/ghost/api/v3/content/posts/?key=undefined&fields=title,slug,html&include=tags
#10 46.29     at Object.fetch (node:internal/deps/undici/undici:11118:11)
#10 46.29     at async getPosts (/app/.next/server/pages/index.js:33:17)
#10 46.29     at async getStaticProps (/app/.next/server/pages/index.js:38:19)
#10 46.29     at async renderToHTML (/app/node_modules/next/dist/server/render.js:385:20)
#10 46.29     at async /app/node_modules/next/dist/export/worker.js:277:36
#10 46.29     at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:79:20)
#10 46.29 info  - Generating static pages (1/6)
#10 46.29 (node:114) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
#10 46.29 (Use `node --trace-warnings ...` to show where the warning was created)
#10 46.36 info  - Generating static pages (2/6)
#10 46.38 info  - Generating static pages (4/6)
#10 46.42 info  - Generating static pages (6/6)
#10 46.42
#10 46.43 > Build error occurred

因此,變量不會以某種方式“到達”我的獲取請求: undefined/ghost/api/v3/content/posts/?key=undefined&fields=title,slug,html&include=tags 關聯的 function 如下所示:

async function getPosts() {
  const res = await fetch(
    `${process.env.BLOG_URL}/ghost/api/v3/content/posts/?key=${process.env.CONTENT_API_KEY}&fields=title,slug,html&include=tags`,
  ).then((resp) => resp.json());

  const posts = res.posts;

  return posts;
}

我的 Dockerfile 看起來像這樣:

# Initialize builder layer
FROM node:18-alpine AS builder
ENV NODE_ENV production
# Install necessary tools
RUN apk add --no-cache libc6-compat yq --repository=https://dl-cdn.alpinelinux.org/alpine/edge/community
WORKDIR /app
# Copy the content of the project to the machine
COPY . .
RUN yq --inplace --output-format=json '.dependencies = .dependencies * (.devDependencies | to_entries | map(select(.key | test("^(typescript|@types/*|@upleveled/)"))) | from_entries)' package.json
RUN --mount=type=secret,id=BLOG_URL \
    BLOG_URL="$(cat /run/secrets/BLOG_URL)"
RUN --mount=type=secret,id=CONTENT_API_KEY \
    CONTENT_API_KEY="$(cat /run/secrets/CONTENT_API_KEY)"
RUN yarn install --frozen-lockfile
RUN yarn build

# Initialize runner layer
FROM node:18-alpine AS runner
ENV NODE_ENV production

# Copy built app
COPY --from=builder /app/.next ./.next

# Copy only necessary files to run the app (minimize production app size, improve performance)
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./
COPY --from=builder /app/.env.production ./

CMD ["yarn", "start"]

構建問題似乎與環境變量有關,我嘗試過 1) 通過flyctl secrets set BLOG_URL=xyz設置運行時和 2) 嘗試通過flyctl deploy --build-secret BLOG_URL=xyz設置並告訴docker 像這樣在我的 Dockerfile 中使用它(也見上文):

RUN --mount=type=secret,id=BLOG_URL \
    BLOG_URL="$(cat /run/secrets/BLOG_URL)"
RUN --mount=type=secret,id=CONTENT_API_KEY \
    CONTENT_API_KEY="$(cat /run/secrets/CONTENT_API_KEY)"

當我運行flyctl secrets list時,(運行時)秘密存在。

我不知道發生了什么,也不知道為什么構建秘密和運行時秘密都沒有改變我的“未定義”URL 的任何內容。

我感謝任何提示和幫助!

我可以在朋友的幫助下找到解決方案/解決方法。 該問題可以在我尚未發布的部分代碼中解決:

export const getServerSideProps = async () => {
  const posts = await getPosts();
  if (typeof posts === 'undefined') {
    return {
      props: {
        error: 'Nothing to see here',
      },
    };
  }
  return {
    props: { posts },
  };
};

在這段代碼中,我使用了getStaticProps ,它在構建期間需要變量。 通過使用getServerSideProps ,運行時環境變量就足夠了。

所以,現在我不使用任何docker secrets ,既不在部署期間也不在我的 Dockerfile 中,只使用運行時fly secrets

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM