Skip to content

Commit

Permalink
Refactored Docker CI workflow and updated Node.js and pnpm versions
Browse files Browse the repository at this point in the history
  • Loading branch information
micthiesen committed Oct 26, 2024
1 parent 535be47 commit 4951b5d
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 73 deletions.
90 changes: 56 additions & 34 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,77 @@
name: Docker Image CI
name: Build and Publish Docker Image

on:
workflow_dispatch:
push:
branches: [main]
branches:
- main
pull_request:
branches:
- main

env:
IMAGE_NAME: edgecontrol
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: true

jobs:
lint:
lint-and-test:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
run_install: false
- uses: actions/setup-node@v4
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Pnpm
uses: pnpm/action-setup@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
node-version-file: ".nvmrc"
cache: "pnpm"
- run: pnpm install
- run: npm run check
- run: npm run typecheck

push:
needs: lint
- name: Install dependencies
run: pnpm install

- name: Lint
run: pnpm run check

- name: Typecheck
run: pnpm run typecheck

build-and-publish:
runs-on: ubuntu-latest
needs: [lint-and-test]

permissions:
packages: write
contents: read
packages: write

steps:
- uses: actions/checkout@v4
- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"
- name: Checkout code
uses: actions/checkout@v4

- name: Log in to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Push image
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
VERSION=$(echo "${{ github.sha }}" | cut -c 1-7)
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

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

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository_owner }}/edgecontrol:latest
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/edgecontrol:latest
cache-to: type=inline

docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:latest
docker push --all-tags $IMAGE_ID
- name: Logout from GitHub Container Registry
run: docker logout ghcr.io
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.16.0
34 changes: 19 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
FROM node:20-slim AS build
# Use a specific Node.js version as a base image
FROM node:20.16.0-slim

COPY . /build
WORKDIR /build
# Enable corepack
RUN corepack enable

RUN npm install -g pnpm@9
RUN pnpm install --frozen-lockfile
RUN pnpm run build
# Set working directory
WORKDIR /app

FROM node:20-slim
ENV NODE_ENV=production
# Copy package.json and pnpm-lock.yaml
COPY package.json pnpm-lock.yaml ./

COPY --from=build /build/dist /app
# Install pnpm and project dependencies
RUN corepack prepare pnpm@latest --activate && pnpm install

WORKDIR /app
COPY package.json pnpm-lock.yaml ./
# Copy the rest of the application code
COPY . .

# Build the TypeScript code
RUN pnpm run build

RUN npm install -g pnpm@9
RUN pnpm install --production
# Expose port 3000
EXPOSE 3000

EXPOSE 5888
CMD ["node", "/app/index.js"]
# Command to run the application
CMD ["node", "dist/index.js"]
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"name": "edgecontrol",
"version": "0.0.0",
"private": true,
"type": "module",
"main": "dist/index.js",
"scripts": {
"start": "node dist/index.js",
"dev": "dotenvx run -- tsx watch src/index.ts",
"build": "tsc --outDir dist src/index.ts",
"build": "tsc",
"typecheck": "tsc --noEmit",
"lint": "biome lint .",
"format": "biome format .",
"check": "biome check ."
"check": "biome check .",
"docker:build": "docker build -t edgecontrol .",
"docker:publish": "docker push edgecontrol",
"docker:build-and-publish": "pnpm run docker:build && pnpm run docker:publish",
"docker:dev": "docker run -p 3000:3000 --rm --name edgecontrol edgecontrol"
},
"dependencies": {
"@hono/node-server": "^1.13.2",
Expand All @@ -24,5 +30,9 @@
"tsx": "^4.7.1",
"typescript": "^5.0.4"
},
"engines": {
"node": "20.16.0",
"pnpm": "9.12.0"
},
"packageManager": "[email protected]"
}
10 changes: 5 additions & 5 deletions src/dns.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import env from "./env";
import { sendNotification, type PushoverMessage } from "./notify";
import { parseDnsServers } from "./parsing";
import { withSshConnection, type SNodeSSH } from "./ssh";
import { arrayItemsEqual } from "./utils";
import env from "./env.js";
import { sendNotification, type PushoverMessage } from "./notify.js";
import { parseDnsServers } from "./parsing.js";
import { withSshConnection, type SNodeSSH } from "./ssh.js";
import { arrayItemsEqual } from "./utils.js";

let timeoutId: NodeJS.Timeout | null = null;
export const RESTORE_DELAY_MINUTES = 2;
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { type HttpBindings, serve } from "@hono/node-server";
import { Hono } from "hono";
import { logger } from "hono/logger";
import { timeout } from "hono/timeout";
import { RESTORE_DELAY_MINUTES, toggleDns, validateDns } from "./dns";
import env from "./env";
import { sendNotification } from "./notify";
import { withSshConnection } from "./ssh";
import { RESTORE_DELAY_MINUTES, toggleDns, validateDns } from "./dns.js";
import env from "./env.js";
import { sendNotification } from "./notify.js";
import { withSshConnection } from "./ssh.js";

const app = new Hono<{ Bindings: HttpBindings }>();
app.use(logger(), timeout(15000));
Expand Down
2 changes: 1 addition & 1 deletion src/notify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import https from "node:https";
import { URLSearchParams } from "node:url";
import env from "./env";
import env from "./env.js";

export interface PushoverMessage {
message: string;
Expand Down
2 changes: 1 addition & 1 deletion src/ssh.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeSSH } from "node-ssh";
import env from "./env";
import env from "./env.js";

export type SNodeSSH = NodeSSH & {
execSafe: (command: string) => Promise<string>;
Expand Down
17 changes: 7 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"include": ["src/**/*"],
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"lib": ["ES2020"],
"moduleResolution": "node",
"esModuleInterop": true,
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"sourceMap": false
"types": ["node"]
}
}

0 comments on commit 4951b5d

Please sign in to comment.