Skip to content

Commit

Permalink
feat: publish BundleMon platform docker image (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
LironEr authored Jan 4, 2025
1 parent 4d48627 commit 74a010b
Show file tree
Hide file tree
Showing 27 changed files with 568 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
Expand Down
62 changes: 52 additions & 10 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
packages:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

strategy:
fail-fast: false
Expand Down Expand Up @@ -44,7 +44,10 @@ jobs:
run: yarn lint-packages

website:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
defaults:
run:
working-directory: ./apps/website

steps:
- uses: actions/checkout@v4
Expand All @@ -59,21 +62,21 @@ jobs:
run: yarn

- name: Lint
working-directory: ./apps/website
run: yarn lint

- name: Build
working-directory: ./apps/website
run: yarn build

service:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
defaults:
run:
working-directory: ./apps/service

steps:
- uses: actions/checkout@v4

- name: Start mock services
working-directory: ./apps/service
run: yarn start:mock-services -d

- name: Setup Node.js
Expand All @@ -86,22 +89,61 @@ jobs:
run: yarn

- name: Test
working-directory: ./apps/service
run: yarn test

- uses: codecov/codecov-action@v1
with:
file: ./service/coverage/coverage-final.json

- name: Lint
working-directory: ./apps/service
run: yarn lint

- name: Build
working-directory: ./apps/service
run: yarn build

- name: Stop mock services
if: always()
working-directory: ./apps/service
run: yarn stop:mock-services

platform:
runs-on: ubuntu-22.04
defaults:
run:
working-directory: ./apps/platform

steps:
- uses: actions/checkout@v4

- name: Start mock services
run: yarn start:mock-services -d

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'yarn'

- name: Install dependencies
run: yarn

- name: Build image
env:
INPUT_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: yarn build:image

- name: Start container
run: yarn start:platform

- name: Lint
run: yarn lint

- name: Test
run: yarn test

- name: Stop container
if: always()
run: yarn stop:platform

- name: Stop mock services
if: always()
run: yarn stop:mock-services
64 changes: 64 additions & 0 deletions .github/workflows/publish-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: publish-docker

on:
push:
tags:
- platform@v*.*.*

concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

jobs:
publish-docker:
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
id-token: write # needed for provenance data generation
timeout-minutes: 15

steps:
- name: Login to Github Packages
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Extract app name
id: app_name
run: echo "value=$(echo $GITHUB_REF_NAME | cut -d '@' -f 1)" >> $GITHUB_OUTPUT

# validate the version is the same as the tag
- name: Validate version
run: |
ACTUAL_VERSION=$(jq -r '.version' packages/${{ steps.app_name.outputs.value }}/package.json)
EXPECTED_VERSION=$(echo $GITHUB_REF_NAME | sed 's/.*@v//')
if [ "$ACTUAL_VERSION" != "$EXPECTED_VERSION" ]; then
echo "Version mismatch between package.json ($ACTUAL_VERSION) and tag ($EXPECTED_VERSION)"
exit 1
fi
- name: Install Node
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
cache: yarn

- name: Install dependencies
run: yarn

- name: Set up Docker Buildx
uses: docker/[email protected]

- name: Publish
env:
INPUT_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx nx container ${{ steps.app_name.outputs.value }} --configuration production --verbose
3 changes: 2 additions & 1 deletion .github/workflows/publish-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
jobs:
test:
name: publish-packages
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
permissions:
contents: read
id-token: write # needed for provenance data generation
Expand Down Expand Up @@ -40,6 +40,7 @@ jobs:
with:
node-version: 20
registry-url: https://registry.npmjs.org/
cache: yarn

- name: Install dependencies
run: yarn
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '18'
node-version: '20'

- name: Install dependencies
run: yarn
Expand Down
9 changes: 9 additions & 0 deletions apps/platform/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore everything
*

# Allow files and directories
!/dist

# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/.DS_Store
7 changes: 7 additions & 0 deletions apps/platform/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}
16 changes: 16 additions & 0 deletions apps/platform/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:20-alpine

ENV HOST=0.0.0.0 PORT=3333 SHOULD_SERVE_WEBSITE=true NODE_ENV=production

WORKDIR /app

RUN addgroup --system service && adduser --system -G service service

# Needed by @fastify/secure-session & for source maps
RUN npm i [email protected] [email protected]

