|
| 1 | +name: Build |
| 2 | +on: |
| 3 | + push: |
| 4 | + |
| 5 | + workflow_dispatch: |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +concurrency: release-${{ github.ref }} |
| 10 | +jobs: |
| 11 | + build: |
| 12 | + name: Build |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v3 |
| 16 | + - uses: actions/setup-node@v3 |
| 17 | + with: |
| 18 | + node-version: "20" |
| 19 | + - name: Install modules |
| 20 | + run: npm ci --ignore-scripts |
| 21 | + - name: Build |
| 22 | + run: npm run build |
| 23 | + - name: Publish artifact |
| 24 | + uses: actions/upload-artifact@v3 |
| 25 | + with: |
| 26 | + name: "build" |
| 27 | + path: "dist" |
| 28 | + |
| 29 | + build-binaries: |
| 30 | + name: Build binaries - ${{ matrix.config.name }} |
| 31 | + needs: |
| 32 | + - build |
| 33 | + runs-on: ${{ matrix.config.os }} |
| 34 | + strategy: |
| 35 | + fail-fast: false |
| 36 | + matrix: |
| 37 | + config: |
| 38 | + - name: "Windows MSVC" |
| 39 | + os: windows-2022 |
| 40 | + cc: "cl" |
| 41 | + cxx: "cl" |
| 42 | + environment_script: "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Auxiliary/Build/vcvars64.bat" |
| 43 | + generators: "Visual Studio 17 2022" |
| 44 | + artifact: "win" |
| 45 | + - name: "Ubuntu GCC" |
| 46 | + os: ubuntu-22.04 |
| 47 | + cc: "gcc" |
| 48 | + cxx: "g++" |
| 49 | + generators: "Ninja" |
| 50 | + artifact: "linux" |
| 51 | + - name: "macOS Clang" |
| 52 | + os: macos-12 |
| 53 | + cc: "clang" |
| 54 | + cxx: "clang++" |
| 55 | + generators: "Xcode" |
| 56 | + artifact: "mac" |
| 57 | + |
| 58 | + steps: |
| 59 | + - uses: actions/checkout@v3 |
| 60 | + - uses: actions/setup-node@v3 |
| 61 | + with: |
| 62 | + node-version: "20" |
| 63 | + |
| 64 | + - uses: actions/download-artifact@v3 |
| 65 | + with: |
| 66 | + name: build |
| 67 | + path: dist |
| 68 | + |
| 69 | + - uses: actions/setup-python@v4 |
| 70 | + with: |
| 71 | + python-version: "3.10" |
| 72 | + |
| 73 | + - name: Install dependencies on windows |
| 74 | + if: startsWith(matrix.config.os, 'windows') |
| 75 | + run: | |
| 76 | + choco install ninja cmake |
| 77 | +
|
| 78 | + - name: Install dependencies on ubuntu |
| 79 | + if: startsWith(matrix.config.name, 'Ubuntu Latest GCC') |
| 80 | + run: | |
| 81 | + sudo apt-get update |
| 82 | + sudo apt-get install ninja-build cmake libtbb-dev |
| 83 | +
|
| 84 | + - name: Install dependencies on macos |
| 85 | + if: startsWith(matrix.config.os, 'macos') |
| 86 | + run: | |
| 87 | + brew install cmake ninja |
| 88 | + alias make=cmake |
| 89 | +
|
| 90 | + - name: Setup & Build |
| 91 | + id: build |
| 92 | + shell: bash |
| 93 | + timeout-minutes: 40 |
| 94 | + env: |
| 95 | + ARTIFACT_NAME: ${{ matrix.config.artifact }} |
| 96 | + run: | |
| 97 | + npm ci --ignore-scripts |
| 98 | + |
| 99 | + npx zx -y <<'EOF' |
| 100 | + |
| 101 | + async function getLatestNodeVersions(maxDate) { |
| 102 | + const res = await fetch("https://nodejs.org/dist/index.json"); |
| 103 | + const data = await res.json(); |
| 104 | + const versions = new Map(); |
| 105 | + let latestVersion = null; |
| 106 | + |
| 107 | + for (const version of data) { |
| 108 | + const majorVersion = Number(version.version.split(".")[0].slice("v".length)); |
| 109 | + const versionDate = new Date(version.date); |
| 110 | + |
| 111 | + if (maxDate != null && versionDate.getTime() > maxDate) |
| 112 | + continue; |
| 113 | + |
| 114 | + if (!versions.has(majorVersion)) { |
| 115 | + versions.set(majorVersion, version.version); |
| 116 | + } |
| 117 | + |
| 118 | + if (latestVersion === null || majorVersion > latestVersion) { |
| 119 | + latestVersion = majorVersion; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return {versions, latestVersion}; |
| 124 | + } |
| 125 | + |
| 126 | + function getArches() { |
| 127 | + switch (process.env.ARTIFACT_NAME) { |
| 128 | + case "win": |
| 129 | + return ["x64"]; |
| 130 | + case "linux": |
| 131 | + return ["x64", "arm64", "armv7l", "ppc64le"]; |
| 132 | + case "mac": |
| 133 | + return ["x64", "arm64"]; |
| 134 | + } |
| 135 | + |
| 136 | + return ["x64"]; |
| 137 | + } |
| 138 | + |
| 139 | + const {versions: latestNodeVersions, latestVersion: latestNodeVersion} = await getLatestNodeVersions(Date.now() - 1000 * 60 * 60 * 24 * 14); |
| 140 | + |
| 141 | + const minNodeVersion = latestNodeVersion - 4; |
| 142 | + |
| 143 | + const nodeVersions = [...latestNodeVersions].reduce((acc, [majorVersion, version]) => { |
| 144 | + if (majorVersion >= minNodeVersion) |
| 145 | + acc.push(version); |
| 146 | + |
| 147 | + return acc; |
| 148 | + }, []); |
| 149 | + const arches = getArches(); |
| 150 | + |
| 151 | + console.log("Building for node versions", nodeVersions, "and archs", arches); |
| 152 | + |
| 153 | + await $`mkdir -p llamaBins`; |
| 154 | + |
| 155 | + for (const nodeVersion of nodeVersions) { |
| 156 | + for (const arch of arches) { |
| 157 | + console.log(`Building ${arch} for node ${nodeVersion}`); |
| 158 | + |
| 159 | + const majorNodeVersion = parseInt(nodeVersion.slice("v".length)) |
| 160 | + |
| 161 | + const binName = `${process.env.ARTIFACT_NAME}-${arch}-${majorNodeVersion}.node`; |
| 162 | + await $`node ./dist/cli/cli.js download --arch ${arch} --nodeTarget ${nodeVersion}`; |
| 163 | + await $`mv ./llama/build/Release/llama.node ${"./llamaBins/" + binName}`; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + await $`echo "Built binaries:"`; |
| 168 | + await $`ls llamaBins`; |
| 169 | + |
| 170 | + EOF |
| 171 | +
|
| 172 | + - name: Publish artifact |
| 173 | + uses: actions/upload-artifact@v3 |
| 174 | + with: |
| 175 | + name: "bins-${{ matrix.config.artifact }}" |
| 176 | + path: "llamaBins/*" |
| 177 | + |
| 178 | + release: |
| 179 | + name: Release |
| 180 | + if: github.ref == 'refs/heads/master' |
| 181 | + runs-on: ubuntu-latest |
| 182 | + needs: |
| 183 | + - build |
| 184 | + - build-binaries |
| 185 | + steps: |
| 186 | + - uses: actions/checkout@v3 |
| 187 | + - uses: actions/setup-node@v3 |
| 188 | + with: |
| 189 | + node-version: "20" |
| 190 | + - name: Install modules |
| 191 | + run: npm ci --ignore-scripts |
| 192 | + - uses: actions/download-artifact@v3 |
| 193 | + with: |
| 194 | + path: artifacts |
| 195 | + - name: Move artifacts |
| 196 | + run: | |
| 197 | + mkdir -p llamaBins |
| 198 | + mv artifacts/bins-*/* llamaBins/ |
| 199 | + mv artifacts/build dist/ |
| 200 | + |
| 201 | + echo "Built binaries:" |
| 202 | + ls llamaBins |
| 203 | + - name: Release |
| 204 | + env: |
| 205 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 206 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 207 | + run: npx semantic-release |
| 208 | + |
0 commit comments