Skip to content

Commit

Permalink
chore: add container action
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Feb 13, 2024
1 parent 3027fc4 commit bc41bd6
Show file tree
Hide file tree
Showing 12 changed files with 3,567 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.next
58 changes: 17 additions & 41 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
name: Azure Static Web Apps CI/CD
name: Publish Docker image

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
release:
types: [published]

jobs:
test:
permissions:
checks: write
uses: ./.github/workflows/main.yml

deploy:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
needs: [ test ]
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
submodules: true
lfs: false
- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_JOLLY_FIELD_066760403 }}
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
action: "upload"
###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
app_location: "/" # App source code path
api_location: "" # Api source code path - optional
output_location: "" # Built app content directory - optional
###### End of Repository/Build Configurations ######

close_pull_request_job:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
steps:
- name: Close Pull Request
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
images: zeiss/service-lens
- uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_JOLLY_FIELD_066760403 }}
action: "close"
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
42 changes: 38 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
FROM node:18-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* ./
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
Expand All @@ -11,16 +45,16 @@ ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY ./public ./public
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 --chown=nextjs:nodejs ./.next/standalone ./
COPY --chown=nextjs:nodejs ./.next/static ./packages/app/.next/static
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

Expand All @@ -32,4 +66,4 @@ ENV HOSTNAME "0.0.0.0"

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "packages/app/server.js"]
CMD ["node", "server.js"]
104 changes: 104 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"
"log"
"os"

"github.com/katallaxie/pkg/logger"
"github.com/spf13/cobra"
"github.com/zeiss/service-lens/internal/adapters"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

// DB ...
type DB struct {
Username string
Password string
Port int
Database string
Host string
}

// Flags contains the command line flags.
type Flags struct {
Addr string
DB *DB
}

// New ...
func New() *Config {
return &Config{
Flags: &Flags{
DB: &DB{
Username: "example",
Password: "example",
Port: 5432,
Database: "example",
Host: "host.docker.internal",
},
},
}
}

// Config ...
type Config struct {
Flags *Flags
}

// Cwd returns the current working directory.
func (c *Config) Cwd() (string, error) {
return os.Getwd()
}

var cfg = New()

var (
version = "dev"
commit = "none"
date = "unknown"
)

var rootCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return run(cmd.Context())
},
}

func init() {
rootCmd.PersistentFlags().StringVar(&cfg.Flags.Addr, "addr", ":8080", "addr")
rootCmd.PersistentFlags().StringVar(&cfg.Flags.DB.Database, "db-database", cfg.Flags.DB.Database, "Database name")
rootCmd.PersistentFlags().StringVar(&cfg.Flags.DB.Username, "db-username", cfg.Flags.DB.Username, "Database user")
rootCmd.PersistentFlags().StringVar(&cfg.Flags.DB.Password, "db-password", cfg.Flags.DB.Password, "Database password")
rootCmd.PersistentFlags().IntVar(&cfg.Flags.DB.Port, "db-port", cfg.Flags.DB.Port, "Database port")

rootCmd.SilenceUsage = true
}

func main() {
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}

func run(ctx context.Context) error {
log.SetFlags(0)
log.SetOutput(os.Stderr)

logger.RedirectStdLog(logger.LogSink)

dsn := "host=host.docker.internal user=example password=example dbname=example port=5432 sslmode=disable"
conn, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return err
}

db := adapters.NewDB(conn)
err = db.RunMigration()
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit bc41bd6

Please sign in to comment.