Skip to content

Commit f68218e

Browse files
authored
Merge pull request #1 from getdevopspro/dev
build: add basic nodejs app
2 parents 38817de + c532de0 commit f68218e

16 files changed

Lines changed: 4638 additions & 0 deletions

.eslintignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/
2+
build/
3+
dist/
4+
coverage/
5+
*.config.ts
6+
*.config.js

.eslintrc.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"parserOptions": {
5+
"ecmaVersion": 2022,
6+
"sourceType": "module",
7+
"project": "./tsconfig.json"
8+
},
9+
"plugins": ["@typescript-eslint"],
10+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
11+
"env": {
12+
"node": true,
13+
"es2022": true
14+
},
15+
"rules": {
16+
"@typescript-eslint/no-unused-vars": [
17+
"error",
18+
{ "argsIgnorePattern": "^_" }
19+
],
20+
"@typescript-eslint/explicit-function-return-type": "off",
21+
"@typescript-eslint/no-explicit-any": "warn"
22+
}
23+
}

.github/workflows/build.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Build
2+
3+
on:
4+
workflow_call:
5+
outputs:
6+
image-digests:
7+
description: 'The output image digests'
8+
value: ${{ jobs.build.outputs.image-digests }}
9+
10+
jobs:
11+
build:
12+
name: Build
13+
uses: getdevopspro/github-actions/.github/workflows/build.yml@v6.0.3
14+
with:
15+
version-package: package.json
16+
version-package-lock: package-lock.json
17+
pre-lint-command: NODEAPP_UID=$(id -u) NODEAPP_RESTART_POLICY=no NODEAPP_COMMAND="bash -c 'npm ci --ignore-scripts; npm run lint'" docker compose up nodeapp --force-recreate --remove-orphans --abort-on-container-exit --exit-code-from nodeapp
18+
pre-test-unit-command: NODEAPP_UID=$(id -u) NODEAPP_RESTART_POLICY=no NODEAPP_COMMAND="bash -c 'npm ci --ignore-scripts; npm run test'" docker compose up nodeapp --force-recreate --remove-orphans --abort-on-container-exit --exit-code-from nodeapp

.github/workflows/pull-request.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: PR
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.job }}
9+
cancel-in-progress: true
10+
11+
permissions:
12+
contents: read
13+
packages: write
14+
15+
jobs:
16+
ci:
17+
name: CI
18+
uses: ./.github/workflows/build.yaml

.github/workflows/release.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
tags:
9+
- 'v*'
10+
paths-ignore:
11+
- '**.md'
12+
13+
concurrency:
14+
group: release-${{ github.workflow }}
15+
cancel-in-progress: false
16+
17+
permissions:
18+
contents: write
19+
packages: write
20+
21+
jobs:
22+
build:
23+
name: CI
24+
uses: ./.github/workflows/build.yaml
25+
26+
promote:
27+
name: CI
28+
needs: build
29+
uses: getdevopspro/github-actions/.github/workflows/promote.yml@v6.0.3
30+
with:
31+
version-package: package.json
32+
git-add-files: package.json
33+
git-user-name: getdevopspro-cibot
34+
git-user-email: 203600057+getdevopspro-cibot@users.noreply.github.com
35+
image-digests: ${{ build.ci.outputs.image-digests }}

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules/
2+
build/
3+
dist/
4+
.npm/
5+
*.log
6+
.env
7+
.env.local
8+
.DS_Store
9+
coverage/

