Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
ci: build every image once per commit on main (build-images.yml) (#1691)
Sid702dfa72b38b
diff --git a/.github/scripts/build-images.sh b/.github/scripts/build-images.shnew file mode 100755index 0000000000..a88d2411f0--- /dev/null+++ b/.github/scripts/build-images.sh@@ -0,0 +1,109 @@+#!/usr/bin/env bash+# Plans one image for build-images.yml: build it, re-tag the previous+# commit's image (its files did not change), or skip (already tagged).+# Writes action, image, from, file, target and args to $GITHUB_OUTPUT, and+# for an assembler build, occt and occt_build: the OCCT base it links, and+# whether that base has to be built first.+#+# REGISTRY=… SHA=… [BEFORE=…] build-images.sh <image>+#+# REGISTRY is any OCI registry the runner is logged into, with an optional+# namespace: ghcr.io/acme, docker.io/acme, 123456789012.dkr.ecr.….+set -euo pipefail++name=${1:?usage: build-images.sh <image>}+image="$REGISTRY/carbon/$name"+target=""+args=""++# erp, mes and ops share the root Dockerfile's deps stage.+node=(Dockerfile .dockerignore package.json pnpm-lock.yaml pnpm-workspace.yaml+ .npmrc turbo.json lingui.config.js apps/erp apps/mes packages patches scripts)++case "$name" in+ erp | mes)+ file=Dockerfile+ args="APP=$name"$'\n'"NODE_OPTIONS=--max-old-space-size=4096"+ paths=("${node[@]}")+ ;;+ ops)+ file=Dockerfile+ target=ops+ paths=("${node[@]}")+ ;;+ edge-functions)+ file=docker/edge-functions/Dockerfile+ paths=(docker/edge-functions packages/database/supabase/functions)+ ;;+ assembler)+ file=apps/assembler/Dockerfile+ # Tagged by what it is built from, so an unchanged base is never rebuilt+ # and a changed one never reused.+ occt_tree=$(git ls-tree -r "$SHA" -- apps/assembler/occt.Dockerfile apps/assembler/occt-patches)+ [[ -n $occt_tree ]] || { echo "no OCCT sources at $SHA" >&2; exit 1; }+ occt_src=$(git hash-object --stdin <<<"$occt_tree")+ occt="$REGISTRY/carbon/occt:${occt_src:0:12}"+ args="OCCT_IMAGE=$occt"+ paths=(.dockerignore apps/assembler crates Cargo.toml Cargo.lock)+ ;;+ *)+ echo "unknown image: $name" >&2+ exit 1+ ;;+esac++out() { echo "$1=$2" >>"$GITHUB_OUTPUT"; }+exists() { docker buildx imagetools inspect "$1" >/dev/null 2>&1; }+has_tag() { exists "$image:$1"; }++# ECR is the one registry that will not create a repository on first push.+ensure_repo() {+ [[ $REGISTRY =~ \.dkr\.ecr\.[a-z0-9-]+\.amazonaws\.com ]] || return 0+ local repo=${1#*/}+ aws ecr describe-repositories --repository-names "$repo" >/dev/null 2>&1 ||+ aws ecr create-repository --repository-name "$repo" \+ --image-scanning-configuration scanOnPush=true >/dev/null+}++ensure_repo "$image"++out image "$image"+out file "$file"+out target "$target"+printf 'args<<EOF\n%s\nEOF\n' "$args" >>"$GITHUB_OUTPUT"++if has_tag "$SHA"; then+ echo "$image:$SHA exists"+ out action skip+ exit 0+fi++# A manual run has no BEFORE; a push of several commits puts it outside+# the shallow checkout.+before=${BEFORE:-}+if [[ -z $before || $before =~ ^0+$ ]]; then+ before=$(git rev-parse "$SHA^" 2>/dev/null || true)+fi+if [[ -n $before ]]; then+ git fetch --quiet --depth=1 origin "$before" 2>/dev/null || true+fi++if [[ -n $before ]] && git cat-file -e "$before^{commit}" 2>/dev/null &&+ git diff --quiet "$before" "$SHA" -- "${paths[@]}" && has_tag "$before"; then+ echo "unchanged since $before: re-tagging"+ out action retag+ out from "$before"+else+ echo "building"+ out action build+ if [[ -n ${occt:-} ]]; then+ out occt "$occt"+ if exists "$occt"; then+ out occt_build false+ else+ echo "no $occt: building it first"+ ensure_repo "${occt%:*}"+ out occt_build true+ fi+ fi+fidiff --git a/.github/workflows/build-images.yml b/.github/workflows/build-images.ymlnew file mode 100644index 0000000000..f5174a9546--- /dev/null+++ b/.github/workflows/build-images.yml@@ -0,0 +1,131 @@+name: Build images++# Gives every commit on main the complete image set, tagged :<full sha>, so+# any commit can become a release. Images whose files did not change are+# re-tagged from the previous commit instead of rebuilt. Forks run this+# into their own registry.+#+# Configure vars.IMAGE_REGISTRY — any OCI registry, with an optional+# namespace: ghcr.io/acme, docker.io/acme, 123456789012.dkr.ecr.<region>.+# amazonaws.com. Unset, nothing runs. Then credentials for it:+# GHCR none; the workflow token pushes to this repository's packages+# ECR vars.IMAGE_AWS_REGION, and vars.IMAGE_AWS_ROLE_ARN (OIDC) or+# secrets.IMAGE_AWS_ACCESS_KEY_ID + IMAGE_AWS_SECRET_ACCESS_KEY+# other secrets.IMAGE_REGISTRY_USERNAME + IMAGE_REGISTRY_PASSWORD++on:+ push:+ branches: [main]+ workflow_dispatch:++# Serial, so each run finds the previous commit's tags already written.+concurrency:+ group: build-images+ cancel-in-progress: false++permissions:+ contents: read+ id-token: write+ packages: write++env:+ REGISTRY: ${{ vars.IMAGE_REGISTRY }}++jobs:+ image:+ name: carbon/${{ matrix.image }}+ if: vars.IMAGE_REGISTRY != ''+ runs-on: ubuntu-latest+ strategy:+ fail-fast: false+ matrix:+ image: [erp, mes, ops, edge-functions, assembler]+ steps:+ - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0+ with:+ fetch-depth: 2++ - if: vars.IMAGE_AWS_REGION != ''+ uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a # v4.3.1+ with:+ aws-region: ${{ vars.IMAGE_AWS_REGION }}+ role-to-assume: ${{ vars.IMAGE_AWS_ROLE_ARN }}+ aws-access-key-id: ${{ secrets.IMAGE_AWS_ACCESS_KEY_ID }}+ aws-secret-access-key: ${{ secrets.IMAGE_AWS_SECRET_ACCESS_KEY }}++ - id: host+ run: echo "host=${REGISTRY%%/*}" >> "$GITHUB_OUTPUT"++ # ECR logs in from the AWS credentials above when no password is given.+ - uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0+ with:+ registry: ${{ steps.host.outputs.host }}+ username: ${{ secrets.IMAGE_REGISTRY_USERNAME || (steps.host.outputs.host == 'ghcr.io' && github.actor) || '' }}+ password: ${{ secrets.IMAGE_REGISTRY_PASSWORD || (steps.host.outputs.host == 'ghcr.io' && secrets.GITHUB_TOKEN) || '' }}++ - id: buildx+ uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0++ - id: plan+ run: .github/scripts/build-images.sh ${{ matrix.image }}+ env:+ SHA: ${{ github.sha }}+ BEFORE: ${{ github.event.before }}++ - if: steps.plan.outputs.action == 'retag'+ run: docker buildx imagetools create --tag "$IMAGE:$SHA" "$IMAGE:$FROM"+ env:+ IMAGE: ${{ steps.plan.outputs.image }}+ FROM: ${{ steps.plan.outputs.from }}+ SHA: ${{ github.sha }}++ # The assembler links a prebuilt OCCT base; built here only when this+ # registry does not have the one its sources describe.+ - if: steps.plan.outputs.occt_build == 'true'+ uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0+ with:+ context: apps/assembler+ file: apps/assembler/occt.Dockerfile+ push: true+ tags: ${{ steps.plan.outputs.occt }}+ platforms: linux/amd64+ provenance: false++ # The Dockerfiles' cache mounts (the pnpm store, Turborepo's cache)+ # start empty on every runner; carry them between runs. Keyed by+ # commit and restored from the newest, so each run saves what it adds.+ - if: steps.plan.outputs.action == 'build'+ uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0+ with:+ path: cache-mounts+ key: cache-mounts-${{ matrix.image }}-${{ github.sha }}+ restore-keys: cache-mounts-${{ matrix.image }}-++ - if: steps.plan.outputs.action == 'build'+ uses: reproducible-containers/buildkit-cache-dance@5422eac04292c961a382e0f584ea0f03ad9da723 # v3.4.0+ with:+ builder: ${{ steps.buildx.outputs.name }}+ cache-dir: cache-mounts+ dockerfile: ${{ steps.plan.outputs.file }}++ - if: steps.plan.outputs.action == 'build'+ uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0+ with:+ context: .+ file: ${{ steps.plan.outputs.file }}+ target: ${{ steps.plan.outputs.target }}+ build-args: ${{ steps.plan.outputs.args }}+ # source links a GHCR package to this repository, which is what+ # lets the workflow token keep pushing to it.+ labels: |+ org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}+ org.opencontainers.image.revision=${{ github.sha }}+ push: true+ tags: ${{ steps.plan.outputs.image }}:${{ github.sha }}+ platforms: linux/amd64+ provenance: false+ # In the registry beside the image, not GitHub's cache: that holds+ # 10 GB a repository, which five images outgrow. The ECR options+ # are what ECR needs to store a cache manifest.+ cache-from: type=registry,ref=${{ steps.plan.outputs.image }}:buildcache+ cache-to: type=registry,ref=${{ steps.plan.outputs.image }}:buildcache,mode=max,image-manifest=true,oci-mediatypes=truediff --git a/docker/edge-functions/Dockerfile b/docker/edge-functions/Dockerfilenew file mode 100644index 0000000000..f40ee2c5a6--- /dev/null+++ b/docker/edge-functions/Dockerfile@@ -0,0 +1,49 @@+# Carbon edge-functions image: the supabase/edge-runtime base with the+# Carbon edge functions baked in at build time, so deploying functions is+# "build, push, helm upgrade" rather than a host bind mount kept in sync.+#+# Build from the repo root — it reads packages/database/supabase/functions:+# docker build -f docker/edge-functions/Dockerfile -t carbon/edge-functions .+#+# In GovCloud, BASE_IMAGE must point at an ECR mirror of+# supabase/edge-runtime — never Docker Hub.+ARG BASE_IMAGE=supabase/edge-runtime:v1.69.6+ARG DENO_IMAGE=denoland/deno:1.45.2++# --- Module cache warm stage ---------------------------------------------+# The functions import remote modules (deno.land, npm:). Fetching them at+# build time pins what ships and keeps first-invocation latency and runtime+# egress out of the boundary. Failures are tolerated per function (some+# graphs only resolve with env present) — an uncached module falls back to+# a runtime fetch.+FROM ${DENO_IMAGE} AS cache+ENV DENO_DIR=/deno-cache+COPY docker/edge-functions/main /src/main+COPY packages/database/supabase/functions /src/functions+RUN for f in /src/main/index.ts /src/functions/*/index.ts; do \+ [ -f "$f" ] && deno cache "$f" || true; \+ done++# --- Final image ----------------------------------------------------------+FROM ${BASE_IMAGE}++# The upstream edge-runtime base trails Debian bookworm on openssl, gnutls,+# libpam and perl, which scanners flag as fixable CRITICAL/HIGH. Pulling the+# published fixes clears them at the source rather than suppressing them.+USER root+RUN apt-get update \+ && apt-get upgrade -y \+ && rm -rf /var/lib/apt/lists/*++# The main dispatcher (vendored from crbnos/supabase volumes/functions/main).+COPY docker/edge-functions/main /home/deno/functions/main++# The Carbon functions themselves.+COPY packages/database/supabase/functions /home/deno/functions++# Pre-warmed module cache; DENO_DIR points the runtime's module loader at it.+COPY --from=cache /deno-cache /home/deno/.deno-cache+ENV DENO_DIR=/home/deno/.deno-cache++EXPOSE 9000+CMD ["start", "--main-service", "/home/deno/functions/main"]diff --git a/docker/edge-functions/main/index.ts b/docker/edge-functions/main/index.tsnew file mode 100644index 0000000000..0981a59d88--- /dev/null+++ b/docker/edge-functions/main/index.ts@@ -0,0 +1,108 @@+import { serve } from 'https://deno.land/[email protected]/http/server.ts'+import * as jose from 'https://deno.land/x/[email protected]/index.ts'++console.log('main function started')++const JWT_SECRET = Deno.env.get('JWT_SECRET')+const VERIFY_JWT = Deno.env.get('VERIFY_JWT') === 'true'++// An empty key would accept any token signed with an empty key.+if (VERIFY_JWT && !JWT_SECRET) {+ throw new Error('VERIFY_JWT is on but JWT_SECRET is empty')+}++function getAuthToken(req: Request) {+ const authHeader = req.headers.get('authorization')+ if (!authHeader) {+ throw new Error('Missing authorization header')+ }+ const [bearer, token] = authHeader.split(' ')+ if (bearer !== 'Bearer') {+ throw new Error(`Auth header is not 'Bearer {token}'`)+ }+ return token+}++async function verifyJWT(jwt: string): Promise<boolean> {+ const encoder = new TextEncoder()+ const secretKey = encoder.encode(JWT_SECRET)+ try {+ await jose.jwtVerify(jwt, secretKey)+ } catch (err) {+ console.error(err)+ return false+ }+ return true+}++serve(async (req: Request) => {+ if (req.method !== 'OPTIONS' && VERIFY_JWT) {+ try {+ const token = getAuthToken(req)+ const isValidJWT = await verifyJWT(token)++ if (!isValidJWT) {+ return new Response(JSON.stringify({ msg: 'Invalid JWT' }), {+ status: 401,+ headers: { 'Content-Type': 'application/json' },+ })+ }+ } catch (e) {+ console.error(e)+ return new Response(JSON.stringify({ msg: e.toString() }), {+ status: 401,+ headers: { 'Content-Type': 'application/json' },+ })+ }+ }++ const url = new URL(req.url)+ const { pathname } = url+ const path_parts = pathname.split('/')+ const service_name = path_parts[1]++ if (!service_name || service_name === '') {+ const error = { msg: 'missing function name in request' }+ return new Response(JSON.stringify(error), {+ status: 400,+ headers: { 'Content-Type': 'application/json' },+ })+ }++ // main is this dispatcher. Served as a function, it would start itself,+ // and that copy would start another, until the runtime gave out.+ if (service_name === 'main') {+ return new Response(JSON.stringify({ msg: 'function not found' }), {+ status: 404,+ headers: { 'Content-Type': 'application/json' },+ })+ }++ const servicePath = `/home/deno/functions/${service_name}`+ console.error(`serving the request with ${servicePath}`)++ const memoryLimitMb = 150+ const workerTimeoutMs = 1 * 60 * 1000+ const noModuleCache = false+ const importMapPath = null+ const envVarsObj = Deno.env.toObject()+ const envVars = Object.keys(envVarsObj).map((k) => [k, envVarsObj[k]])++ try {+ const worker = await EdgeRuntime.userWorkers.create({+ servicePath,+ memoryLimitMb,+ workerTimeoutMs,+ noModuleCache,+ importMapPath,+ envVars,+ })+ return await worker.fetch(req)+ } catch (e) {+ const error = { msg: e.toString() }+ return new Response(JSON.stringify(error), {+ status: 500,+ headers: { 'Content-Type': 'application/json' },+ })+ }+})