COPY dist service

RUN chown -R service:service .

CMD [ "node", "-r", "source-map-support/register", "service/app.js" ]
3 changes: 3 additions & 0 deletions apps/platform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# BundleMon platform

BundleMon platform docker image contains both the service & the website (UI) of BundleMon.
6 changes: 6 additions & 0 deletions apps/platform/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Config } from '@jest/types';

export default async (): Promise<Config.InitialOptions> => ({
displayName: 'platform',
preset: '../../jest.preset.js',
});
17 changes: 17 additions & 0 deletions apps/platform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "bundlemon-platform",
"version": "1.0.0",
"private": true,
"license": "MIT",
"scripts": {
"build:image": "yarn --cwd ../../ nx container platform --verbose",
"test": "yarn --cwd ../../ nx test platform --verbose",
"lint": "yarn --cwd ../../ nx lint platform --verbose",
"start:mock-services": "docker compose -f ../service/docker-compose.test.yml up --remove-orphans",
"stop:mock-services": "docker compose -f ../service/docker-compose.test.yml down",
"start:platform": "docker run --rm -d --name bundlemon-platform --env-file ../service/.development.env -e NODE_ENV=producation -e MONGO_DB_NAME=test -p 3333:3333 bundlemon-platform",
"stop:platform": "docker stop bundlemon-platform"
},
"dependencies": {},
"devDependencies": {}
}
64 changes: 64 additions & 0 deletions apps/platform/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "platform",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/platform",
"projectType": "application",
"tags": [],
"targets": {
"prepare-image": {
"dependsOn": ["service:build", "website:build"],
"command": "rm -rf apps/platform/dist && cp -r apps/service/dist apps/platform/dist && cp -r dist/apps/website/* apps/platform/dist/public"
},
"container": {
"executor": "@nx-tools/nx-container:build",
"dependsOn": ["prepare-image"],
"defaultConfiguration": "local",
"options": {
"engine": "docker",
"context": "apps/platform",
"file": "apps/platform/Dockerfile",
"metadata": {
"images": ["ghcr.io/lironer/bundlemon-platform"],
"load": true,
"flavor": ["latest=auto"],
"tags": [
"type=semver,pattern={{version}}",
"type=semver,pattern={{major}}.{{minor}}",
"type=semver,pattern=v{{major}}"
],
"cache-from": ["type=registry,ref=ghcr.io/lironer/bundlemon-platform:latest"],
"cache-to": ["type=inline"]
}
},
"configurations": {
"local": {
"metadata": {
"images": ["bundlemon-platform"],
"tags": ["latest"]
},
"push": false
},
"production": {
"platforms": ["linux/amd64", "linux/arm64"],
"provenance": "true",
"push": true
}
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["{projectRoot}/**/*.{ts,js,json}"],
"maxWarnings": 0
}
},
"test": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "{projectRoot}/jest.config.ts",
"runInBand": true
}
}
}
}
1 change: 1 addition & 0 deletions apps/platform/tests/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BASE_URL = 'http://localhost:3333';
7 changes: 7 additions & 0 deletions apps/platform/tests/isAlive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BASE_URL } from './consts';

test('is alive', async () => {
const response = await fetch(`${BASE_URL}/is-alive`);

expect(response.status).toEqual(200);
});
17 changes: 17 additions & 0 deletions apps/platform/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "ES2022",
"baseUrl": ".",
"paths": {
"@tests/*": ["./tests/*"]
}
},
"references": [
{
"path": "./tsconfig.spec.json"
}
],
"files": [],
"include": []
}
8 changes: 8 additions & 0 deletions apps/platform/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": ["jest", "node"]
},
"include": ["jest.config.ts", "tests/**/*", "scripts/**/*"]
}
2 changes: 1 addition & 1 deletion apps/service/.development.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ MONGO_DB_NAME=dev
MONGO_DB_USER=dev-user
MONGO_DB_PASSWORD=password
ROOT_DOMAIN=localhost
SECRET_SESSION_KEY="74925e5027a05d9e31082271747a92b11a3b6988fc303bbb2aae330bef92b3a7"
SECRET_SESSION_KEY=74925e5027a05d9e31082271747a92b11a3b6988fc303bbb2aae330bef92b3a7
Loading

0 comments on commit 74a010b

Please sign in to comment.