Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add build and deployment automation #9

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# What ORG to activate (nfk | atb | fram)
NEXT_PUBLIC_WEBSHOP_ORG_ID=atb
NEXT_PUBLIC_PLANNER_ORG_ID=atb
15 changes: 15 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Docker Build, Push & Deploy

on:
release:
types: [published]
push:
branches: ['main']

jobs:
build:
uses: atb-as/workflows/.github/workflows/cluster-docker-build-tag-push.yaml@v2
with:
image: gcr.io/atb-mobility-platform/planner-web
secrets:
github_pat: ${{ secrets.GH_PAT }}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ jobs:
- run: yarn
- run: yarn setup
env:
NEXT_PUBLIC_WEBSHOP_ORG_ID: atb
NEXT_PUBLIC_PLANNER_ORG_ID: atb
- run: yarn test
2 changes: 1 addition & 1 deletion .github/workflows/tsc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ jobs:
- run: yarn
- run: yarn setup
env:
NEXT_PUBLIC_WEBSHOP_ORG_ID: atb
NEXT_PUBLIC_PLANNER_ORG_ID: atb
- run: yarn tsc
- run: yarn lint
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# production
/build
/dist

# misc
.DS_Store
Expand Down
44 changes: 44 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Install dependencies only when needed
FROM node:18-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json yarn.lock* ./
RUN yarn --frozen-lockfile

# Rebuild the source code only when needed
FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN yarn build:all

# Production image, copy all the files and run next
FROM node:18-alpine AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Set the correct permission for prerender cache
RUN mkdir dist
RUN chown nextjs:nodejs dist

COPY --from=builder --chown=nextjs:nodejs /app/dist ./dist
COPY server.js ./

USER nextjs

EXPOSE 3000

ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ TBA

```
# What ORG to activate (nfk | atb | fram)
NEXT_PUBLIC_WEBSHOP_ORG_ID=atb
NEXT_PUBLIC_PLANNER_ORG_ID=atb
```
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('next').NextConfig} */

const orgId = process.env.NEXT_PUBLIC_WEBSHOP_ORG_ID;
const orgId = process.env.NEXT_PUBLIC_PLANNER_ORG_ID;

const nextConfig = {
optimizeFonts: false,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"test": "vitest",
"dev": "next dev",
"build": "next build",
"build:all": "sh ./scripts/build.sh",
"start": "next start",
"lint": "next lint",
"clean-assets": "rimraf ./public/assets/",
"setup": "generate-assets all $NEXT_PUBLIC_WEBSHOP_ORG_ID -o ./public/assets/ -ts -ts-o ./src/components/icon"
"setup": "generate-assets all $NEXT_PUBLIC_PLANNER_ORG_ID -o ./public/assets/ -ts -ts-o ./src/components/icon"
},
"dependencies": {
"@atb-as/theme": "^7.1.1",
Expand Down
21 changes: 21 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# This script prepares and organizes the application for different organizations
# by setting up the environment, building the application, and organizing the output
# into the 'dist' directory with separate subdirectories for each organization.

mkdir dist

for orgId in atb nfk fram; do
mkdir dist/$orgId
export NEXT_PUBLIC_PLANNER_ORG_ID=$orgId
echo "Running yarn setup && yarn build for $orgId"
yarn setup $orgId
yarn build

echo "Moving the output into the dist directory"
mv .next/standalone dist/$orgId
mv .next/static dist/$orgId/standalone/.next
cp -r public dist/$orgId/standalone
cp next.config.js dist/$orgId/standalone
done
15 changes: 15 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const orgId = process.env.NEXT_PUBLIC_PLANNER_ORG_ID || 'atb';

switch (orgId) {
case 'atb':
require('./dist/atb/standalone/server.js');
break;
case 'nfk':
require('./dist/nfk/standalone/server.js');
break;
case 'fram':
require('./dist/fram/standalone/server.js');
break;
default:
throw new Error('Invalid org ID provided.');
}
4 changes: 2 additions & 2 deletions src/modules/org-data/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type WEBSHOP_ORGS = 'nfk' | 'atb' | 'fram';
export const currentOrg = getCurrentOrg();

function getCurrentOrg(): WEBSHOP_ORGS {
const orgId = process.env.NEXT_PUBLIC_WEBSHOP_ORG_ID;
const orgId = process.env.NEXT_PUBLIC_PLANNER_ORG_ID;
switch (orgId) {
case 'atb':
return 'atb';
Expand All @@ -12,5 +12,5 @@ function getCurrentOrg(): WEBSHOP_ORGS {
return 'fram';
}

throw new Error('NEXT_PUBLIC_WEBSHOP_ORG_ID required but missing');
throw new Error('NEXT_PUBLIC_PLANNER_ORG_ID required but missing');
}
3 changes: 3 additions & 0 deletions src/pages/_error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ErrorPage() {
return <h1>Something went wrong...</h1>;
}