makefile & dockerfile

makefile

PROJECT:=data_report

swag:
	@swag i -g init_router.go -dir app/admin/router --instanceName admin --parseDependency -o docs/admin

test:
	@go test ./... -v


pre-push:
    @commit_message=`git log -1 --pretty=format:"%s"`; \
    if [ -z "$$commit_message" ]; then \
        echo "Error: No commit message provided. Please use 'git commit -m' to add a commit message."; \
        exit 1; \
    fi

git-commit:
	@git add .
	@git commit -m "$(COMMIT)"

git-push: pre-push
	@git push origin $(BRANCH)

# make build-linux
compile:
	@# 32-Bit Systems
	@# FreeBDS
	@GOOS=freebsd GOARCH=386 go build -o target/$(PROJECT)-freebsd-386 main.go
	@# MacOS
	@GOOS=darwin GOARCH=386 go build -o target/$(PROJECT)-darwin-386 main.go
	@# Linux
	@GOOS=linux GOARCH=386 go build -o target/$(PROJECT)-linux-386 main.go
	@# Windows
	@GOOS=windows GOARCH=386 go build -o target/$(PROJECT)-windows-386 main.go
	@# 64-Bit
	@# FreeBDS
	@GOOS=freebsd GOARCH=amd64 go build -o target/$(PROJECT)-freebsd-amd64 main.go
	@# MacOS
	@GOOS=darwin GOARCH=amd64 go build -o target/$(PROJECT)-darwin-amd64 main.go
	@# Linux
	@GOOS=linux GOARCH=amd64 go build -o target/$(PROJECT)-linux-amd64 main.go
	@# Windows
	@GOOS=windows GOARCH=amd64 go build -o target/$(PROJECT)-windows-amd64 main.go

build:
	@docker build -t $(IMAGE):$(VERSION) .
	@echo "build successful"

docker-push:
	docker push $(IMAGE):$(VERSION)

## pre: swag test
pre: swag test

## git commit: git-commit git-push
git: pre git-commit git-push

## all: build、docker-push
all: build docker-push

.PHONY: build docker-push

dockerfile

FROM golang:1.20.4 AS builder

WORKDIR /go/cache

ADD go.mod .
ADD go.sum .

#--mount jenkins存在文件 穿透,如果结合jenkins 则需要删除
RUN --mount=type=cache,mode=0777,id=go-mod,target=/go/pkg/mod \
    go env -w GO111MODULE=on \
    && go env -w GOPROXY=https://goproxy.cn,direct \
    && go mod download

WORKDIR /src

COPY . /src

RUN #go test ./... -v

RUN #go vet

RUN GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -ldflags="-s -w" -o miz

FROM debian as prod

RUN  echo "Asia/Shanghai" > /etc/timezone \
    && rm -f /etc/localtime \
    && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

WORKDIR /opt

COPY --from=builder /src/miz /opt

EXPOSE 80

CMD ["./miz", "server"]


## 相关命令

把docker加入环境变量
export PATH="$PATH:/Applications/Docker.app/Contents/Resources/bin/"

#构建镜像
make -f Makefile all VERSION=v1.0.1 IMAGE=test


#run 起来一个镜像
 docker run -it -v ~/goProject/data_report/config/settings.yml:/opt/config/settings.yml -p 80:80 test:v1.0.1 sh 


#commit 
make -f Makefile git-commit COMMIT="调整git"
#推送远程分支
 make -f Makefile git-push BRANCH=master  


你可能感兴趣的:(docker,makefile,dockerfile)