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 *.swp
public/ public/
resources/ 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: when:
branch: main branch: main
@ -14,13 +6,21 @@ variables:
- &repo git.jmbit.de/${CI_REPO_OWNER}/${CI_REPO_NAME} - &repo git.jmbit.de/${CI_REPO_OWNER}/${CI_REPO_NAME}
steps: steps:
dryrun: hugo:
image: git.jmbit.de/jmb/docker-hugo image: git.jmbit.de/jmb/docker-hugo
commands: commands:
- hugo --minify - hugo --minify
when: when:
event: [ pull-request, push] 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: publish:
image: woodpeckerci/plugin-docker-buildx image: woodpeckerci/plugin-docker-buildx
settings: settings:

View File

@ -1,2 +1,11 @@
FROM nginx:latest FROM golang:alpine AS builder
COPY public /usr/share/nginx/html 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) HEAD=$(shell git rev-parse --short HEAD)
CTNAME:=git.jmbit.de/jmb/www-jmbit-de
all: hugo container
dev: dev:
hugo server -D hugo server -D
hugo: hugo:
hugo hugo --minify
webserver:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app .
container: container:
podman build -t docker.io/jmbitci/www-jmbit-de:latest -t docker.io/jmbitci/www-jmbit-de:$(HEAD) . podman build -t $(CTNAME):latest -t $(CTNAME):$(HEAD) .
publish:
podman push docker.io/jmbitci/www-jmbit-de:latest --all-tags
nopub: hugo container nopub: hugo webserver container
podman run --rm -p8080:80 docker.io/jmbitci/www-jmbit-de podman run --rm -p8080:80 $(CTNAME)
#rollout: run:
# kubectl --context=jmbit-prod rollout restart deployment www-jmbit-de -n jmbit-web podman run --rm -p8080:80 $(CTNAME)
clean: clean:
rm -rf public rm -rf public
all: hugo container publish

View File

@ -1,23 +1,31 @@
package main package main
import ( import (
"embed"
"log" "log"
"net/http" "net/http"
"github.com/gin-gonic/gin" "git.jmbit.de/jmb/www-jmbit-de/public"
) )
//go:embed public/*
var HtmlFS embed.FS
func main() { func main() {
router := gin.New() // Register a custom handler
gin.SetMode(gin.ReleaseMode) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
router.Use(gin.Recovery()) // Specify the file path you want to block
blockFilePath := "/public.go"
router.StaticFS("/", http.FS(HtmlFS)) // Check if the requested path matches the blocked file path
err := router.Run("0.0.0.0:80") 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 { if err != nil {
log.Fatal(err) 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