Skip to content

Commit

Permalink
Refactor lawndon-pi build and deployment (#22)
Browse files Browse the repository at this point in the history
* Refactor builds

* Refactor lawndon-pi build and deployment
  • Loading branch information
jordojordo authored Nov 23, 2024
1 parent d2beec6 commit 14e0b36
Show file tree
Hide file tree
Showing 20 changed files with 465 additions and 281 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/build-lawndon.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Build Lawndon

on:
workflow_dispatch:
release:
types: [released]

jobs:
build-lawndon:
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Arduino cli
uses: arduino/setup-arduino-cli@v2

- name: Install libraries
run: |
arduino-cli lib install IBusBM Servo
- name: Compile Lawndon
uses: arduino/compile-sketches@v1
with:
fqbn: "arduino:avr:mega"
libraries: |
- name: IBusBM
- name: Servo
sketch-paths: |
- ./lawndon
enable-warnings-report: true
verbose: false
cli-compile-flags: |
- --export-binaries
- name: Package build
if: startsWith(github.ref, 'refs/tags/')
run: |
for dir in ./lawndon/build/*; do
if [ -d "${dir}" ]; then
tar -czvf "${dir}.tar.gz" -C "${dir}" .
fi
done
- name: Release build
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
lawndon/build/*.tar.gz
build-lawndon-pi:
runs-on: ubuntu-latest
needs: build-lawndon
env:
WORKING_DIR: ./pi
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up QEMU for multi-platform builds
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-flags: --allow-insecure-entitlement security.insecure
install: true

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push server image
uses: docker/build-push-action@v4
working-directory: ${{ env.WORKING_DIR }}
with:
context: .
file: ./Dockerfile.server
push: true
platforms: linux/amd64,linux/arm64
tags: |
ghcr.io/${{ github.repository_owner }}/lawndon-pi-server:${{ github.ref_name }}
ghcr.io/${{ github.repository_owner }}/lawndon-pi-server:${{ github.sha }}
- name: Build and push UI image
uses: docker/build-push-action@v4
working-directory: ${{ env.WORKING_DIR }}
with:
context: .
file: ./Dockerfile.ui
push: true
platforms: linux/amd64,linux/arm64
tags: |
ghcr.io/${{ github.repository_owner }}/lawndon-pi-ui:${{ github.ref_name }}
ghcr.io/${{ github.repository_owner }}/lawndon-pi-ui:${{ github.sha }}
82 changes: 0 additions & 82 deletions .github/workflows/build-lawndon.yml

This file was deleted.

12 changes: 6 additions & 6 deletions .github/workflows/tests.yml → .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- cron: "0 4 * * 4"

jobs:
build-lawndon:
build-lawndon-test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
cli-compile-flags: |
- --export-binaries
package-lawndon-pi:
build-lawndon-pi-test:
runs-on: ubuntu-latest
env:
WORKING_DIR: ./pi
Expand Down Expand Up @@ -79,12 +79,12 @@ jobs:
run: |
pnpm build:server
- name: Run package script
- name: Test Docker build for Server
working-directory: ${{ env.WORKING_DIR }}
run: |
./package.sh
docker build --no-cache --file ./Dockerfile.server --tag lawndon-pi-server-test .
- name: Verify package contents
- name: Test Docker build for UI
working-directory: ${{ env.WORKING_DIR }}
run: |
tar -df lawndon-pi.tar.gz lawndon-pi
docker build --no-cache --file ./Dockerfile.ui --tag lawndon-pi-ui-test .
27 changes: 27 additions & 0 deletions pi/Dockerfile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Build stage
FROM node:22-alpine AS build
WORKDIR /usr/src/app

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV NODE_ENV=production
ENV CONFIG_PATH=/usr/src/app/config

RUN corepack enable
COPY server/package*.json ./
RUN pnpm install

COPY server ./server
WORKDIR /usr/src/app/server
RUN pnpm build

# Runtime stage
FROM node:22-alpine
WORKDIR /usr/src/app

COPY --from=build /usr/src/app/server/dist ./dist
COPY --from=build /usr/src/app/server/node_modules ./node_modules
COPY config ./config

EXPOSE 5000 8080
CMD ["node", "dist/server.js"]
22 changes: 22 additions & 0 deletions pi/Dockerfile.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Build stage
FROM node:22-alpine AS build
WORKDIR /usr/src/app

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV VITE_NODE_ENV=production

RUN corepack enable
COPY ui/package*.json ./
RUN pnpm install

COPY ui ./ui
WORKDIR /usr/src/app/ui
RUN pnpm build

# Runtime stage
FROM nginx:alpine
COPY --from=build /usr/src/app/ui/dist /usr/share/nginx/html

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
28 changes: 28 additions & 0 deletions pi/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
server:
image: ghcr.io/jordojordo/lawndon-pi-server:latest
environment:
NODE_ENV: production
CONFIG_PATH: /usr/src/app/config
ports:
- "5000:5000" # WebSocket and API
- "8080:8080" # ESP32 module communication
volumes:
- ./config:/usr/src/app/config:ro # Mount config for anchor positions
networks:
- app_network

ui:
image: ghcr.io/jordojordo/uwb-ui:latest
environment:
VITE_NODE_ENV: production
ports:
- "80:80" # Expose UI
depends_on:
- server
networks:
- app_network

networks:
app_network:
driver: bridge
3 changes: 2 additions & 1 deletion pi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "lawndon-pi",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"install:ui": "pnpm install --prefix ui",
"install:server": "pnpm install --prefix server",
Expand All @@ -10,7 +11,7 @@
"start": "node server/dist/server.js",
"dev": "concurrently -k -n SERVER,UI \"pnpm dev:server\" \"pnpm dev:ui\"",
"dev:ui": "pnpm --prefix ui dev",
"dev:server": "NODE_ENV=development pnpm --prefix server dev"
"dev:server": "pnpm --prefix server dev"
},
"packageManager": "pnpm@9.14.2",
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions pi/server/.env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NODE_ENV="development"
CONFIG_PATH="../../config"
2 changes: 2 additions & 0 deletions pi/server/.env.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NODE_ENV="production"
CONFIG_PATH="../config"
1 change: 0 additions & 1 deletion pi/server/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default [
'newline-per-chained-call': ['warn', { 'ignoreChainWithDepth': 4 }],
'no-caller': 'warn',
'no-cond-assign': ['warn', 'except-parens'],
'no-console': 'warn',
'no-debugger': 'warn',
'no-eq-null': 'warn',
'no-eval': 'warn',
Expand Down
8 changes: 5 additions & 3 deletions pi/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "uwb-server",
"version": "0.1.0",
"description": "",
"main": "src/server.js",
"main": "dist/server.js",
"type": "module",
"scripts": {
"start": "node dist/server.js",
"build": "tsc --outDir dist",
"dev": "nodemon --watch src --ext ts --exec 'node --loader ts-node/esm ./src/server.ts'",
"dev": "NODE_ENV=development nodemon --watch src --ext ts --exec 'node --loader ts-node/esm ./src/server.ts'",
"lint": "npx eslint .",
"format": "npx eslint . --fix"
},
Expand All @@ -17,14 +17,16 @@
"packageManager": "pnpm@9.14.2",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"mathjs": "^14.0.0",
"socket.io": "^4.8.1"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/dotenv": "^8.2.3",
"@types/express": "^5.0.0",
"@types/node": "^22.9.1",
"@types/node": "^22.9.3",
"eslint": "^9.15.0",
"globals": "^15.12.0",
"http-proxy-middleware": "^3.0.3",
Expand Down
Loading

0 comments on commit 14e0b36

Please sign in to comment.