Skip to content

Commit

Permalink
Create a Docker Image for Web (#4533)
Browse files Browse the repository at this point in the history
  • Loading branch information
Podginator authored Feb 5, 2025
1 parent e34749f commit ae6a25f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build-self-host-docker-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push web
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/omnivore-app/sh-web:latest,ghcr.io/omnivore-app/sh-web:${{ github.sha }}
file: packages/web/Dockerfile-self
cache-from: type=registry,ref=ghcr.io/omnivore-app/sh-web:${{ github.sha }}
cache-to: type=registry,ref=ghcr.io/omnivore-app/sh-web:${{ github.sha }},mode=max

- name: Build and push image-proxy
uses: docker/build-push-action@v6
with:
Expand Down
12 changes: 3 additions & 9 deletions packages/web/Dockerfile-self
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@

FROM node:22.12-alpine as builder

Check warning on line 4 in packages/web/Dockerfile-self

View workflow job for this annotation

GitHub Actions / Build self-host docker images

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/
ENV NODE_OPTIONS=--max-old-space-size=8192
ARG APP_ENV
ARG BASE_URL
ARG SERVER_BASE_URL
ARG HIGHLIGHTS_BASE_URL
ENV NEXT_PUBLIC_APP_ENV=$APP_ENV
ENV NEXT_PUBLIC_BASE_URL=$BASE_URL
ENV NEXT_PUBLIC_SERVER_BASE_URL=$SERVER_BASE_URL
ENV NEXT_PUBLIC_HIGHLIGHTS_BASE_URL=$HIGHLIGHTS_BASE_URL


RUN apk add g++ make python3 py3-setuptools
Expand Down Expand Up @@ -50,4 +42,6 @@ COPY --from=builder /app/node_modules /app/node_modules
COPY --from=builder /app/package.json /app/package.json

EXPOSE 8080
CMD ["yarn", "workspace", "@omnivore/web", "start"]
COPY ./packages/web/env.sh /app/packages/web/env.sh
RUN chmod +x /app/packages/web/env.sh
ENTRYPOINT ["/app/packages/web/env.sh"]
21 changes: 21 additions & 0 deletions packages/web/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

if [ -z "$SERVER_BASE_URL" ]; then
echo "Error: SERVER_BASE_URL environment variable is not set."
exit 1
fi

file_contents=$(cat /app/packages/web/public/env.js)
new_contents=${file_contents//\{\{SERVER_BASE_URL\}\}/$SERVER_BASE_URL}
echo "$new_contents" > /app/packages/web/public/env.js

if [ -z "$BASE_URL" ]; then
echo "Error: BASE_URL environment variable is not set."
exit 1
fi

file_contents=$(cat /app/packages/web/public/env.js)
new_contents=${file_contents//\{\{BASE_URL\}\}/$BASE_URL}
echo "$new_contents" > /app/packages/web/public/env.js

yarn workspace @omnivore/web start
18 changes: 12 additions & 6 deletions packages/web/lib/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ const baseURLRecords: BaseURLRecords = {

function serverBaseURL(env: AppEnvironment): string {
const value = baseURLRecords[appEnv].serverBaseURL
if (value.length == 0) {
throw new Error(
`Couldn't find environment variable for server base url in ${env} environment`
if (value.length == 0 && global.window) {
const windowEnv = (window as any).omnivoreEnv
console.warn(
`Couldn't find environment variable for server base url in ${env} environment, using ${windowEnv.SERVER_BASE_URL}`
)

return windowEnv.SERVER_BASE_URL ?? ''
}
return value
}

function webURL(env: AppEnvironment): string {
const value = baseURLRecords[appEnv].webBaseURL
if (value.length == 0) {
throw new Error(
`Couldn't find environment variable for web base url in ${env} environment`
if (value.length == 0 && global.window) {
const windowEnv = (window as any).omnivoreEnv
console.warn(
`Couldn't find environment variable for base url in ${env} environment, using ${windowEnv.BASE_URL}`
)

return windowEnv.BASE_URL ?? ''
}
return value
}
Expand Down
2 changes: 2 additions & 0 deletions packages/web/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import NextDocument, { Html, Head, Main, NextScript } from 'next/document'
import { getCssText, globalStyles } from '../components/tokens/stitches.config'
import { Toaster } from 'react-hot-toast'
import Script from 'next/script'

export default class Document extends NextDocument {
render() {
Expand All @@ -18,6 +19,7 @@ export default class Document extends NextDocument {
<link rel="manifest" href="/manifest.webmanifest" />
<script async src="/static/scripts/intercom.js" />
<script async src="/static/scripts/inject-sw.js" />
<Script strategy={"beforeInteractive"} src={"/env.js"} />

{/* prefetch (not preload) fonts that will be used by the reader */}
<link rel="prefetch" href="/static/fonts/Lora/Lora-Regular.ttf" />
Expand Down
4 changes: 4 additions & 0 deletions packages/web/public/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
window.omnivoreEnv = {
SERVER_BASE_URL: "{{SERVER_BASE_URL}}",
BASE_URL: "{{BASE_URL}}"
}
10 changes: 1 addition & 9 deletions self-hosting/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,7 @@ services:
condition: service_started

web:
build:
context: ../../
dockerfile: ./packages/web/Dockerfile-self
args:
- APP_ENV=prod
- BASE_URL=http://localhost:3000
- SERVER_BASE_URL=http://localhost:4000
- HIGHLIGHTS_BASE_URL=http://localhost:3000
- USE_NATIVE_PDF=true
image: "ghcr.io/omnivore-app/sh-web:latest"
container_name: "omnivore-web"
ports:
- "3000:8080"
Expand Down

0 comments on commit ae6a25f

Please sign in to comment.