I stick to this problem quite long now:
I have a standard NextJS app, which uses environment variables (for client side NEXT_PUBLIC_MY_VAR
as well for server side ones MY_OTHER_VAR
).
I use the Gitlab CI-CD AutoDevOps with an tiny custom .gitlab-ci.yml
-file (see below).
I have a successful connection to my Kubernetes cluster with Gitlab, and my NextJS app is getting also deployed successfully. (Gitlab/K8s cache cleaned, too)
The only thing where I struggle is to get the process.env.ENV_VARS
running.
Whatever I tried they are undefined
.
I deployed my app manually into the cluster, and mounted a configMap to my deployment ( so a .env.local
-file is present at /app/.env.local
ONLY THEN the ENV_VARS
are set correctly.
So how to set the ENV_VARS
when deploying my NextJS app via Gitlab Auto DevOps?
I've tried so far:
ENV_VARS
in Gitlab -> Settings -> CI/CD -> VariablesFROM node:alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# ...
# ..
# docker build --build-arg API_URL=http://myApiEndpoint
ARG API_URL
ENV API_URL=$API_URL
RUN npm run build
FROM node:alpine AS runner
WORKDIR /app
ENV NODE_ENV production
# COPY --from=builder /app/next.config.js ./ # TRIED WITH AND WITHOUT
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /app/.next
USER nextjs
EXPOSE 5000
CMD ["npm", "run", "start"]
next.config.js
module.exports = withSvgr({
serverRuntimeConfig: {},
publicRuntimeConfig: {
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
API_URL: process.env.API_URL,
},
});
.gitlab-ci.yml
file (complete file here):yaml include: - template: Auto-DevOps.gitlab-ci.yml
# added vars for build
build:
stage: build
variables:
API_URL: $API_URL
This is the error message in my POD
> metashop-frontend.react@1.8.0 start
> next start -p 5000
ready - started server on 0.0.0.0:5000, url: http://localhost:5000
ApiURL alias: undefined #### <<<----
API_URL: undefined #### <<<----
TypeError: Only absolute URLs are supported
at getNodeRequestOptions (/app/node_modules/node-fetch/lib/index.js:1305:9)
at /app/node_modules/node-fetch/lib/index.js:1410:19
at new Promise (<anonymous>)
at fetch (/app/node_modules/node-fetch/lib/index.js:1407:9)
at Object.getItems (/app/.next/server/pages/_app.js:1194:12)
at getServerSideProps (/app/.next/server/pages/index.js:2952:55)
at renderToHTML (/app/node_modules/next/dist/next-server/server/render.js:40:221)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /app/node_modules/next/dist/next-server/server/next-server.js:109:97
at async /app/node_modules/next/dist/next-server/server/next-server.js:102:142
And is the code that the error refers to:
const ApiUrl = process.env.API_URL;
console.log("ApiURL alias:", ApiUrl);
console.log("API_URL:", process.env.API_URL);
console.log("NEXT_PUBLIC_API_URL:", process.env.NEXT_PUBLIC_API_URL);
return fetch(`${ApiUrl}/items.json?${qs.stringify(options)}`).then(
(response) => response.json()
);
and for completeness (but mostly useless) the tail of the failing job (which seems the normal error when K8s not responding):
Error: release production failed, and has been uninstalled due to atomic being set: timed out waiting for the condition
Uploading artifacts for failed job
00:01
Uploading artifacts...
WARNING: environment_url.txt: no matching files
WARNING: tiller.log: no matching files
ERROR: No files to upload
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1
.env.local
the only way to use NextJS ENV_VARS in Kubernetes?Thank you in advance, any help would be appreciated
If you stick on Auto Devops on GITLAB then YOU HAVE TO set this Gitlab ENV Variable AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS
and set it to
--build-arg=NEXT_PUBLIC_API_URL=http://your.domain.com/api
In your Dockerfile you can assign it via
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
More information here:
https://docs.gitlab.com/ee/topics/autodevops/customize.html#passing-arguments-to-docker-build