Skip to content

Commit 2ee725d

Browse files
authored
Merge pull request #12 from icdocsoc/collection-deploy: add docker files & docs for collection
Get Collection ready for Deployment
2 parents 5a2b9ee + dc1067c commit 2ee725d

File tree

15 files changed

+374
-38
lines changed

15 files changed

+374
-38
lines changed

.github/workflows/ci.yml

Lines changed: 142 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: CI
22

3+
env:
4+
REGISTRY: ghcr.io
5+
COLLECTION_IMAGE_NAME: icdocsoc/collection
6+
37
on:
48
push:
59
pull_request:
@@ -9,32 +13,152 @@ permissions:
913
contents: read
1014

1115
jobs:
12-
main:
16+
changes:
1317
runs-on: ubuntu-latest
18+
outputs:
19+
migrations: ${{ steps.filter.outputs.migrations }}
1420
steps:
1521
- uses: actions/checkout@v4
22+
- uses: dorny/paths-filter@v3
23+
id: filter
1624
with:
17-
fetch-depth: 0
18-
19-
# Connect your workspace on nx.app and uncomment this to enable task distribution.
20-
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested
21-
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build"
25+
filters: |
26+
migrations:
27+
- 'collection/prisma/migrations/**'
2228
23-
# Cache node_modules
24-
- uses: actions/setup-node@v3
29+
install-dependencies:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v4
33+
- uses: actions/setup-node@v4
2534
with:
26-
node-version: 20
35+
node-version-file: ".nvmrc"
2736
cache: "npm"
37+
- name: Cache node_modules
38+
uses: actions/cache@v4
39+
id: cache-primes
40+
with:
41+
path: node_modules
42+
key: ${{ runner.os }}-nx-${{ hashFiles('package-lock.json') }}
43+
- name: Install dependencies
44+
run: npm ci
2845

29-
- run: npm ci
30-
- uses: nrwl/nx-set-shas@v4
46+
build:
47+
runs-on: ubuntu-latest
48+
needs: install-dependencies
49+
steps:
50+
- uses: actions/checkout@v4
51+
with:
52+
fetch-depth: 0
53+
- uses: actions/setup-node@v4
54+
with:
55+
node-version-file: ".nvmrc"
56+
cache: "npm"
57+
- name: Load cached node_modules
58+
uses: actions/cache@v4
59+
id: cache-primes
60+
with:
61+
path: node_modules
62+
key: ${{ runner.os }}-nx-${{ hashFiles('package-lock.json') }}
63+
- name: Build libs
64+
run: npx nx run-many -t build-local
65+
- name: Prisma generate
66+
run: npx nx run-many -t prisma-generate
67+
- name: Derive appropriate SHAs for base and head for `nx affected` commands
68+
uses: nrwl/nx-set-shas@v4
69+
- name: Build all
70+
run: npx nx affected -t build build-local
3171

32-
# Build util
33-
- run: npx nx run-many -t build-local
72+
lint:
73+
runs-on: ubuntu-latest
74+
needs: install-dependencies
75+
steps:
76+
- uses: actions/checkout@v4
77+
with:
78+
fetch-depth: 0
79+
- uses: actions/setup-node@v4
80+
with:
81+
node-version-file: ".nvmrc"
82+
cache: "npm"
83+
- name: Load cached node_modules
84+
uses: actions/cache@v4
85+
id: cache-primes
86+
with:
87+
path: node_modules
88+
key: ${{ runner.os }}-nx-${{ hashFiles('package-lock.json') }}
89+
- name: Derive appropriate SHAs for base and head for `nx affected` commands
90+
uses: nrwl/nx-set-shas@v4
91+
- name: Run Linting
92+
run: npx nx affected -t lint
3493

