build.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. name: Build
  2. on:
  3. push:
  4. branches-ignore:
  5. - 'weblate'
  6. paths:
  7. - "app/**/*.js"
  8. - "app/**/*.ts"
  9. - "app/**/*.vue"
  10. - "app/src/language/**/*.po"
  11. - "app/i18n.json"
  12. - "app/package.json"
  13. - "app/.env*"
  14. - "**/*.go"
  15. - "go.mod"
  16. - "go.sum"
  17. - ".github/workflows/build*.yml"
  18. - "resources/docker/docker/*"
  19. - "resources/development/*"
  20. - "resources/demo/*"
  21. - "Dockerfile"
  22. - "demo.Dockerfile"
  23. pull_request:
  24. types: [ opened, synchronize, reopened ]
  25. paths:
  26. - "**/*.js"
  27. - "**/*.vue"
  28. - "app/package.json"
  29. - "app/.env*"
  30. - "**/*.go"
  31. - "go.mod"
  32. - "go.sum"
  33. - ".github/workflows/*.yml"
  34. - "resources/docker/docker/*"
  35. - "resources/development/*"
  36. - "resources/demo/*"
  37. release:
  38. types:
  39. - published
  40. jobs:
  41. build_app:
  42. runs-on: macos-14
  43. steps:
  44. - name: Checkout
  45. uses: actions/checkout@v4
  46. - name: Set up nodejs
  47. uses: actions/setup-node@v4
  48. with:
  49. node-version: current
  50. - name: Install dependencies
  51. run: |
  52. corepack enable
  53. corepack prepare pnpm@latest --activate
  54. pnpm install
  55. working-directory: app
  56. - name: Check frontend code style
  57. run: |
  58. pnpm run lint
  59. working-directory: app
  60. - name: Check frontend types
  61. run: |
  62. pnpm run typecheck
  63. working-directory: app
  64. - name: Build
  65. run: |
  66. npx update-browserslist-db@latest
  67. pnpm build
  68. working-directory: app
  69. - name: Archive app artifacts
  70. uses: actions/upload-artifact@v4
  71. with:
  72. name: app-dist
  73. path: app/dist
  74. - name: Prepare publish
  75. if: github.event_name == 'release'
  76. run: |
  77. cp README*.md app/dist
  78. find app/dist -printf '%P\n' | tar -C app/dist --no-recursion -zcvf app-dist.tar.gz -T -
  79. - name: Publish
  80. uses: softprops/action-gh-release@v2
  81. if: github.event_name == 'release'
  82. with:
  83. files: app-dist.tar.gz
  84. build:
  85. runs-on: ubuntu-latest
  86. needs: build_app
  87. strategy:
  88. matrix:
  89. goos: [ linux, darwin, windows ]
  90. goarch: [ amd64, 386, arm64 ]
  91. exclude:
  92. # Exclude i386 on darwin.
  93. - goarch: 386
  94. goos: darwin
  95. include:
  96. # BEGIN Linux ARM 5 6 7
  97. - goos: linux
  98. goarch: arm
  99. goarm: 7
  100. - goos: linux
  101. goarch: arm
  102. goarm: 6
  103. - goos: linux
  104. goarch: arm
  105. goarm: 5
  106. # END Linux ARM 5 6 7
  107. - goos: linux
  108. goarch: riscv64
  109. - goos: linux
  110. goarch: loong64
  111. # BEGIN MIPS
  112. - goos: linux
  113. goarch: mips64
  114. - goos: linux
  115. goarch: mips64le
  116. - goos: linux
  117. goarch: mipsle
  118. - goos: linux
  119. goarch: mips
  120. # END MIPS
  121. env:
  122. CGO_ENABLED: 1
  123. GOOS: ${{ matrix.goos }}
  124. GOARCH: ${{ matrix.goarch }}
  125. GOARM: ${{ matrix.goarm }}
  126. steps:
  127. - name: Checkout
  128. uses: actions/checkout@v4
  129. - name: Set up Go
  130. uses: actions/setup-go@v5
  131. with:
  132. go-version: ^1.24.4
  133. cache: false
  134. - name: Setup environment
  135. id: info
  136. run: |
  137. export _NAME=$(jq ".$GOOS[\"$GOARCH$GOARM\"].name" -r < .github/build/build_info.json)
  138. export _ARCH=$(jq ".$GOOS[\"$GOARCH$GOARM\"].arch" -r < .github/build/build_info.json)
  139. export _ABI=$(jq ".$GOOS[\"$GOARCH$GOARM\"].abi // \"\"" -r < .github/build/build_info.json)
  140. export _ARTIFACT=nginx-ui-$GOOS-$GOARCH$(if [[ "$GOARM" ]]; then echo "v$GOARM"; fi)
  141. export _BINARY=nginx-ui$(if [[ "$GOOS" == "windows" ]]; then echo ".exe"; fi)
  142. echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, ABI: $_ABI, RELEASE_NAME: $_NAME, ARTIFACT_NAME: $_ARTIFACT, BINARY_NAME: $_BINARY"
  143. echo "CACHE_NAME=$_NAME" >> $GITHUB_ENV
  144. echo "ARCH_NAME=$_ARCH" >> $GITHUB_ENV
  145. echo "ABI=$_ABI" >> $GITHUB_ENV
  146. echo "DIST=nginx-ui-$_NAME" >> $GITHUB_ENV
  147. echo "ARTIFACT=$_ARTIFACT" >> $GITHUB_ENV
  148. echo "BINARY_NAME=$_BINARY" >> $GITHUB_ENV
  149. - name: Setup Go modules cache
  150. uses: actions/cache@v4
  151. with:
  152. path: |
  153. ~/go/pkg/mod
  154. key: go-${{ runner.os }}-${{ runner.arch }}-mod-${{ hashFiles('go.mod') }}
  155. restore-keys: |
  156. go-${{ runner.os }}-${{ runner.arch }}-mod-
  157. - name: Setup Go build cache
  158. uses: actions/cache@v4
  159. with:
  160. path: |
  161. ~/.cache/go-build
  162. key: go-${{ runner.os }}-${{ runner.arch }}-${{ env.CACHE_NAME }}-${{ hashFiles('go.mod') }}
  163. restore-keys: |
  164. go-${{ runner.os }}-${{ runner.arch }}-${{ env.CACHE_NAME }}-
  165. - name: Download app artifacts
  166. uses: actions/download-artifact@v4
  167. with:
  168. name: app-dist
  169. path: app/dist
  170. - name: Generate files
  171. env:
  172. GOOS: linux
  173. GOARCH: amd64
  174. run: go generate cmd/version/generate.go
  175. - name: Install musl cross compiler
  176. if: env.GOOS == 'linux'
  177. uses: nginxui/musl-cross-compilers@v1
  178. id: musl
  179. with:
  180. target: ${{ env.ARCH_NAME }}-linux-musl${{ env.ABI }}
  181. variant: ${{ env.GOARCH == 'loong64' && 'userdocs/qbt-musl-cross-make' || 'richfelker/musl-cross-make' }}
  182. - name: Post install musl cross compiler
  183. if: env.GOOS == 'linux'
  184. run: |
  185. echo "PATH=${{ steps.musl.outputs.path }}:$PATH" >> $GITHUB_ENV
  186. echo "CC=${{ env.ARCH_NAME }}-linux-musl${{ env.ABI }}-gcc" >> $GITHUB_ENV
  187. echo "CXX=${{ env.ARCH_NAME }}-linux-musl${{ env.ABI }}-g++" >> $GITHUB_ENV
  188. echo "LD_FLAGS=-w --extldflags '-static'" >> $GITHUB_ENV
  189. - name: Install darwin cross compiler
  190. if: env.GOOS == 'darwin'
  191. run: |
  192. curl -L https://github.com/Hintay/crossosx/releases/latest/download/crossosx.tar.zst -o crossosx.tar.zst
  193. tar xvaf crossosx.tar.zst
  194. echo "LD_LIBRARY_PATH=$(pwd)/crossosx/lib/" >> $GITHUB_ENV
  195. echo "PATH=$(pwd)/crossosx/bin/:$PATH" >> $GITHUB_ENV
  196. echo "CC=${{ env.ARCH_NAME }}-clang" >> $GITHUB_ENV
  197. echo "CXX=${{ env.ARCH_NAME }}-clang++" >> $GITHUB_ENV
  198. echo "LD_FLAGS=-w" >> $GITHUB_ENV
  199. - name: Setup for Windows
  200. if: env.GOOS == 'windows'
  201. run: |
  202. echo "LD_FLAGS=-w" >> $GITHUB_ENV
  203. echo "CGO_ENABLED=1" >> $GITHUB_ENV
  204. # Install cross compilers based on architecture
  205. sudo apt-get update
  206. sudo apt-get install -y zip
  207. if [[ "$GOARCH" == "amd64" ]]; then
  208. echo "Installing x86_64 Windows cross compiler"
  209. sudo apt-get install -y gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64
  210. echo "CC=x86_64-w64-mingw32-gcc" >> $GITHUB_ENV
  211. echo "CXX=x86_64-w64-mingw32-g++" >> $GITHUB_ENV
  212. elif [[ "$GOARCH" == "386" ]]; then
  213. echo "Installing i686 Windows cross compiler"
  214. sudo apt-get install -y gcc-mingw-w64-i686 g++-mingw-w64-i686
  215. echo "CC=i686-w64-mingw32-gcc" >> $GITHUB_ENV
  216. echo "CXX=i686-w64-mingw32-g++" >> $GITHUB_ENV
  217. elif [[ "$GOARCH" == "arm64" ]]; then
  218. echo "Installing ARM64 Windows cross compiler"
  219. # Ubuntu's apt repositories don't have mingw for ARM64
  220. # Use llvm-mingw project instead
  221. mkdir -p $HOME/llvm-mingw
  222. wget -q https://github.com/mstorsjo/llvm-mingw/releases/download/20231128/llvm-mingw-20231128-ucrt-ubuntu-20.04-x86_64.tar.xz
  223. tar xf llvm-mingw-20231128-ucrt-ubuntu-20.04-x86_64.tar.xz -C $HOME/llvm-mingw --strip-components=1
  224. echo "PATH=$HOME/llvm-mingw/bin:$PATH" >> $GITHUB_ENV
  225. echo "CC=aarch64-w64-mingw32-clang" >> $GITHUB_ENV
  226. echo "CXX=aarch64-w64-mingw32-clang++" >> $GITHUB_ENV
  227. else
  228. echo "Unsupported Windows architecture: $GOARCH"
  229. exit 1
  230. fi
  231. - name: Build
  232. run: |
  233. mkdir -p dist
  234. go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'" -o dist/$BINARY_NAME -v main.go
  235. - name: Archive backend artifacts
  236. uses: actions/upload-artifact@v4
  237. with:
  238. name: ${{ env.ARTIFACT }}
  239. path: dist/${{ env.BINARY_NAME }}
  240. - name: Prepare publish
  241. run: |
  242. cp README*.md ./dist
  243. find dist -printf '%P\n' | tar -C dist --no-recursion -zcvf ${{ env.DIST }}.tar.gz -T -
  244. openssl dgst -sha512 ${{ env.DIST }}.tar.gz | sed 's/([^)]*)//g' | awk '{print $NF}' >> ${{ env.DIST }}.tar.gz.digest
  245. # Create zip for Windows builds (for winget compatibility)
  246. if [[ "$GOOS" == "windows" ]]; then
  247. cd dist
  248. zip -r ../${{ env.DIST }}.zip .
  249. cd ..
  250. openssl dgst -sha512 ${{ env.DIST }}.zip | sed 's/([^)]*)//g' | awk '{print $NF}' >> ${{ env.DIST }}.zip.digest
  251. fi
  252. - name: Publish
  253. uses: softprops/action-gh-release@v2
  254. if: github.event_name == 'release'
  255. with:
  256. files: |
  257. ${{ env.DIST }}.tar.gz
  258. ${{ env.DIST }}.tar.gz.digest
  259. ${{ env.GOOS == 'windows' && format('{0}.zip', env.DIST) || '' }}
  260. ${{ env.GOOS == 'windows' && format('{0}.zip.digest', env.DIST) || '' }}
  261. - name: Set up nodejs
  262. uses: actions/setup-node@v4
  263. with:
  264. node-version: current
  265. - name: Install dependencies
  266. run: |
  267. corepack enable
  268. corepack prepare pnpm@latest --activate
  269. - name: Upload to R2
  270. if: github.event_name != 'pull_request' && github.ref == 'refs/heads/dev'
  271. uses: cloudflare/wrangler-action@v3
  272. env:
  273. WRANGLER_LOG: debug
  274. WRANGLER_LOG_SANITIZE: false
  275. with:
  276. accountId: ${{ secrets.CF_ACCOUNT_ID }}
  277. apiToken: ${{ secrets.CF_R2_API_TOKEN }}
  278. wranglerVersion: "4.23.0"
  279. command: |
  280. r2 object put nginx-ui-dev-build/${{ env.DIST }}.tar.gz --file ./${{ env.DIST }}.tar.gz --remote
  281. r2 object put nginx-ui-dev-build/${{ env.DIST }}.tar.gz.digest --file ./${{ env.DIST }}.tar.gz.digest --remote
  282. docker-build:
  283. if: github.event_name != 'pull_request'
  284. runs-on: ubuntu-latest
  285. needs: build
  286. env:
  287. PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/arm/v5
  288. steps:
  289. - name: Checkout
  290. uses: actions/checkout@v4
  291. - name: Docker meta
  292. id: meta
  293. uses: docker/metadata-action@v5
  294. with:
  295. images: |
  296. uozi/nginx-ui
  297. tags: |
  298. type=schedule
  299. type=ref,event=branch
  300. type=semver,pattern={{version}}
  301. type=semver,pattern={{raw}}
  302. type=sha
  303. type=raw,value=latest,enable=${{ github.event_name == 'release' && !github.event.release.prerelease }}
  304. - name: Download artifacts
  305. uses: actions/download-artifact@v4
  306. with:
  307. path: ./dist
  308. - name: Prepare Artifacts
  309. run: chmod +x ./dist/nginx-ui-*/nginx-ui*
  310. - name: Set up Docker Buildx
  311. id: buildx
  312. uses: docker/setup-buildx-action@v3
  313. - name: Login to DockerHub
  314. uses: docker/login-action@v3
  315. with:
  316. username: ${{ secrets.DOCKERHUB_USER }}
  317. password: ${{ secrets.DOCKERHUB_TOKEN }}
  318. - name: Prepare Dockerfile
  319. run: |
  320. cp ./Dockerfile ./dist
  321. cp -rp ./resources ./dist
  322. - name: Build and push
  323. uses: docker/build-push-action@v6
  324. with:
  325. context: ./dist
  326. file: ./dist/Dockerfile
  327. platforms: ${{ env.PLATFORMS }}
  328. push: ${{ github.event_name != 'pull_request' }}
  329. tags: ${{ steps.meta.outputs.tags }}
  330. labels: ${{ steps.meta.outputs.labels }}
  331. - name: Prepare Demo Dockerfile
  332. run: |
  333. cp ./demo.Dockerfile ./dist
  334. cp -rp ./resources ./dist
  335. - name: Build and push demo
  336. uses: docker/build-push-action@v6
  337. if: github.ref == 'refs/heads/dev'
  338. with:
  339. context: ./dist
  340. file: ./dist/demo.Dockerfile
  341. platforms: ${{ env.PLATFORMS }}
  342. push: 'true'
  343. tags: |
  344. uozi/nginx-ui-demo:latest