# 构建可执行文件
FROM {{cross_compiling }}golang:{{ golang_version }} as build_bin
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.io
# 启用cgo
ENV CGO_ENABLED=1
WORKDIR /code
# 复制依赖列表
COPY go.mod /code/go.mod
COPY go.sum /code/go.sum
# 复制源码
COPY {{ app_name }}.go /code/{{ app_name }}.go
# 编译可执行文件
RUN go build -ldflags "-s -w" -o {{ app_name }}-go {{ app_name }}.go

# 直接使用可执行文件构造镜像
FROM {{ cross_compiling }}debian:buster-slim as build_img
# 部署可执行文件
COPY --from=build_bin /code/{{ app_name }}-go .
RUN chmod +x /{{ app_name }}-go
ENTRYPOINT [ "/{{ app_name }}-go"]


# 压缩可执行文件
FROM {{ cross_compiling }}alpine:3.11 as upx
WORKDIR /code
# 安装upx
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk update && apk add --no-cache upx && rm -rf /var/cache/apk/*
COPY --from=build_bin /code/{{ app_name }}-go .
RUN upx --best --lzma -o {{ app_name }} {{ app_name }}-go

# 构造镜像
FROM {{ cross_compiling }}debian:buster-slim as build_upximg
# 部署可执行文件
COPY --from=upx /code/{{ app_name }} .
RUN chmod +x /{{ app_name }}
ENTRYPOINT [ "/{{ app_name }}"]