35-
# Generate prisma
36-
- run: npx nx run-many -t prisma-generate
94+
test:
95+
runs-on: ubuntu-latest
96+
needs: install-dependencies
97+
steps:
98+
- uses: actions/checkout@v4
99+
with:
100+
fetch-depth: 0
101+
- uses: actions/setup-node@v4
102+
with:
103+
node-version-file: ".nvmrc"
104+
cache: "npm"
105+
- name: Load cached node_modules
106+
uses: actions/cache@v4
107+
id: cache-primes
108+
with:
109+
path: node_modules
110+
key: ${{ runner.os }}-nx-${{ hashFiles('package-lock.json') }}
111+
- name: Build libs
112+
run: npx nx run-many -t build-local
113+
- name: Prisma generate
114+
run: npx nx run-many -t prisma-generate
115+
- name: Derive appropriate SHAs for base and head for `nx affected` commands
116+
uses: nrwl/nx-set-shas@v4
117+
- name: Test
118+
run: npx nx affected -t test
37119

38-
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
39-
# - run: npx nx-cloud record -- echo Hello World
40-
- run: npx nx affected -t lint test build build-local
120+
build-and-push-collection-image:
121+
# Note that build is not a dependency of this job, as this will build the same stuff as the build job anyway
122+
# However no point in building if lint or test fails!
123+
needs:
124+
- lint
125+
- test
126+
# if: github.ref == 'refs/heads/main'
127+
runs-on: ubuntu-latest
128+
permissions:
129+
contents: read
130+
packages: write
131+
attestations: write
132+
id-token: write
133+
steps:
134+
- name: Checkout repository
135+
uses: actions/checkout@v4
136+
- name: Set up Docker Buildx
137+
uses: docker/setup-buildx-action@v3
138+
- name: Log in to the Container registry
139+
uses: docker/login-action@v3
140+
with:
141+
registry: ${{ env.REGISTRY }}
142+
username: ${{ github.actor }}
143+
password: ${{ secrets.GITHUB_TOKEN }}
144+
- name: Extract metadata (tags, labels) for Docker
145+
id: meta
146+
uses: docker/metadata-action@v5
147+
with:
148+
images: ${{ env.REGISTRY }}/${{ env.COLLECTION_IMAGE_NAME }}
149+
tags: |
150+
type=ref,event=branch
151+
type=ref,event=tag
152+
type=ref,event=pr
153+
type=sha
154+
type=raw,value=latest,enable={{is_default_branch}}
155+
- name: Build and push Docker image
156+
id: push
157+
uses: docker/build-push-action@v6
158+
with:
159+
context: .
160+
file: ./collection/Dockerfile
161+
push: true
162+
tags: ${{ steps.meta.outputs.tags }}
163+
labels: ${{ steps.meta.outputs.labels }}
164+
no-cache: true

.nvmrc

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

