![](/img/trans.png)
[英]Google App Engine: Setting environment variables for React Production Build
[英]React secure environment variables on production build
我正在構建一個React應用,並使用Docker和Jenkins進行生產部署...如何為該靜態應用安全地提供環境變量? 這是我的Dockerfile:
# stage 1: build the react app
FROM node:10.15.0 as react-build
WORKDIR /app
COPY . /app
ARG REACT_APP_API_ENTRYPOINT
ARG REACT_APP_CONNECT_URI
ARG REACT_APP_CONNECT_CLIENT_ID
ARG REACT_APP_CONNECT_SECRET
ARG REACT_APP_CONNECT_CALLBACK_URL
RUN yarn
ENV NODE_ENV=production
ENV REACT_APP_API_ENTRYPOINT=${REACT_APP_API_ENTRYPOINT}
ENV REACT_APP_CONNECT_URI=${REACT_APP_CONNECT_URI}
ENV REACT_APP_CONNECT_CLIENT_ID=${REACT_APP_CONNECT_CLIENT_ID}
ENV REACT_APP_CONNECT_SECRET=${REACT_APP_CONNECT_SECRET}
ENV REACT_APP_CONNECT_CALLBACK_URL=${REACT_APP_CONNECT_CALLBACK_URL}
RUN yarn run -s build
# Stage 2: build the production environment
FROM openresty/openresty:alpine
COPY deploy/files/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=react-build /app /usr/local/openresty/nginx/html
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log && \
ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log
EXPOSE 8000
ENTRYPOINT ["nginx", "-g", "daemon off;"]
目前,構建過程使這些var暴露了:(
您可以使用dotenv在構建過程中使用.env文件加載環境變量(例如,在帶有DefinePlugin的webkpack中)。
然后,您只需RUN yarn run -s build
而不必指定每個單獨的env變量。
甚至為您的Webpack設置提供了一個快捷npm軟件包。 的WebPack-dotenv
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.