Skip to content

Commit 7802de2

Browse files
committed
feat(workshop): web-app-serve setup
1 parent 9a6c917 commit 7802de2

File tree

7 files changed

+135
-50
lines changed

7 files changed

+135
-50
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Publish web app serve
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- develop
8+
- web-app-serve-session-01/shreeyash # TODO: REMOVE me
9+
10+
permissions:
11+
packages: write
12+
13+
jobs:
14+
publish_image:
15+
name: Publish Docker Image
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
submodules: true
21+
22+
- name: Publish web-app-serve
23+
uses: toggle-corp/web-app-serve-action@v0.1.1
24+
with:
25+
github_token: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,45 @@ RUN apt-get update -y \
1212
WORKDIR /code
1313

1414
# -------------------------- Nginx - Builder --------------------------------
15-
FROM dev AS nginx-build
15+
FROM dev AS web-app-serve-build
1616

1717
COPY ./package.json ./pnpm-lock.yaml /code/
1818

1919
RUN pnpm install
2020

2121
COPY . /code/
2222

23-
# Build variables (Requires backend pulled)
23+
# NOTE: Dynamic env variables
24+
# These env variables can be dynamically defined in web-app-serve container runtime.
25+
# These variables are not included in the build files but the values should still be valid.
26+
# See "schema" field in "./env.ts"
27+
ENV APP_TITLE="Shreeyash Dashboard"
28+
ENV APP_GRAPHQL_ENDPOINT=https://my-best-dashboard.com/graphql/
29+
30+
ENV APP_ENVIRONMENT=production
31+
32+
ENV APP_GRAPHQL_DOMAIN=https://my-best-dashboard.com/domain/
33+
34+
# NOTE: These are set directly in `vite.config.ts`
35+
# We're using raw web-app-serve placeholder values here to treat them as dynamic values
36+
ENV APP_ANALYTIC_SRC=WEB_APP_SERVE_PLACEHOLDER__APP_ANALYTIC_SRC
37+
38+
# NOTE: Static env variables:
39+
# These env variables are used during build
2440
ENV APP_GRAPHQL_CODEGEN_ENDPOINT=./backend/schema.graphql
2541

26-
RUN pnpm generate:type && pnpm build
42+
# NOTE: WEB_APP_SERVE_ENABLED=true will skip defining the above dynamic env variables
43+
# See "overrideDefine" field in "./env.ts"
44+
RUN pnpm generate:type && WEB_APP_SERVE_ENABLED=true pnpm build
45+
46+
# ---------------------------------------------------------------------
47+
# Final image using web-app-serve
48+
FROM ghcr.io/toggle-corp/web-app-serve:v0.1.2 AS web-app-serve
49+
50+
LABEL org.opencontainers.image.source="https://github.com/toggle-corp/shreeyash-dashboard"
51+
LABEL org.opencontainers.image.authors="shreeyash.shrestha@togglecorp.com"
52+
53+
# Env for apply-config script
54+
ENV APPLY_CONFIG__SOURCE_DIRECTORY=/code/build/
55+
56+
COPY --from=web-app-serve-build /code/build "$APPLY_CONFIG__SOURCE_DIRECTORY"

env.ts

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
import { defineConfig, Schema } from '@julr/vite-plugin-validate-env';
1+
import {
2+
defineConfig,
3+
overrideDefineForWebAppServe,
4+
Schema,
5+
} from '@julr/vite-plugin-validate-env';
6+
7+
const webAppServeEnabled = process.env.WEB_APP_SERVE_ENABLED?.toLowerCase() === 'true';
8+
if (webAppServeEnabled) {
9+
// eslint-disable-next-line no-console
10+
console.warn('Building application for web-app-serve');
11+
}
12+
const overrideDefine = webAppServeEnabled
13+
? overrideDefineForWebAppServe
14+
: undefined;
215

316
export default defineConfig({
4-
APP_TITLE: Schema.string(),
5-
APP_ENVIRONMENT: (key, value) => {
6-
// NOTE: APP_ENVIRONMENT_PLACEHOLDER is meant to be used with image builds
7-
// The value will be later replaced with the actual value
8-
const regex = /^production|staging|testing|alpha-\d+|development|APP_ENVIRONMENT_PLACEHOLDER$/;
9-
const valid = !!value && (value.match(regex) !== null);
10-
if (!valid) {
11-
throw new Error(`Value for environment variable "${key}" must match regex "${regex}", instead received "${value}"`);
12-
}
13-
if (value === 'APP_ENVIRONMENT_PLACEHOLDER') {
14-
console.warn(`Using ${value} for app environment. Make sure to not use this for builds without nginx-serve`)
15-
}
16-
return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development' | 'APP_ENVIRONMENT_PLACEHOLDER');
17+
overrideDefine,
18+
validator: 'builtin',
19+
schema: {
20+
APP_TITLE: Schema.string(),
21+
APP_ENVIRONMENT: (key, value) => {
22+
// NOTE: APP_ENVIRONMENT_PLACEHOLDER is meant to be used with image builds
23+
// The value will be later replaced with the actual value
24+
const regex = /^production|staging|testing|alpha-\d+|development|APP_ENVIRONMENT_PLACEHOLDER$/;
25+
const valid = !!value && (value.match(regex) !== null);
26+
if (!valid) {
27+
throw new Error(`Value for environment variable "${key}" must match regex "${regex}", instead received "${value}"`);
28+
}
29+
if (value === 'APP_ENVIRONMENT_PLACEHOLDER') {
30+
console.warn(`Using ${value} for app environment. Make sure to not use this for builds without nginx-serve`);
31+
}
32+
return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development' | 'APP_ENVIRONMENT_PLACEHOLDER');
33+
},
34+
APP_GRAPHQL_CODEGEN_ENDPOINT: Schema.string(),
35+
APP_GRAPHQL_DOMAIN: Schema.string(),
36+
APP_UMAMI_SRC: Schema.string.optional(),
37+
APP_UMAMI_ID: Schema.string.optional(),
38+
APP_SENTRY_DSN: Schema.string.optional(),
1739
},
18-
APP_GRAPHQL_CODEGEN_ENDPOINT: Schema.string(),
19-
APP_GRAPHQL_DOMAIN: Schema.string(),
20-
APP_UMAMI_SRC: Schema.string.optional(),
21-
APP_UMAMI_ID: Schema.string.optional(),
22-
APP_SENTRY_DSN: Schema.string.optional(),
2340
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@graphql-codegen/cli": "^5.0.2",
4444
"@graphql-codegen/client-preset": "^4.3.3",
4545
"@graphql-typed-document-node/core": "^3.2.0",
46-
"@julr/vite-plugin-validate-env": "^2.2.0",
46+
"@julr/vite-plugin-validate-env": "git+https://github.com/toggle-corp/vite-plugin-validate-env#v2.2.0-tc.1",
4747
"@types/node": "^20.11.6",
4848
"@types/react": "^18.0.28",
4949
"@types/react-dom": "^18.0.11",

pnpm-lock.yaml

Lines changed: 24 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web-app-serve/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

web-app-serve/docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# NOTE: The name should is mandatory and should be unique
2+
name: shreeyash-dashboard
3+
4+
services:
5+
web-app-serve:
6+
build:
7+
context: ../
8+
target: web-app-serve
9+
environment:
10+
# web-app-serve config
11+
APPLY_CONFIG__ENABLE_DEBUG: true
12+
# NOTE: See "Dockerfile" to get dynamic env variables for .env file
13+
env_file: .env
14+
ports:
15+
- '8050:80'

0 commit comments

Comments
 (0)