• 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    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"]
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    
    ## 相关命令
    
    把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  
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    java138-异常处理
    买火车票的学问
    设计模式---原型模式
    丢失d3dx11_43.dll如何修复,d3dx11_43.dll丢失的5种解决方法
    东南电子开启申购:客户集中度较高,预计上市时市值约18亿元
    【Android知识笔记】热修复专题
    【机器学习】聚类算法中的距离度量有哪些及公式表示?
    Linux基础入门到精通之Linux系统安装日志
    Android Jetpack组件架构 :LiveData的使用和原理
    lvgl 实现状态提示图标自动对齐补位显示
  • 原文地址:https://blog.csdn.net/weixin_38385580/article/details/132689009