Skip to content

Commit

Permalink
chore: build docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Nov 7, 2024
1 parent 329ae11 commit 93f60b4
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 26 deletions.
51 changes: 51 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name-template: 'v$RESOLVED_VERSION'
tag-template: 'v$RESOLVED_VERSION'
change-template: '* $TITLE (#$NUMBER) by @$AUTHOR'
template: |
$CHANGES
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION
sort-direction: ascending

categories:
- title: '⚠️ BREAKING CHANGES'
label: 'breaking'
- title: '💫 Features'
label: 'feature'
- title: '🛠️ Bug fixes'
label: 'fix'
- title: '🕹️ Others'
label: 'chore'

version-resolver:
# Major is not meant to be used at the moment.
# Should be used with label breaking in the future.
major:
labels:
- 'major'
minor:
labels:
- 'breaking'
- 'feature'
- 'chore'
patch:
labels:
- 'fix'

exclude-labels:
- 'skip-changelog'

autolabeler:
- label: 'breaking'
title:
- '/!:/i'
- label: 'chore'
title:
- '/^chore/i'
- label: 'fix'
title:
- '/^fix/i'
- label: 'feature'
title:
- '/^feat/i'
98 changes: 98 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Docker

on:
push:
branches:
- main
paths:
- 'src/**'
- '*.ts'
- '*.json'
- '*.yaml'
- Dockerfile
- .github/workflows/docker.yaml
pull_request:
branches:
- main
paths:
- 'src/**'
- '*.ts'
- '*.json'
- '*.yaml'
- Dockerfile
- .github/workflows/docker.yaml
release:
types:
- published

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
PLATFORMS: ${{ github.event_name == 'pull_request' && 'linux/amd64' || 'linux/amd64,linux/arm64' }}

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: "Generate Build ID (main)"
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: |
branch=${GITHUB_REF##*/}
sha=${GITHUB_SHA::8}
ts=$(date +%s)
echo "BUILD_ID=${branch}-${ts}-${sha}" >> $GITHUB_ENV
- name: "Generate Build ID (PR)"
if: github.event_name == 'pull_request'
run: |
echo "BUILD_ID=pr-${{ github.event.number }}-$GITHUB_RUN_ID" >> $GITHUB_ENV
- name: "Generate Build ID (Release)"
if: github.event_name == 'release'
run: |
echo "BUILD_ID=${GITHUB_REF##*/}" >> $GITHUB_ENV
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=pr
type=ref,event=branch
type=raw,value=${{ env.BUILD_ID }}
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ env.PLATFORMS }}
cache-from: type=gha
cache-to: type=gha,mode=max
29 changes: 29 additions & 0 deletions .github/workflows/pr-title.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: 'Validate PR title'

on:
pull_request:
branches: [ main ]
types: [ opened, reopened, synchronize ]

permissions:
pull-requests: read
statuses: write

jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
# Please look up the latest version from
# https://github.com/amannn/action-semantic-pull-request/releases
- uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
types: |
fix
feat
chore
requireScope: false
wip: true
validateSingleCommit: false
22 changes: 22 additions & 0 deletions .github/workflows/release-drafter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Release Drafter

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
types: [ opened, reopened, synchronize ]

permissions:
contents: read

jobs:
update_release_draft:
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70 changes: 70 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# syntax=docker.io/docker/dockerfile:1

FROM node:22-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi


# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED=1

RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT=3000

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]
2 changes: 1 addition & 1 deletion next.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
/* config options here */
output: "standalone",
};

export default nextConfig;
3 changes: 3 additions & 0 deletions public/humans.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* TEAM */
Name: Matthieu Moquet
Contact: matthieu [at] moquet [dot] net
6 changes: 0 additions & 6 deletions src/app/alerts/[handle]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AlertsTemplate } from "@/components/alerts/template"
import { config } from "@/config"
import { notFound } from "next/navigation"

type Props = {
params: Promise<{ handle: string }>
Expand All @@ -9,10 +7,6 @@ type Props = {
export default async function AlertsViewPage(props: Props) {
const { handle } = await props.params

if (!config.views[handle]) {
return notFound()
}

return (
<AlertsTemplate view={handle} />
)
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/clusters/[slug]/alerts/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { config } from "@/config";
import { getConfig } from "@/config";

export async function GET(
_: Request,
{ params }: { params: Promise<{ slug: string }> }
) {
const cluster = (await params).slug
const config = await getConfig()

const endpoint = config.clusters.find((c) => c.name === cluster)?.endpoint
if (!endpoint) {
Expand Down
13 changes: 12 additions & 1 deletion src/app/api/config/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { clientConfig } from "@/config/client";
import { getConfig } from "@/config";

export async function GET(_: Request) {
const config = await getConfig()

// Remove the endpoints from the client config
const clientConfig = {
...config,
clusters: config.clusters.map((c) => ({
...c,
endpoint: ""
})),
}

return new Response(JSON.stringify(clientConfig), {
headers: { "Content-Type": "application/json" },
});
Expand Down
7 changes: 4 additions & 3 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Metadata } from "next";
import localFont from "next/font/local";
import "./globals.css";
import { clientConfig } from "@/config/client";
import { ConfigProvider } from "@/contexts/config";
import { ThemeProvider } from "next-themes";
import { AppLayout } from "@/components/layout/app-layout";
import { AlertsProvider } from "@/contexts/alerts";

export const dynamic = "force-dynamic";

const geistSans = localFont({
src: "./fonts/GeistVF.woff",
variable: "--font-geist-sans",
Expand All @@ -23,7 +24,7 @@ export const metadata: Metadata = {
description: "Dashboard for AlertManager Prometheus",
};

export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
Expand All @@ -39,7 +40,7 @@ export default function RootLayout({
enableSystem
disableTransitionOnChange
>
<ConfigProvider config={clientConfig}>
<ConfigProvider>
<AlertsProvider>
<AppLayout>
{children}
Expand Down
6 changes: 5 additions & 1 deletion src/components/alerts/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AlertModal } from './alert-modal'
import { LabelFilter, Group } from './types'
import { useConfig } from '@/contexts/config'
import { alertFilter, alertSort } from './utils'

import { notFound } from 'next/navigation'

type Props = {
view: string
Expand All @@ -22,6 +22,10 @@ export function AlertsTemplate(props: Props) {
const { alerts, loading, errors, refreshAlerts } = useAlerts()
const [selectedAlert, setSelectedAlert] = useState<Alert | null>(null)

if (!config.views[view]) {
return notFound()
}

const { filters, groupBy } = config.views[view]

// Flatten alerts
Expand Down
2 changes: 1 addition & 1 deletion src/config/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from "."
import { config } from "./server"

// Remove the endpoints from the client config
export const clientConfig = {
Expand Down
Loading

0 comments on commit 93f60b4

Please sign in to comment.