This commit is contained in:
C菌
2022-04-11 20:37:19 +08:00
parent 2f848fda1e
commit 9631ecf68b
3 changed files with 54 additions and 22 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
test.go
bin/

View File

@@ -6,6 +6,10 @@ ENV GO111MODULE=off
RUN GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o hello main.go
FROM alpine:latest AS runner
# 设置alpine 时间为上海时间
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
COPY --from=builder /app/hello /app/hello
WORKDIR /app
EXPOSE 80

View File

@@ -1,28 +1,55 @@
# https://eddycjy.com/posts/go/gin/2018-08-26-makefile/
.PHONY: build clean tool lint docker help
all: build
GOPATH:=$(shell go env GOPATH)
VERSION=$(shell git describe --tags --always)
APP='helloworld'
.PHONY: build
# build
build:
go build -v .
tool:
go tool vet . |& grep -v vendor; true
gofmt -w .
lint:
golint ./...
clean:
rm -rf main helloworld
go clean -i .
mkdir -p bin
GOARCH=amd64 GOOS=linux go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/$(APP)"-linux-amd64" ./...
GOARCH=arm64 GOOS=linux go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/$(APP)"-linux-arm64" ./...
GOARCH=amd64 GOOS=darwin go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/$(APP)"-darwin-amd64" ./...
GOARCH=arm64 GOOS=darwin go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/$(APP)"-darwin-arm64" ./...
.PHONY: docker
# build docker image
docker:
docker build -t mirrors2/helloworld:latest .
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t mirrors2/$(APP):latest . --push
.PHONY: clean
# clean build
clean:
rm -rf bin/
.PHONY: cleand
# clean docker
cleand:
docker rmi $(docker images |grep none|awk '{print $3}') -f
docker rm $(docker ps -aq)
# docker rm $(docker ps -a |grep -v Up)
.PHONY: all
# generate all
all:
# show help
help:
@echo "make: compile packages and dependencies"
@echo "make tool: run specified go tool"
@echo "make lint: golint ./..."
@echo "make clean: remove object files and cached files"
@echo "make docker: build docker image"
@echo ''
@echo 'Usage:'
@echo ' make [target]'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^# (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help