Skip to content

Commit

Permalink
Build docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Aug 9, 2024
1 parent 863e715 commit b9036ef
Show file tree
Hide file tree
Showing 7 changed files with 1,176 additions and 27 deletions.
40 changes: 38 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ name: Build

on:
push:
branches: [main]
branches: [main, docker]

permissions:
id-token: write
contents: read

jobs:
test-core:
Expand Down Expand Up @@ -37,4 +41,36 @@ jobs:

- name: Run ESLint
run: pnpm lint
working-directory: ./core
working-directory: ./core

build-local:
name: Build local docker image
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_GH_ASSUME_ROLE }}
role-session-name: github-actions-bemi-local
aws-region: us-east-1

- name: Login to Amazon ECR Public
id: login-ecr-public
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public

- name: Build, tag, and push image to Amazon ECR
env:
REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
REGISTRY_ALIAS: u4i4s8t6
REPOSITORY: bemi-local
IMAGE_TAG: latest
run: |
cd worker
mv ../core ./core
docker build -t $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG
58 changes: 38 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ Bemi automatically tracks database changes ensuring 100% reliability and a compr

- [Highlights](#highlights)
- [Use cases](#use-cases)
- [System dependencies](#system-dependencies)
- [Quickstart](#quickstart)
- [Running with Docker](#running-with-docker)
- [Running with Devbox](#running-with-devbox)
- [Running with Natively](#running-with-natively)
- [Contextualizing data changes](#contextualizing-data-changes)
- [Architecture](#architecture)
- [Testing](#testing)
- [License](#license)
Expand All @@ -54,51 +59,62 @@ There's a wide range of use cases that Bemi is built for! The tech was initially
- **Testing:** Rollback or roll-forward to different application test states.
- **Analyzing Trends:** Gain insights into historical trends and changes for informed decision-making.

## Quickstart

### System dependencies
## System dependencies

- [Node.js](https://github.com/nodejs/node)
- [NATS server](https://github.com/nats-io/nats-server)

You can install these system dependencies manually or use [Devbox](https://github.com/jetpack-io/devbox) that relies on
[Nix Packages](https://github.com/NixOS/nixpkgs) for providing isolated shells without containerization.

And of course, you need a PostgreSQL database that you want to connect to to track data changes. Make sure your database has `SHOW wal_level;` returning `logical`. Otherwise, you need to run the following SQL command and restart your PostgreSQL server:

```sql
ALTER SYSTEM SET wal_level = logical;
```

### Installation
## Quickstart

After installing all system dependencies, install all project dependencies with Node.js:
### Running with Docker

```sh
make worker-setup && cd worker && npm install
```
...

### Running with Devbox

Alternatively, you can use Devbox instead and run a single command that will also install Node.js with pnpm and NATS server:
You can install all system dependencies manually or use [Devbox](https://github.com/jetpack-io/devbox) that relies on
[Nix Packages](https://github.com/NixOS/nixpkgs) for providing isolated shells without containerization.
Run a single command that will install Node.js with pnpm and NATS server:

```sh
make worker-install
```

### Data change tracking

Set environment variables specifying connection settings for a PostgreSQL database you want to track:
Set environment variables specifying connection settings for a PostgreSQL database you want to track and run a worker:

```sh
export DB_HOST=127.0.0.1 DB_PORT=5432 DB_NAME=postgres DB_USER=postgres DB_PASSWORD=postgres
make worker-up
```

Run a worker as a single process with directly installed Node.js:
Now try making some database changes like:

```sql
UPDATE _bemi_migrations SET executed_at = NOW() WHERE id = 1;
```

This will add a new record in the `changes` table within the same database after a few seconds.

### Running natively

After installing all system dependencies, install all project dependencies with Node.js:

```sh
cd worker && npm concurrently -- "npm:up:*"
make worker-setup && cd worker && npm install
```

# Alternatively, with Devbox
make worker-up
Set environment variables specifying connection settings for a PostgreSQL database you want to track run a worker as a single process with directly installed Node.js:


```sh
export DB_HOST=127.0.0.1 DB_PORT=5432 DB_NAME=postgres DB_USER=postgres DB_PASSWORD=postgres
npm concurrently -- "npm:up:*"
```

Now try making some database changes like:
Expand All @@ -109,7 +125,9 @@ UPDATE _bemi_migrations SET executed_at = NOW() WHERE id = 1;

This will add a new record in the `changes` table within the same database after a few seconds.

To optionally automatically enhance these low-level database changes with application-specific context (e.g., user ID, API endpoint, etc.), check out our compatible [ORM packages](https://docs.bemi.io/#supported-orms).
## Contextualizing data changes

Optionally, to automatically enhance these low-level database changes with application-specific context (e.g., user ID, API endpoint, etc.), check out our compatible [ORM packages](https://docs.bemi.io/#supported-orms).

## Architecture

Expand Down
40 changes: 40 additions & 0 deletions worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM --platform=linux/amd64 node:21-bookworm AS base

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN npm install -g pnpm@8.8.0

WORKDIR /app

COPY package.json pnpm-lock.yaml ./

################################################################################

FROM base AS deps

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

################################################################################

FROM base AS build

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

COPY . ./

RUN mv ./core ../core && \
pnpm run build

################################################################################

FROM base

RUN curl -O https://repo1.maven.org/maven2/io/debezium/debezium-server-dist/2.5.0.Final/debezium-server-dist-2.5.0.Final.tar.gz && \
tar -xvf debezium-server-dist-2.5.0.Final.tar.gz && \
rm debezium-server-dist-2.5.0.Final.tar.gz && \
curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@v2.10.9 | sh

COPY --from=deps /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist

CMD ["pnpm", "run", "concurrently -- \"pnpm:up-built:*\""]
4 changes: 2 additions & 2 deletions worker/mikro-orm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const mikroOrmConfig: Options = {
highlighter: new SqlHighlighter(),
debug: true,
allowGlobalContext: true,
entities: ['../core/src/entities/**/*.js'],
entities: ['./dist/core/src/entities/**/*.js'],
entitiesTs: ['../core/src/entities/**/*.ts'],
migrations: {
path: '../core/src/migrations',
path: './dist/core/src/migrations',
pathTs: '../core/src/migrations',
tableName: '_bemi_migrations',
},
Expand Down
9 changes: 7 additions & 2 deletions worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
"devDependencies": {
"@types/node": "^20.11.16",
"concurrently": "^8.2.2",
"ts-node": "^10.9.2"
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
},
"scripts": {
"build": "pnpm tsc",
"up:worker": "sleep 5 && LOG_LEVEL=DEBUG pnpm ts-node src/index.ts",
"up:nats": "nats-server -js",
"up:debezium": "sleep 1 && pnpm ts-node src/reset.ts && ./debezium.sh"
"up:debezium": "sleep 1 && pnpm ts-node src/reset.ts && ./debezium.sh",
"up-built:worker": "sleep 5 && LOG_LEVEL=DEBUG node dist/worker/src/index.js",
"up-built:nats": "nats-server -js",
"up-built:debezium": "sleep 1 && node dist/worker/src/reset.js && ./debezium.sh"
}
}
Loading

0 comments on commit b9036ef

Please sign in to comment.