R
Railway7mo ago
Craig

ENV vars not been passed on build?

Hello, i'm currently building a Payload CMS & Next.js website, I'm trying to deploy the site to Railway but keep getting the following error. Error: Error: missing secret key. A secret key is needed to secure Payload Even though all my env vars are set. Anyone had a similar sort of issue?
17 Replies
Percy
Percy7mo ago
Project ID: 1c0a43f8-b799-450b-a357-77fe5d973c74
Craig
Craig7mo ago
1c0a43f8-b799-450b-a357-77fe5d973c74
Fragly
Fragly7mo ago
Are the env vars set on railway's "Service Variables" in your project settings?
Craig
Craig7mo ago
Yeah
No description
Brody
Brody7mo ago
are you building with a dockerfile?
Craig
Craig7mo ago
@Brody yeah
Brody
Brody7mo ago
are you referencing the variable with ARG VARIABLE_NAME
Craig
Craig7mo ago
@Brody I don't reference any of the above variables in my docker file
FROM node:18.8-alpine as base

FROM base as builder

WORKDIR /home/node/app
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js

WORKDIR /home/node/app
COPY package*.json ./
COPY yarn.lock ./

RUN yarn install --production
COPY --from=builder /home/node/app/dist ./dist
COPY --from=builder /home/node/app/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]
FROM node:18.8-alpine as base

FROM base as builder

WORKDIR /home/node/app
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js

WORKDIR /home/node/app
COPY package*.json ./
COPY yarn.lock ./

RUN yarn install --production
COPY --from=builder /home/node/app/dist ./dist
COPY --from=builder /home/node/app/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]
Craig
Craig7mo ago
Turns out adding them does nothing... pushing my .env file does fix the issue but this obviously isn't ideal.
Brody
Brody7mo ago
show me the updated dockerfile please
Craig
Craig7mo ago
FROM node:18.8-alpine as base

FROM base as builder

WORKDIR /home/node/app
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js

ENV DATABASE_URI=xxx
ENV NEXT_PRIVATE_DRAFT_SECRET=xxx
ENV NEXT_PRIVATE_REVALIDATION_KEY=xxx
ENV NEXT_PUBLIC_IS_LIVE=false
ENV NEXT_PUBLIC_SERVER_URL=https://xxx
ENV PAYLOAD_PUBLIC_DRAFT_SECRET=xxx
ENV PAYLOAD_PUBLIC_SERVER_URL=https://xxx
ENV PAYLOAD_SECRET=xxx
ENV PORT=3000
ENV REVALIDATION_KEY=xxx

WORKDIR /home/node/app
COPY package*.json ./
COPY yarn.lock ./

RUN yarn install --production
COPY --from=builder /home/node/app/dist ./dist
COPY --from=builder /home/node/app/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]
FROM node:18.8-alpine as base

FROM base as builder

WORKDIR /home/node/app
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js

ENV DATABASE_URI=xxx
ENV NEXT_PRIVATE_DRAFT_SECRET=xxx
ENV NEXT_PRIVATE_REVALIDATION_KEY=xxx
ENV NEXT_PUBLIC_IS_LIVE=false
ENV NEXT_PUBLIC_SERVER_URL=https://xxx
ENV PAYLOAD_PUBLIC_DRAFT_SECRET=xxx
ENV PAYLOAD_PUBLIC_SERVER_URL=https://xxx
ENV PAYLOAD_SECRET=xxx
ENV PORT=3000
ENV REVALIDATION_KEY=xxx

WORKDIR /home/node/app
COPY package*.json ./
COPY yarn.lock ./

RUN yarn install --production
COPY --from=builder /home/node/app/dist ./dist
COPY --from=builder /home/node/app/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]
Brody
Brody7mo ago
hmmm don't think you read the docs page I linked either way, nixpacks should have no problem building a payload app, try temporarily removing or renaming the dockerfile and for sure remove the .env file from the repo and make sure it's in your gitignore
Craig
Craig7mo ago
Sorry just realised I didn't redo the file far enough, but I did use ARG PAYLOAD_SECRET=xxx already removed the .env was just a tempory thing to see if it built. WIll try removing the docker file and re adding it again.
Brody
Brody7mo ago
let me know how that goes!
Craig
Craig7mo ago
@Brody simple fix after all I just needed to move ARG PAYLOAD_SECRET before FROM base as runtime
Brody
Brody7mo ago
nixpacks couldn't build your app?