打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

在 Ubuntu VPS 上快速编译 Swift Vapor Project

来自md5.pw
这是此页面的最新修订,它没有已核准修订。

前置说明:

正常情况下,使用 Docker 编译 Vapor 项目,Swift 的编译缓存是无法使用的,也就是说每次编译 Vapor 项目都需要在 Docker 中将 swift-package、whole compile 全走一遍,我的云服务器在未优化流程之前,每次可能需要耗费十分钟以上。

测试编译的机器配置:

Ubuntu 24.04, 2GB RAM, 2 vCPU

原理

Vapor 提供的 Dockerfile 文件是将项目的编译、复制等操作都放到了 Docker 容器中进行,而 Docker 容器无法缓存编译信息(意思是下次编译时无法获取到上次的编译缓存进行增量编译),所以每次都会全量一遍。

而 Vapor 运行,只需要编译后的 Run 产物和一些必要的 Public、Resource 资源。

依照这个思路,我们可以将 Vapor 的编译放置到机器上进行,编译完成后,将 Run 和 Public、Resource 资源拷贝进 Docker 中即可。

安装 Swift 可参考: 在搬瓦工VPS上安装 Swift 和他的依赖

该安装方式与 Swift Docker 保持一致,我正在使用该方式。

更多披露

在 Vapor 项目中新建一个 fastDockerfile, 内容如下:

因为是在机器上编译,所以这里只需要使用 -noble/-slim 最小的镜像进行打包即可

# ================================
# Copy Resources Image
# ================================
FROM ubuntu:24.04 AS build

# Set up a build area
WORKDIR /build

# Copy entire repo into container
COPY . .

# Switch to the staging area
WORKDIR /staging

# Copy main executable to staging area
COPY ./Run ./

# Copy any resources from the public directory and views directory if the directories exist
# Ensure that by default, neither the directory nor any of its contents are writable.
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true

# ================================
# Run image
# ================================
FROM swift:6.2.1-focal-slim

# Create a vapor user and group with /app as its home directory
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor

# Switch to the new home directory
WORKDIR /app

# Copy built executable and any staged resources from builder
COPY --from=build --chown=vapor:vapor /staging /app

# Ensure all further commands run as the vapor user
USER vapor:vapor

# Let Docker bind to port 8080
EXPOSE 8080

# Set Language Encoding
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

# Start the Vapor service when the image is run, default to listening on 8080 in production environment
ENTRYPOINT ["./Run"]
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

编写脚本 fast_build.sh:

请注意,调用脚本时需要提供镜像名称

#!/bin/bash

local IMAGE_NAME=${1:-"custom-image:latest"}

echo "🚀 fast building start"

# stop when happen error
set -e

echo "🚀 > git pull"
git pull

echo "🚀 > git submodule update --remote"
git submodule update --remote

echo "🚀 > swift build"
swift build -c release

# move run to .
echo "🚀 > copy {Run} to ./Run"
cp .build/x86_64-unknown-linux-gnu/release/Run ./Run

echo "🚀 > docker build"
docker build -f fastDockerfile . -t $IMAGE_NAME

echo "🚀 > rm local ./Run"
rm -rf ./Run

# redirect
echo "🚀 > sh redirect.main.sh"
docker-compose -f docker-compose.yml up -d

# force clean <none> docker images
echo "🚀 > clean invalid images"
docker image rm -f $(docker images -f dangling=true -q)

echo "🚀 > 🎉🎉🎉 all of done ~"

🎉 OK

验证

验证工作,在服务器上快速编译时,使用

bash fast_build.sh <镜像名称>

例如:

bash fast_build.sh my-image:latest

该脚本将使用上次编译的缓存(因为是在机器上编译,而 Dockerfile 只是将机器上编译完成的产物打包成镜像),因此将增量编译时间缩短至 1 分钟以内,极小的改动仅需 3~8 秒即可完成。