Fly.io 部署 - 秘密环境变量未定义

问题描述 投票:0回答:2

我对编码还很陌生(从八月开始),并将尝试提供所需的所有必要信息!

我正在为一位朋友在 Next.js 中开发一个博客,该博客有一个幽灵后端供她工作(在 Fly.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
。相关函数如下所示:

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 的任何内容。

我感谢任何提示和帮助!

next.js deployment dockerfile docker-secrets fly
2个回答
0
投票

看起来你只需要一个运行时秘密:

process.env.BLOG_URL
是运行时秘密。

使用

fly secrets
并且环境变量将被设置/呈现。

https://fly.io/docs/apps/secrets/


-1
投票

我可以在朋友的帮助下找到解决方案/解决方法。这个问题可以在我还没有发布的部分代码中解决:

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

🆒

© www.soinside.com 2019 - 2024. All rights reserved.