Browse Source

build: add docker build

build/docker 0.0.1
Evan 7 months ago
parent
commit
33531e1203
  1. 4
      .dockerignore
  2. 24
      Dockerfile
  3. 54
      Makefile

4
.dockerignore

@ -0,0 +1,4 @@
**/node_modules
**/bin
docs
assets

24
Dockerfile

@ -0,0 +1,24 @@
FROM golang:1.15-alpine as backend
RUN apk add --no-cache git gcc musl-dev
WORKDIR /src
RUN go get github.com/swaggo/swag/cmd/swag
RUN go get github.com/rakyll/statik
COPY go.mod go.sum /src/
RUN go mod download
COPY . .
RUN go generate
RUN VERSION=$(git describe --tags)-$(git rev-parse --short HEAD) &&\
go build \
-ldflags="-w -X 'github.com/mutsuki333/calendar/cmd.Version=${VERSION}' -X 'github.com/mutsuki333/calendar/services.Version=${VERSION}'" \
-o /calendar main.go
FROM golang:1.15-alpine
RUN apk add --no-cache gcc musl-dev
WORKDIR /
COPY --from=backend /calendar .
CMD ./calendar

54
Makefile

@ -1,5 +1,5 @@
VERSION=$(shell git describe --tags --abbrev=4)
VERSION=$(shell git describe --tags)
BUILD=$(shell git rev-parse --short HEAD)
PROJECTNAME := $(shell basename "$(PWD)")
@ -13,49 +13,33 @@ LDFLAG=-X 'github.com/mutsuki333/calendar/cmd.Version=$(VERSION)-$(BUILD)'
LDFLAG+=-X 'github.com/mutsuki333/calendar/services.Version=$(VERSION)-$(BUILD)'
LDFLAGS=-ldflags="-w $(LDFLAG)"
MAKEFLAGS += --silent
# MAKEFLAGS += --silent
generate:
go generate
.PHONY: statik swagger
statik:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) statik -src=ui/dist -dest=assets -f
statik -src=ui/dist -dest=assets -f
swagger:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) swag init -g services/services.go
swag init -g services/services.go
build:
@echo " > Building binary..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
go build $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
debug: statik build
debug: swagger build
all: swagger statik build min
all: generate build min
min:
@echo " > Building minimized binary..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build -tags min $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME)-min $(GOFILES)
go-get:
@echo " > Checking if there is any missing dependencies..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get $(get)
exec:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) $(run)
compile:
# 32-Bit Systems
# FreeBDS
GOOS=freebsd GOARCH=386 go build -o bin/calendar-freebsd-386 main.go
# MacOS
GOOS=darwin GOARCH=386 go build -o bin/calendar-darwin-386 main.go
# Linux
GOOS=linux GOARCH=386 go build -o bin/calendar-linux-386 main.go
# Windows
GOOS=windows GOARCH=386 go build -o bin/calendar-windows-386 main.go
# 64-Bit
# FreeBDS
GOOS=freebsd GOARCH=amd64 go build -o bin/calendar-freebsd-amd64 main.go
# MacOS
GOOS=darwin GOARCH=amd64 go build -o bin/calendar-darwin-amd64 main.go
# Linux
GOOS=linux GOARCH=amd64 go build -o bin/calendar-linux-amd64 main.go
# Windows
GOOS=windows GOARCH=amd64 go build -o bin/calendar-windows-amd64 main.go
go build -tags min $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME)-min $(GOFILES)
docker-build:
docker build -t $(PROJECTNAME):$(VERSION) .
docker-run: docker-build
docker run -it --rm --name $(PROJECTNAME) -p 8080:8080 $(PROJECTNAME):$(VERSION)