Skip to content

Commit

Permalink
feat: encode env as build args for frontend build
Browse files Browse the repository at this point in the history
  • Loading branch information
nischalstha9 committed Aug 21, 2024
1 parent 4858fc7 commit 2c7f0fd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,34 @@ jobs:
dockerfile: Dockerfile
secrets: inherit

get_envs:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
environment: ${{ github.ref_name }}
secrets: inherit

encode_envs:
runs-on: ubuntu-latest
needs:
- get_envs
outputs:
base64_env: ${{ steps.to_base64.outputs.base64_env }}
steps:
- name: Export env to base64 for frontend build
id: to_base64
run: |
echo "base64_env=`${{ needs.get_envs.outputs.all_vars }}' | base64`" >> "$GITHUB_OUTPUT"
frontend-build:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
needs:
- encode_envs
with:
context: ./src/frontend
build_target: live
image_name: ghcr.io/${{ github.repository }}/frontend
dockerfile: Dockerfile
extra_build_args: B64_ARGS_TO_ENV=${{ needs.encode_envs.outputs.base64_env }}
secrets: inherit

postgres-vol-creation:
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ services:
context: src/frontend
dockerfile: Dockerfile
target: ${FRONTEND_TARGET_OVERRIDE:-development}
args:
B64_ARGS_TO_ENV: <USE FOR LIVE. Base64 encoded env variables>
ports:
- ${FRONTEND_WEB_APP_PORT:-3040}:3040
depends_on:
Expand Down
13 changes: 7 additions & 6 deletions src/frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# ARG for the base image
ARG NODE_BASE=18.16.0-bullseye-slim
ARG MINIO_TAG=RELEASE.2024-08-03T04-33-23Z
ARG B64_ARGS_TO_ENV

# Base image with frontend code
FROM node:${NODE_BASE} AS base

RUN mkdir -p /app
WORKDIR /app
COPY ./package.json .
RUN ["/bin/sh", "-c", "env | tee .env"]
RUN yarn

# Run development environment
FROM base AS development
COPY . /app
ENTRYPOINT ["/bin/sh", "-c", "yarn start --host 0.0.0.0;"]
ENTRYPOINT ["/bin/sh", "-c", "yarn; yarn start --host 0.0.0.0;"]

# Generate frontend build files
FROM base AS build
ARG B64_ARGS_TO_ENV
COPY ./package.json .
RUN yarn
COPY . /app
RUN ( echo ${B64_ARGS_TO_ENV} | base64 -d ) > .env
RUN yarn build

# Copy static files to minio and generated index.html to volume
FROM docker.io/minio/minio:${MINIO_TAG} AS live
COPY --from=build /app/dist /tmp/dist
RUN ( echo ${B64_ARGS_TO_ENV} | base64 -d ) > /.env
VOLUME /tmp/frontend_html
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT [ "/docker-entrypoint.sh" ]
2 changes: 2 additions & 0 deletions src/frontend/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/sh
set -e

source /.env

if [[ -z "${S3_ACCESS_KEY}" ]]; then
echo "Missing environment variable S3_ACCESS_KEY"
exit 0
Expand Down

0 comments on commit 2c7f0fd

Please sign in to comment.