項目地址:https://github.com/helson-lin/pkg_sqlite
在ffandown
項目內,由于項目使用了sqlite3
,在跨平臺打包的時候,除了本機外其他平臺打包之后運行缺少node_sqlite3.node
依賴。
為了解決問題,百度了很久,能夠實現的方案就三種。
- 分別在不同平臺打包,這個是最直接的方法。
- 將
node_sqlite3.node
文件放在可執行文件同級目錄下,運行的時候binding
會自動找到。 - 在每一次構建之后,手動將
node_sqlite3.node
依賴移動到node_modules/sqlite3/building/Release/
, 該方案支持github actions
自動打包。
方案3如何實現github actions自動打包
準備好各個平臺的node_sqlite3.node文件
文件可以從TryGhost-GIthub下載
編寫腳本打包之前替換node_sqlite3.node文件
腳本如下,主要思路就是,在每個平臺打包之前,現將對應平臺的文件移動到node_modules/sqlite3/building/Release/
下面。
如果直接使用改腳本,請確保package下面文件的名稱和我的保持一致。
📢 需要注意,這個腳本運行之前有一些前提的條件
- package.json配置好pkg配置項, 包括targets和assets
- 所有的node文件放在同級的package目錄下
- 腳本文件放在根目錄下面的
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { execSync} = require('child_process')
// 源文件路徑(根據你的項目結構調整)
let isDebug = false;
let releaseName;
const argv = process.argv.slice(2)
// 支持debug參數
if (argv && argv[0] === '--debug') isDebug = true
// package為我的項目根目錄下面文件夾存放node文件
const sourcePath = path.join(__dirname, "package/");
// 目標路徑
const targetPath = path.join(__dirname, "node_modules/sqlite3/build/Release/");const moveNodeSqlite = (targetPlatform) => {// 根據目標平臺選擇正確的文件,這里只寫了幾個平臺可以自行補充let targetFile;const name = targetPlatform.split('-').slice(1).join('-')switch (name) {case "linux-x64":targetFile = "linux_x64_node_sqlite3.node";break;case "linux-arm64":targetFile = "linux_arm64_node_sqlite3.node";break;case "macos-arm64":targetFile = "macos_arm64_node_sqlite3.node";break;case "macos-arm64":targetFile = "macos_x64_node_sqlite3.node";break;default:console.error(`\n ?? Unsupported target platform:${targetPlatform} \n`);}if (targetFile) {// 復制文件fs.copyFileSync(path.join(sourcePath, targetFile),path.join(targetPath, "node_sqlite3.node"));console.log(`\n ? Copied ${path.join(sourcePath, targetFile)} to ${path.join(targetPath,"node_sqlite3.node")}\n`);}
};const pkgRelease = (targetPlatform) => {moveNodeSqlite(targetPlatform);// 執行打包命令// --output指定輸出的目錄地址,和文件的名稱execSync(`pkg . -t ${targetPlatform} --output ./dist/${releaseName}-${targetPlatform}${targetPlatform.indexOf('windows') !== -1 ? '.exe' : ''}` + (isDebug ? ' --debug' : ''), { stdio: 'inherit' })
};const start = () => {try {const dataString = fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8')const data = JSON.parse(dataString)const platforms = data.pkg.targetsreleaseName = data.namefor (let item of platforms) {pkgRelease(item)}} catch (e) {console.error('?? read package.json failed', e)}
}start()
package.json配置
這里只粘貼了pkg配置部分、
scripts
配置項為需要打包的js文件
assets
配置項必不可少,少了node文件不會被打包到可執行文件內
targets
配置項填寫需要構建的平臺,這里被腳本引用了,少了腳本會出現問題。
{"name": "docker_sync","version": "1.0.0","description": "","main": "index.js","bin": "index.js","scripts": {"build": "node build.js"},"repository": {"type": "git","url": "git+https://github.com/helson-lin/docker_sync_template.git"},"keywords": ["demo"],"author": "helsonlin","license": "ISC","pkg": {"scripts": ["index.js","db.js"],"assets": ["/node_modules/sqlite3/build/**/*"],"targets": ["node14-macos-arm64","node14-macos-x64","node14-windows-x64","node14-linux-x64","node14-linux-arm64","node14-alpine-x64","node14-alpine-arm64"],"outputPath": "dist"},"bugs": {"url": "https://github.com/helson-lin/docker_sync_template/issues"},"homepage": "https://github.com/helson-lin/docker_sync_template#readme","devDependencies": {"body-parser": "^1.20.2","express": "^4.18.2","pkg": "^5.8.1","sqlite3": "^5.1.6"}
}
Github actions配置
該配置文件不做過多解釋
name: Build and push Docker imageon:push:branches: [ main ]tags:- 'v*.*.*'jobs:build:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v2- name: Npm Installrun: npm install --registry=https://registry.npmmirror.com- name: Build Releaserun: npm run build- name: releaseuses: softprops/action-gh-release@v1if: startsWith(github.ref, 'refs/tags/')with:files: "dist/**"env:GITHUB_TOKEN: ${{ secrets.TOKEN }}