diff --git a/.github/workflows/ca.yaml b/.github/workflows/ca.yaml new file mode 100644 index 0000000..558872f --- /dev/null +++ b/.github/workflows/ca.yaml @@ -0,0 +1,46 @@ +name: Create and publish a Docker image + +on: + push: + branches: + - "main" + tags: + - "*" + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: docker/Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Makefile b/Makefile index 78a1dfb..8b207bc 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,42 @@ -run: gen +BINARY_NAME := skatcounter +IMAGE_NAME := ghcr.io/tarow/$(BINARY_NAME) +TAGS := latest + + +all: clean install gen tidy build + +run: go run main.go +dev: + air + +build: + go build -o bin/$(BINARY_NAME) main.go + +clean: + rm -f bin/* + +install: + go mod download + +lint: + @golangci-lint --version + golangci-lint run + tidy: go mod tidy -gen: - templ generate -dev: gen - air \ No newline at end of file +docker-build: + docker build -f docker/Dockerfile . --tag $(IMAGE_NAME):$(firstword $(TAGS)) + $(foreach tag,$(filter-out $(firstword $(TAGS)),$(TAGS)),\ + docker tag $(IMAGE_NAME):$(firstword $(TAGS)) $(IMAGE_NAME):$(tag); \ + ) + +docker-push: + $(foreach tag, $(TAGS),\ + docker push $(IMAGE_NAME):$(tag); \ + ) + + diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..502a537 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:1.21.5-alpine as builder + +WORKDIR /app + +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY main.go . +COPY internal/ internal/ +COPY static/ static/ +COPY templates/ templates/ + +RUN go build -o skatcounter + +FROM alpine:3.18 + +WORKDIR /app +COPY --from=builder /app/skatcounter /usr/bin/skatcounter + +CMD ["skatcounter"] + diff --git a/main.go b/main.go index a4bd248..75330de 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,16 @@ package main import ( + "embed" + "github.com/labstack/echo/v4" api "github.com/tarow/skat-counter/internal/api" "github.com/tarow/skat-counter/internal/skat" ) +//go:embed static/* +var staticAssets embed.FS + func main() { e := echo.New() @@ -20,7 +25,7 @@ type Form struct { } func registerRoutes(e *echo.Echo, handler api.Handler) { - e.Static("/static", "./static") + e.StaticFS("/static", echo.MustSubFS(staticAssets, "static")) e.GET("/", handler.GetIndex) e.POST("/games", handler.CreateGame)