now builds 10MB container image only including webserver binary

gin
Johannes Bülow 2024-01-11 14:42:33 +01:00
parent c9bf1221ff
commit 3fd7f2ca3c
Signed by untrusted user who does not match committer: jmb
GPG Key ID: B56971CF7B8F83A6
6 changed files with 62 additions and 32 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.swp
public/
resources/
app

View File

@ -1,11 +1,3 @@
# Build and publish Docker images for multible architectures.
#
# Pushing an image to codeberg as container registry,
# package owner will be the repo owner.
#
# this config also shows usage of yaml aliases and
# was taken from https://codeberg.org/6543/docker-images/src/commit/37e29c227717c1c07d2776cddcf14725bf952875/.woodpecker/hello.yml
when:
branch: main
@ -14,13 +6,21 @@ variables:
- &repo git.jmbit.de/${CI_REPO_OWNER}/${CI_REPO_NAME}
steps:
dryrun:
hugo:
image: git.jmbit.de/jmb/docker-hugo
commands:
- hugo --minify
when:
event: [ pull-request, push]
webserver:
image: golang:alpine
commands:
- go mod download && go mod verify
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -buildvcs=true -o app .
when:
event: [ pull-request, push]
publish:
image: woodpeckerci/plugin-docker-buildx
settings:

View File

@ -1,2 +1,11 @@
FROM nginx:latest
COPY public /usr/share/nginx/html
FROM golang:alpine AS builder
RUN apk update && apk add --no-cache git
WORKDIR $GOPATH/src/www-jmbit-de
COPY . .
RUN go get -d -v
RUN go build -o /go/bin/www
FROM scratch
COPY --from=builder /go/bin/www /go/bin/www
ENTRYPOINT ["/go/bin/www"]

View File

@ -1,22 +1,26 @@
HEAD=$(shell git rev-parse --short HEAD)
CTNAME:=git.jmbit.de/jmb/www-jmbit-de
all: hugo container
dev:
hugo server -D
hugo:
hugo
hugo --minify
webserver:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app .
container:
podman build -t docker.io/jmbitci/www-jmbit-de:latest -t docker.io/jmbitci/www-jmbit-de:$(HEAD) .
publish:
podman push docker.io/jmbitci/www-jmbit-de:latest --all-tags
podman build -t $(CTNAME):latest -t $(CTNAME):$(HEAD) .
nopub: hugo container
podman run --rm -p8080:80 docker.io/jmbitci/www-jmbit-de
nopub: hugo webserver container
podman run --rm -p8080:80 $(CTNAME)
#rollout:
# kubectl --context=jmbit-prod rollout restart deployment www-jmbit-de -n jmbit-web
run:
podman run --rm -p8080:80 $(CTNAME)
clean:
rm -rf public
all: hugo container publish

View File

@ -1,23 +1,31 @@
package main
import (
"embed"
"log"
"net/http"
"github.com/gin-gonic/gin"
"git.jmbit.de/jmb/www-jmbit-de/public"
)
//go:embed public/*
var HtmlFS embed.FS
func main() {
router := gin.New()
gin.SetMode(gin.ReleaseMode)
router.Use(gin.Recovery())
// Register a custom handler
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Specify the file path you want to block
blockFilePath := "/public.go"
router.StaticFS("/", http.FS(HtmlFS))
err := router.Run("0.0.0.0:80")
// Check if the requested path matches the blocked file path
if r.URL.Path == blockFilePath {
// Return a 404 Not Found error
http.NotFound(w, r)
return
}
// For other paths, serve the files using the file server
http.FileServer(http.FS(public.HtmlFS)).ServeHTTP(w, r)
})
// Start the HTTP server on port 80
err := http.ListenAndServe(":80", nil)
if err != nil {
log.Fatal(err)
}

8
static/public.go Normal file
View File

@ -0,0 +1,8 @@
package public
import (
"embed"
)
//go:embed *
var HtmlFS embed.FS