Dockerfile

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Set default base image
2+
ARG ARG_IMAGE_FROM=docker.io/node:22
3+
4+
# -----------------------------------------------------------------------------
5+
# Stage 1: Nodeapp build
6+
# -----------------------------------------------------------------------------
7+
FROM ${ARG_IMAGE_FROM} AS nodeapp-base
8+
9+
WORKDIR /home/node/app
10+
11+
# -----------------------------------------------------------------------------
12+
# Stage 2: Nodeapp build
13+
# -----------------------------------------------------------------------------
14+
FROM nodeapp-base AS nodeapp-build
15+
16+
COPY static /home/node/app/static
17+
COPY *.json *.ts *.js *.cjs /home/node/app/
18+
COPY src /home/node/app/src
19+
20+
RUN --mount=type=cache,target=/home/node/.npm,sharing=locked \
21+
npm ci --ignore-scripts && \
22+
npm run build && \
23+
npm prune --omit=dev
24+
25+
# -----------------------------------------------------------------------------
26+
# TARGET 1: Nodeapp dev image
27+
# -----------------------------------------------------------------------------
28+
FROM nodeapp-base AS nodeapp-dev
29+
30+
ARG NODE_ENV=development
31+
ARG NODE_UID=1000
32+
ENV NODE_UID=$NODE_UID
33+
ENV NODE_ENV=$NODE_ENV
34+
ENV NPM_CONFIG_PREFIX=/home/node/app/.npm
35+
ENV NPM_CONFIG_CACHE=/home/node/app/.npm
36+
37+
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
38+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
39+
set -ex && \
40+
apt-get update && \
41+
apt-get install --no-install-recommends -y \
42+
# Alphabetical order per best practice
43+
# (To make finding packages in lists easier and to help avoid duplicates)
44+
git \
45+
curl \
46+
make \
47+
tar \
48+
unzip \
49+
zip
50+
51+
RUN usermod -u $NODE_UID node && \
52+
groupmod -g $NODE_UID node && \
53+
usermod -d /home/node -m node && \
54+
chown -R $NODE_UID:$NODE_UID /home/node
55+
56+
USER $NODE_UID
57+
58+
CMD ["/bin/bash"]
59+
60+
# -----------------------------------------------------------------------------
61+
# TARGET 2: Nodeapp image
62+
# -----------------------------------------------------------------------------
63+
FROM nodeapp-base AS nodeapp
64+
65+
ARG ENV=production
66+
ARG PORT=3000
67+
ENV NODE_ENV=$ENV
68+
69+
COPY --from=nodeapp-build --chown=node:root /home/node/app/package.json .
70+
COPY --from=nodeapp-build --chown=node:root /home/node/app/node_modules node_modules/
71+
COPY --from=nodeapp-build --chown=node:root /home/node/app/build build/
72+
73+
EXPOSE $PORT
74+
75+
CMD [ "node", "-r", "dotenv/config", "build" ]
76+
77+
# -----------------------------------------------------------------------------
78+
# TARGET 3: Final image
79+
# -----------------------------------------------------------------------------
80+
FROM nodeapp AS final

docker-bake.hcl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// docker-bake.hcl
2+
target "docker-metadata-action" {}
3+
4+
target "build" {
5+
inherits = ["docker-metadata-action"]
6+
context = "./"
7+
dockerfile = "Dockerfile"
8+
platforms = [
9+
"linux/amd64",
10+
"linux/arm64",
11+
]
12+
}

docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
services:
2+
nodeapp:
3+
image: ${NODEAPP_IMAGE:-nodeapp:dev}
4+
user: '${NODEAPP_UID:-1000}'
5+
working_dir: '${NODEAPP_WORKING_DIR:-/home/node/app}'
6+
network_mode: 'host'
7+
env_file:
8+
- path: '${NODEAPP_ENV_FILE:-.env}'
9+
required: false
10+
restart: '${NODEAPP_RESTART_POLICY:-unless-stopped}'
11+
build:
12+
context: .
13+
args:
14+
NODE_UID: '${NODEAPP_UID:-1000}'
15+
dockerfile: '${NODEAPP_DOCKERFILE:-Dockerfile}'
16+
target: '${NODEAPP_DOCKERFILE_TARGET:-nodeapp-dev}'
17+
environment:
18+
- PORT=${NODEAPP_PORT:-5173}
19+
- NODE_UID=${NODEAPP_UID:-1000}
20+
volumes:
21+
- './:${NODEAPP_WORKING_DIR:-/home/node/app}:rw,z'
22+
stdin_open: ${NODEAPP_STDIN_OPEN:-false}
23+
command: ${NODEAPP_COMMAND:-bash -c "npm i; npm run dev -- --host 0.0.0.0 --port ${NODEAPP_PORT:-5173}"}

0 commit comments

Comments
 (0)