Skip to content

Commit

Permalink
feat: add ci pipeline with docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarow committed Dec 23, 2023
1 parent b391954 commit 9228075
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 6 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/ca.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
41 changes: 36 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
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); \
)


22 changes: 22 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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)
Expand Down

0 comments on commit 9228075

Please sign in to comment.