From 6c0aea6dd253c34be86425c3e205ca06ed5ff601 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Wed, 30 Aug 2023 16:33:08 -0400 Subject: [PATCH] feat: try to quicken builds by using docker and sharing image --- .github/workflows/pull-request.yml | 120 +++++++++++++---------------- api/Dockerfile | 17 +++- api/dev/Unraid.net/myservers.cfg | 2 +- api/dev/states/myservers.cfg | 2 +- api/docker-compose.yml | 12 +-- api/package-lock.json | 31 +++----- api/package.json | 2 - api/src/cli.ts | 11 +++ api/src/cli/index.ts | 9 --- 9 files changed, 97 insertions(+), 109 deletions(-) create mode 100644 api/src/cli.ts diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index dc4aaa51c..e5fa06d2c 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -11,68 +11,71 @@ concurrency: cancel-in-progress: true jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v3 + with: + persist-credentials: true + - name: Reconfigure git to use HTTP authenti:cation + run: > + git config --global url."https://github.com/".insteadOf + ssh://git@github.com/ + - name: Build Docker Compose + working-directory: api + run: | + docker-compose build builder + - name: Save Image + run: docker save unraid-api:builder > /tmp/builder.tar + - name: Upload Image + uses: actions/upload-artifact@v3 + with: + name: builder + path: /tmp/builder.tar + lint-api: + needs: [build] continue-on-error: true defaults: run: working-directory: api runs-on: ubuntu-latest steps: - - name: Checkout repo - uses: actions/checkout@v3 + - name: Download artifact + uses: actions/download-artifact@v3 with: - persist-credentials: false - - name: Reconfigure git to use HTTP authenti:cation - run: > - git config --global url."https://github.com/".insteadOf - ssh://git@github.com/ - - - name: Install node - uses: actions/setup-node@v3 - with: - node-version-file: "api/.nvmrc" - - # - name: Cache dependencies - # uses: actions/cache@v2 - # with: - # path: ~/.npm - # key: npm-${{ hashFiles('package-lock.json') }} - # restore-keys: npm- - - - name: Install libvirt-dev - run: sudo apt-get update && sudo apt-get install libvirt-dev - - - name: Installing node deps - run: npm ci - - - name: Lint files - run: npm run lint + name: builder + path: /tmp + - name: Load image + run: | + docker load --input /tmp/builder.tar + - name: Lint + run: | + docker run unraid-api:builder npm run lint test-api: + needs: [build] defaults: run: working-directory: api runs-on: ubuntu-latest - steps: - - name: Checkout repo - uses: actions/checkout@v3 + - name: Download artifact + uses: actions/download-artifact@v3 with: - persist-credentials: false - - - name: Reconfigure git to use HTTP authentication - run: > - git config --global url."https://github.com/".insteadOf - ssh://git@github.com/ - - - name: Build Docker Compose + name: builder + path: /tmp + - name: Load image run: | - docker network create mothership_default - docker-compose build builder - - name: Run Docker Compose - run: docker-compose run builder npm run coverage + docker load --input /tmp/builder.tar + - name: Test + run: | + docker run unraid-api:builder npm run coverage + build-api: + needs: [build] defaults: run: working-directory: api @@ -87,30 +90,17 @@ jobs: - name: Checkout repo uses: actions/checkout@v3 - - name: Install node - uses: actions/setup-node@v3 + - name: Download artifact + uses: actions/download-artifact@v3 with: - node-version-file: "api/.nvmrc" + name: build + path: /tmp + - name: Load image + run: | + docker load --input /tmp/builder.tar - - name: Install libvirt-dev - run: sudo apt-get update && sudo apt-get install libvirt-dev - - # - name: Cache dependencies - # uses: actions/cache@v2 - # with: - # path: ~/.npm - # key: npm-${{ hashFiles('package-lock.json') }} - # restore-keys: npm- - - - name: Installing node deps - run: npm install - - - name: Install pkg - run: npm i -g pkg - - - name: Build and Pack - id: build-pack-binary - run: WORKDIR=${{ github.workspace }} && npm run build-pkg + - name: Run Docker Compose + run: docker run builder npm run build-pkg - name: Set Hashes id: set-hashes diff --git a/api/Dockerfile b/api/Dockerfile index 26561968e..89ffbd17b 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -1,11 +1,13 @@ ########################################################### # Development/Build Image ########################################################### -ARG NODE_IMAGE=node:18.17.1 +ARG NODE_IMAGE=node:18.17.1-alpine FROM $NODE_IMAGE As development -# Install build tools -RUN apt-get update -y && apt-get install -y \ +# Install build tools and dependencies +RUN apk update && apk add \ + bash \ + alpine-sdk \ python3 \ libvirt-dev \ jq \ @@ -13,7 +15,6 @@ RUN apt-get update -y && apt-get install -y \ RUN mkdir /var/log/unraid-api/ - ARG NODE_ENV=development ARG NPM_I_CMD=npm i @@ -39,4 +40,12 @@ RUN npm i -g pkg zx EXPOSE 4000 +########################################################### +# Builder Image +########################################################### + +FROM development AS builder + +COPY ./src ./src + CMD ["npm", "run", "build-pkg"] \ No newline at end of file diff --git a/api/dev/Unraid.net/myservers.cfg b/api/dev/Unraid.net/myservers.cfg index 28922af40..8dd9a1747 100644 --- a/api/dev/Unraid.net/myservers.cfg +++ b/api/dev/Unraid.net/myservers.cfg @@ -1,5 +1,5 @@ [api] -version="3.1.1+251f9020" +version="3.1.1+8efc0992" [local] [notifier] apikey="unnotify_30994bfaccf839c65bae75f7fa12dd5ee16e69389f754c3b98ed7d5" diff --git a/api/dev/states/myservers.cfg b/api/dev/states/myservers.cfg index 30c96c633..5800457d5 100644 --- a/api/dev/states/myservers.cfg +++ b/api/dev/states/myservers.cfg @@ -1,5 +1,5 @@ [api] -version="3.1.1+251f9020" +version="3.1.1+8efc0992" [local] [notifier] apikey="unnotify_30994bfaccf839c65bae75f7fa12dd5ee16e69389f754c3b98ed7d5" diff --git a/api/docker-compose.yml b/api/docker-compose.yml index eb2dcf8c3..e6f2ff99a 100644 --- a/api/docker-compose.yml +++ b/api/docker-compose.yml @@ -27,20 +27,20 @@ x-volumes: &volumes networks: mothership_default: - external: true services: dev: networks: - mothership_default - image: unraid-api-node-18 + image: unraid-api:development ports: - "3001:3001" build: context: . + target: development dockerfile: Dockerfile args: - NODE_IMAGE: node:18.17.1 + NODE_IMAGE: node:18.17.1-alpine NODE_ENV: development NPM_I_CMD: npm i <<: *volumes @@ -54,15 +54,15 @@ services: builder: - image: unraid-api-node-18 + image: unraid-api:builder build: context: . + target: builder dockerfile: Dockerfile args: - NODE_IMAGE: node:18.17.1 + NODE_IMAGE: node:18.17.1-alpine NODE_ENV: development NPM_I_CMD: npm i <<: *volumes - platform: linux/amd64 profiles: - builder \ No newline at end of file diff --git a/api/package-lock.json b/api/package-lock.json index 2603249a4..e325d3679 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -63,7 +63,6 @@ "pidusage": "^3.0.2", "reflect-metadata": "^0.1.13", "request": "^2.88.2", - "segfault-handler": "^1.3.0", "semver": "^7.4.0", "stoppable": "^1.1.0", "subscriptions-transport-ws": "^0.11.0", @@ -4317,6 +4316,7 @@ "node_modules/bindings": { "version": "1.5.0", "license": "MIT", + "optional": true, "dependencies": { "file-uri-to-path": "1.0.0" } @@ -7452,7 +7452,8 @@ }, "node_modules/file-uri-to-path": { "version": "1.0.0", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/fill-range": { "version": "7.0.1", @@ -10705,7 +10706,8 @@ }, "node_modules/nan": { "version": "2.17.0", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/nanoassert": { "version": "1.1.0", @@ -12852,15 +12854,6 @@ "dev": true, "license": "MIT" }, - "node_modules/segfault-handler": { - "version": "1.3.0", - "hasInstallScript": true, - "license": "BSD-3-Clause", - "dependencies": { - "bindings": "^1.2.1", - "nan": "^2.14.0" - } - }, "node_modules/semver": { "version": "7.5.4", "license": "ISC", @@ -18273,6 +18266,7 @@ }, "bindings": { "version": "1.5.0", + "optional": true, "requires": { "file-uri-to-path": "1.0.0" } @@ -20298,7 +20292,8 @@ } }, "file-uri-to-path": { - "version": "1.0.0" + "version": "1.0.0", + "optional": true }, "fill-range": { "version": "7.0.1", @@ -22331,7 +22326,8 @@ } }, "nan": { - "version": "2.17.0" + "version": "2.17.0", + "optional": true }, "nanoassert": { "version": "1.1.0" @@ -23626,13 +23622,6 @@ "version": "1.1.0", "dev": true }, - "segfault-handler": { - "version": "1.3.0", - "requires": { - "bindings": "^1.2.1", - "nan": "^2.14.0" - } - }, "semver": { "version": "7.5.4", "requires": { diff --git a/api/package.json b/api/package.json index 83117e2cd..b0b2bde13 100644 --- a/api/package.json +++ b/api/package.json @@ -13,7 +13,6 @@ "pkg": { "assets": [ "dist/index.cjs", - "node_modules/segfault-handler", "node_modules/@vmngr/libvirt/build/Release", "node_modules/ts-invariant/", "src/**/*.graphql" @@ -112,7 +111,6 @@ "pidusage": "^3.0.2", "reflect-metadata": "^0.1.13", "request": "^2.88.2", - "segfault-handler": "^1.3.0", "semver": "^7.4.0", "stoppable": "^1.1.0", "subscriptions-transport-ws": "^0.11.0", diff --git a/api/src/cli.ts b/api/src/cli.ts new file mode 100644 index 000000000..c0690cacf --- /dev/null +++ b/api/src/cli.ts @@ -0,0 +1,11 @@ +import 'wtfnode'; + +import { am } from 'am'; +import { main } from '@app/cli/index'; +import { internalLogger } from '@app/core/log'; + +void am(main, (error: unknown) => { + internalLogger.fatal((error as Error).message); + // Ensure process is exited + process.exit(1); +}); diff --git a/api/src/cli/index.ts b/api/src/cli/index.ts index e7a842c59..bdf47ae78 100644 --- a/api/src/cli/index.ts +++ b/api/src/cli/index.ts @@ -1,4 +1,3 @@ -import { causeSegfault } from 'segfault-handler'; import { parse } from 'ts-command-line-args'; import { cliLogger } from '@app/core/log'; import { type Flags, mainOptions, options, args } from '@app/cli/options'; @@ -64,14 +63,6 @@ export const main = async (...argv: string[]) => { // Run the command await commandMethod(...argv); - // Only segfault in a specific mode - if (process.env.PLEASE_SEGFAULT_FOR_ME) { - // Wait 30s and then segfault - setTimeout(() => { - causeSegfault(); - }, 30_000); - } - // Allow the process to exit // Don't exit when we start though if (!['start', 'restart'].includes(command)) {