README.md

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,64 @@ The repo is structured as an Nx monorepo (I recommend you look at the [Nx docume
99

1010
## Tools
1111

12-
- `clickup/calendar-sync`: A rust tool that syncs the DoCSoc ClickUp calendar with the DoCSoc Google Calendar. It is designed to be run as a cron job on a server.
13-
- `common/util`: A set of common utilities used by other tools written in TypeScript
14-
- `email/mailmerge`: A TypeScript library that can be used to generate emails from templates and send them. It is designed to be used in conjunction with the `email/mailmerge-cli` tool, but can be used by itself
15-
- `email/mailmerge-cli`: A TypeScript CLI tool that can be used to generate emails from templates, regenerate them after modifying the results, upload them to Outlook drafts and send them.
12+
- `clickup/calendar-sync`: A rust tool that syncs the DoCSoc ClickUp calendar with the DoCSoc Google Calendar. It is designed to be run as a cron job on a server. (application)
13+
- `common/util`: A set of common utilities used by other tools written in TypeScript (library)
14+
- `email/mailmerge`: A TypeScript library that can be used to generate emails from templates and send them. It is designed to be used in conjunction with the `email/mailmerge-cli` tool, but can be used by itself (library)
15+
- `email/mailmerge-cli`: A TypeScript CLI tool that can be used to generate emails from templates, regenerate them after modifying the results, upload them to Outlook drafts and send them. (library & application)
16+
- `collection/`: A Next.js Application that allows us to manage merchandise collections securely. Designed to be ran as a docker container with a Postgres DB. (application)
1617

1718
Each tool's directory has a README with more information on how to use it.
1819

1920
## Building
2021

2122
### Build for development
2223

24+
> [!NOTE]
25+
> You should use `build-local` when working on just the TypeScript code, as `build` will build all tools (TypeScript and Rust) to `/dist/`, including Next.js production builds, whereas `build-local` only compiles TypeScript libraries like those in `common` and `mailmerge`, and does so in-place (instead of copying even libraries over to `/dist/`).
26+
2327
```
24-
npx nx run-many -t build build-local
28+
npx nx run-many -t build-local
29+
npx nx run-many -t build
2530
```
2631

2732
`build` builds all tools (TypeScript and Rust) to `/dist/`, `build-local` builds the typescript tools to `dist/` folders in **each tool's directory**.
2833

29-
You should use `build-local` when working on just the TypeScript code
34+
You should use `build-local` when working on just the TypeScript code.
35+
36+
You can run tasks for a specific tool by running, for example:
37+
38+
```bash
39+
npx nx build collection
40+
npx nx test mailmerge
41+
npx nx run eactivities:test # alternate syntax for running tasks
42+
# etc.
43+
```
44+
45+
Where `build` is the task and `collection` is the tool.
46+
47+
It may help to install the NX Console plugin to view all available tasks.
3048

3149
### Building for production
3250

3351
```
3452
npx nx run-many -t build
3553
```
3654

55+
## Making a release
56+
57+
NOTE: See Nuclino for cargo & npm registry login details.
58+
59+
It is assumed you have already logged into the npm and cargo registries.
60+
61+
```
62+
npx nx release
63+
```
64+
65+
This will build & release all libraries (so everything bar `collection`)
66+
3767
### Documentation
3868

39-
You can get API documentation for everything in the repo by running:
69+
You can get API documentation for all TypeScript libraries in the repo by running:
4070

4171
```
4272
npm run docs
@@ -58,10 +88,46 @@ npx nx run-many -t test
5888

5989
## Nx Mono repo info
6090

91+
### Rust
92+
6193
The plugin `@monodon/rust` has been added to Nx to allow for Rust projects to be built and run in the monorepo. This is used by the `calendar-sync` tool.
94+
6295
The plugin has create a Cargo workspace in the root of the monorepo as such, and all builds for all packages are done in the root of the monorepo and sent to `dist` in the repo root.
6396

64-
# Original Nx README
97+
### Docker
98+
99+
The NX plugin `@nx-tools/nx-container` has been added to allow for Docker images to be built and run in the monorepo. This is used by the `collection` tool.
100+
101+
E.g. to build `collection` as a Docker image `docsoc/collection`:
102+
103+
```bash
104+
npx nx container collection
105+
```
106+
107+
### Prisma
108+
109+
The NX plugin `@nx-tools/nx-prisma` has been added to allow the use of prisma.
110+
111+
Prisma command can be run following this pattern:
112+
113+
If the original command is, for example `prisma studio`, then the command to run it in the monorepo is `npx nx prisma-studio <project>`.
114+
E.g. to run `prisma studio` for the `collection` project:
115+
116+
```bash
117+
npx nx prisma-studio collection
118+
119+
# this also works:
120+
cd collection
121+
npx nx prisma-studio # autodetets the project
122+
```
123+
124+
# Weird Bits
125+
126+
### Q: Why does `collection` and `template` not have a `package.json`
127+
128+
This i because they pull their deps in from the root `package.json`: this way, we only need to maintain one dependency list for common deps like React, etc.
129+
130+
# Original Nx README (rad this to know how NX works)
65131

66132
<a alt="Nx logo" href="https://nx.dev" target="_blank" rel="noreferrer"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-logo.png" width="45"></a>
67133

collection/.env.local.template

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# =======================
2+
# Copy this file into .env.local and fill it in
3+
#
4+
# In production, set the appropriate environment variables on impaas (see README)
5+
# =======================
6+
# Secret for generates auth tokes (generate using `npx auth secret`)
7+
AUTH_SECRET= # Run `npx auth secret`. Read more: https://cli.authjs.dev
8+
9+
# MS Entra SSO details - see README
10+
MS_ENTRA_CLIENT_ID=
11+
MS_ENTRA_CLIENT_SECRET=
12+
MS_ENTRA_TENANT_ID=
13+
14+
# API Key from eActivities for getting data from it
15+
EACTIVITIES_API_KEY=
16+
# Centre number for eActivities (605 is for DoCSoc)
17+
EACTIVITIES_CENTRE_NUMBER="605"
18+
# Full email address of the root user that can always access the system
19+
# After first run, login as this user to add more users/load committee members
20+
ROOT_USER_EMAIL="[email protected]"
21+
22+
# Login details for postgres database
23+
# Replace "postgres:postgres" with "<username>:<password>"
24+
# And docsoc-tools-collection with the name of the database you are using
25+
COLLECTION_DATABASE_URL="postgres://postgres:postgres@localhost:5432/docsoc-tools-collection"

collection/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This template uses Automatically Copying Traced Files feature
22
# so you need to setup your Next Config file to use `output: 'standalone'`
33
# Please read this for more information https://nextjs.org/docs/pages/api-reference/next-config-js/output
4-
FROM node:18-alpine AS builder
4+
FROM node:20-alpine AS builder
55

66
ENV NEXT_TELEMETRY_DISABLED=1
77
WORKDIR /build
@@ -17,7 +17,11 @@ COPY jest* .
1717
COPY tsconfig.base.json tsconfig.base.json
1818
COPY collection collection
1919
COPY common common
20+
# Build libraries we need (will not be auto built)
21+
RUN npx nx run-many -t build-local
22+
# Generate Prisma Client (needed to be included in build)
2023
RUN npx nx run collection:prisma-generate
24+
# Build the collection code
2125
RUN npx nx run collection:build
2226

2327
# Production image, copy all the files and run next

collection/Dockerfile.dev

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Dockerfile for docker compose
2+
FROM node:lts
3+
4+
RUN npm i -g nx

collection/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# DoCSoc Collection System
2+
3+
This is a Next.js Application that allows us to manage merchandise collections securely. Designed to be ran as a docker container with a Postgres DB.
4+
5+
Once deployed, it allows us to control committee member access, import items from eActivities CSV exports, and mark item as collected.
6+
7+
# Why no `package.json`?
8+
9+
This folder has no package.json as it means we can share the same `package.json` as the rest of the monorepo. This means we can share dependencies like React and scripts across all app, rather than having to maintain multiple `package.json` files and React versions, etc.
10+
11+
Other tools have their own package.jsons as that was how they were originally set up.
12+
13+
# Quick Start
14+
15+
## Local
16+
17+
1. From the root of the repo run `npm install`
18+
2. Copy `.env.local.template` to `.env.local` and fill in the details
19+
3. Run from this dir `npx nx prisma-push` to create the database
20+
4. Run `npx nx run-many -t build-local` to build required monorepo libraries
21+
5. Run from this dir `npx nx dev` to start the dev server
22+
23+
## Docker
24+
25+
1. Copy `.env.local.template` to `.env.local` and fill in the details
26+
2. From the current dir (`collection`) run `docker compose -f dev.docker-compose.yml up`. This start a dev docker compose instance that has the source code _mounted_ into it: meaning you need not rebuild the docker image every time you make a change!
27+
3. Visit `http://localhost:3000` in your browser and login with the root user you setu in the `.env.local` file
28+
4. To run future DB migrations in docker compose run `docker exec -i $(docker ps -qf "name=docsoc-collection" | head -n1) npx nx prisma-migrate collection --name migration-name`
29+
30+
# Docker
31+
32+
To build the docker image, run `npx nx container collection` from the _root_ of the repo.

0 commit comments

Comments
 (0)