feat: 使用多阶段构建优化 wasm-go 构建流程 (#192)

This commit is contained in:
悟空
2023-02-24 12:26:31 +08:00
committed by GitHub
parent e839052a9e
commit bd381b3536
5 changed files with 179 additions and 72 deletions

View File

@@ -1,40 +1,84 @@
FROM ubuntu as builder
# The Dockerfile for wasm-go builder only support amd64 and arm64 yet.
# If you want to build on another architecture, the following information may be helpful.
#
# - arch: amd64 \
# base image: docker.io/ubuntu
# go_url: https://golang.google.cn/dl/go1.20.1.linux-amd64.tar.gz"
# tinygo_url: https://github.com/tinygo-org/tinygo/releases/download/v0.26.0/tinygo_0.26.0_amd64.deb
#
# - arch: arm64
# base image: docker.io/ubuntu
# go_url: https://golang.google.cn/dl/go1.20.1.linux-arm64.tar.gz
# tinygo_url: https://github.com/tinygo-org/tinygo/releases/download/v0.26.0/tinygo_0.26.0_arm64.deb
#
# - arch: armel
# base image: build yourself
# go_url: install from source code
# tinygo_url: build yourself
#
# - arch: i386
# base image: build yourself
# go_url: https://dl.google.com/go/go1.20.1.linux-386.tar.gz
# tinygo_url: build yourself
#
# - arch: mips64el
# base image: build your self
# go_url: https://dl.google.com/go/go1.20.1.linux-386.tar.gz
# tinygo_url: build your self
#
# - arch: ppc64el
# base image: build your self
# go_url: https://dl.google.com/go/go1.20.1.linux-ppc64le.tar.gz
# tinygo_url: build your self
#
# - arch: s390x
# base image: docker.io/ubuntu
# go_url: https://dl.google.com/go/go1.20.1.linux-s390x.tar.gz
# tinygo_url: build your self
#
# - arch: armhf
# base image: build your self
# go_url: https://golang.google.cn/dl/go1.20.1.linux-armv6l.tar.gz
# tinygo_url: https://github.com/tinygo-org/tinygo/releases/download/v0.26.0/tinygo_0.26.0_armhf.deb
ARG BASE_IMAGE=docker.io/ubuntu
FROM $BASE_IMAGE
ARG THIS_ARCH
ARG PLUGIN_NAME
ARG GO_VERSION
ARG TINYGO_VERSION
ARG this_arch=${THIS_ARCH:-amd64}
ARG plugin_name=${PLUGIN_NAME:-hello-world}
ARG go_version=${GO_VERSION:-1.19}
ARG tinygo_version=${TINYGO_VERSION:-0.26.0}
ARG go_pkg_name=go$go_version.linux-$this_arch.tar.gz
ARG tinygo_pkg_name=tinygo_${tinygo_version}_$this_arch.deb
LABEL go_version=$GO_VERSION tinygo_version=$TINYGO_VERSION
RUN apt-get update \
&& apt-get install -y wget build-essential \
&& rm -rf /var/lib/apt/lists/*
RUN wget https://golang.google.cn/dl/$go_pkg_name
RUN rm -rf /usr/local/go && tar -C /usr/local -xzf $go_pkg_name
ENV PATH=$PATH:/usr/local/go/bin
RUN wget https://github.com/tinygo-org/tinygo/releases/download/v$tinygo_version/$tinygo_pkg_name
RUN dpkg -i $tinygo_pkg_name
ENV export PATH=$PATH:/usr/local/bin
RUN arch="$(dpkg --print-architecture)"; arch="${arch##*-}"; \
go_url=; \
tinygo_url=; \
go_version=${GO_VERSION:-1.19}; \
tinygo_version=${TINYGO_VERSION:-0.26.0}; \
echo "arch: '$arch'"; \
echo "go go_version: '$go_version'"; \
echo "tinygo_version: '$tinygo_version'"; \
case "$arch" in \
'amd64') \
go_url="https://golang.google.cn/dl/go$go_version.linux-amd64.tar.gz"; \
tinygo_url="https://github.com/tinygo-org/tinygo/releases/download/v$tinygo_version/tinygo_${tinygo_version}_amd64.deb"; \
;; \
'arm64') \
go_url="https://golang.google.cn/dl/go$go_version.linux-arm64.tar.gz"; \
tinygo_url="https://github.com/tinygo-org/tinygo/releases/download/v$tinygo_version/tinygo_${tinygo_version}_arm64.deb"; \
;; \
*) echo >&2 "error: unsupported architecture '$arch' "; exit 1 ;; \
esac; \
echo "go_url: '$go_url'"; \
echo "tinygo_url: '$tinygo_url'"; \
wget -O go.tgz "$go_url" --progress=dot:giga; \
wget -O tinygo.deb "$tinygo_url" --progress=dot:giga; \
echo "Download complete"; \
rm -rf /usr/local/go && tar -C /usr/local -xzf go.tgz && rm -rf go.tgz; \
dpkg -i tinygo.deb && rm -rf tinygo.deb
WORKDIR /workspace
COPY . .
WORKDIR /workspace/extensions/$plugin_name
RUN go mod tidy
RUN tinygo build -o /main.wasm -scheduler=none -target=wasi ./main.go
FROM scratch
COPY --from=builder /main.wasm plugin.wasm
ENV PATH=$PATH:/usr/local/go/bin:/usr/local/bin