打开/关闭搜索
搜索
打开/关闭菜单
66
947
83
2889
md5.pw
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
上传文件
打开/关闭外观设置菜单
通知
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。
user-interface-preferences
个人工具
登录
请求账号
查看“︁在 Ubuntu VPS 上快速编译 Swift Vapor Project”︁的源代码
来自md5.pw
分享此页面
更多语言
查看
阅读
查看源代码
查看历史
associated-pages
页面
讨论
更多操作
←
在 Ubuntu VPS 上快速编译 Swift Vapor Project
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
前置说明:<blockquote>正常情况下,使用 Docker 编译 Vapor 项目,Swift 的编译缓存是无法使用的,也就是说每次编译 Vapor 项目都需要在 Docker 中将 swift-package、whole compile 全走一遍,我的云服务器在未优化流程之前,每次可能需要耗费十分钟以上。</blockquote>测试编译的机器配置: '''Ubuntu 24.04, 2GB RAM, 2 vCPU''' == 原理 == Vapor 提供的 Dockerfile 文件是将项目的编译、复制等操作都放到了 Docker 容器中进行,而 Docker 容器无法缓存编译信息(意思是下次编译时无法获取到上次的编译缓存进行增量编译),所以每次都会全量一遍。 而 Vapor 运行,只需要编译后的 Run 产物和一些必要的 Public、Resource 资源。 依照这个思路,我们可以将 Vapor 的编译放置到机器上进行,编译完成后,将 Run 和 Public、Resource 资源拷贝进 Docker 中即可。 '''⚠️ 注意''' 编译机器的 Swift 安装路径最好与 Docker 中的 swift 安装路径保持一致,不然可能会导致 Docker 中运行时找不到动态库之类的情况。 安装 Swift 可参考: [https://md5.pw/index.php?title=在搬瓦工VPS上安装_Swift_和他的依赖 在搬瓦工VPS上安装 Swift 和他的依赖] 该安装方式与 Swift Docker 保持一致,我正在使用该方式。 == 更多披露 == 我在 Vapor 项目中新建了一个 <code>fastDockerfile</code>, 内容如下:<syntaxhighlight lang="bash" line="1"> # ================================ # 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 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 your-vapor-project-name-on-docker:latest 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 ~" </syntaxhighlight>
返回
在 Ubuntu VPS 上快速编译 Swift Vapor Project
。
查看“︁在 Ubuntu VPS 上快速编译 Swift Vapor Project”︁的源代码
来自md5.pw