build.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. if [[ "$GOARCH" == "amd64" ]]; then
  207. echo "Installing x86_64 Windows cross compiler"
  208. sudo apt-get install -y gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64
  209. echo "CC=x86_64-w64-mingw32-gcc" >> $GITHUB_ENV
  210. echo "CXX=x86_64-w64-mingw32-g++" >> $GITHUB_ENV
  211. elif [[ "$GOARCH" == "386" ]]; then
  212. echo "Installing i686 Windows cross compiler"
  213. sudo apt-get install -y gcc-mingw-w64-i686 g++-mingw-w64-i686
  214. echo "CC=i686-w64-mingw32-gcc" >> $GITHUB_ENV
  215. echo "CXX=i686-w64-mingw32-g++" >> $GITHUB_ENV
  216. elif [[ "$GOARCH" == "arm64" ]]; then
  217. echo "Installing ARM64 Windows cross compiler"
  218. # Ubuntu's apt repositories don't have mingw for ARM64
  219. # Use llvm-mingw project instead
  220. mkdir -p $HOME/llvm-mingw
  221. wget -q https://github.com/mstorsjo/llvm-mingw/releases/download/20231128/llvm-mingw-20231128-ucrt-ubuntu-20.04-x86_64.tar.xz
  222. tar xf llvm-mingw-20231128-ucrt-ubuntu-20.04-x86_64.tar.xz -C $HOME/llvm-mingw --strip-components=1
  223. echo "PATH=$HOME/llvm-mingw/bin:$PATH" >> $GITHUB_ENV
  224. echo "CC=aarch64-w64-mingw32-clang" >> $GITHUB_ENV
  225. echo "CXX=aarch64-w64-mingw32-clang++" >> $GITHUB_ENV
  226. else
  227. echo "Unsupported Windows architecture: $GOARCH"
  228. exit 1
  229. fi
  230. - name: Build
  231. run: |
  232. mkdir -p dist
  233. go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'" -o dist/$BINARY_NAME -v main.go
  234. - name: Archive backend artifacts
  235. uses: actions/upload-artifact@v4
  236. with:
  237. name: ${{ env.ARTIFACT }}
  238. path: dist/${{ env.BINARY_NAME }}
  239. - name: Prepare publish
  240. run: |
  241. cp README*.md ./dist
  242. find dist -printf '%P\n' | tar -C dist --no-recursion -zcvf ${{ env.DIST }}.tar.gz -T -
  243. openssl dgst -sha512 ${{ env.DIST }}.tar.gz | sed 's/([^)]*)//g' | awk '{print $NF}' >> ${{ env.DIST }}.tar.gz.digest
  244. - name: Publish
  245. uses: softprops/action-gh-release@v2
  246. if: github.event_name == 'release'
  247. with:
  248. files: |
  249. ${{ env.DIST }}.tar.gz
  250. ${{ env.DIST }}.tar.gz.digest
  251. - name: Set up nodejs
  252. uses: actions/setup-node@v4
  253. with:
  254. node-version: current
  255. - name: Install dependencies
  256. run: |
  257. corepack enable
  258. corepack prepare pnpm@latest --activate
  259. - name: Upload to R2
  260. if: github.event_name != 'pull_request' && github.ref == 'refs/heads/dev'
  261. uses: cloudflare/wrangler-action@v3
  262. with:
  263. accountId: ${{ secrets.CF_ACCOUNT_ID }}
  264. apiToken: ${{ secrets.CF_R2_API_TOKEN }}
  265. wranglerVersion: "4.21.1"
  266. command: |
  267. r2 object put nginx-ui-dev-build/${{ env.DIST }}.tar.gz --file ./${{ env.DIST }}.tar.gz
  268. r2 object put nginx-ui-dev-build/${{ env.DIST }}.tar.gz.digest --file ./${{ env.DIST }}.tar.gz.digest
  269. docker-build:
  270. if: github.event_name != 'pull_request'
  271. runs-on: ubuntu-latest
  272. needs: build
  273. env:
  274. PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/arm/v5
  275. steps:
  276. - name: Checkout
  277. uses: actions/checkout@v4
  278. - name: Docker meta
  279. id: meta
  280. uses: docker/metadata-action@v5
  281. with:
  282. images: |
  283. uozi/nginx-ui
  284. tags: |
  285. type=schedule
  286. type=ref,event=branch
  287. type=semver,pattern={{version}}
  288. type=semver,pattern={{raw}}
  289. type=sha
  290. type=raw,value=latest,enable=${{ github.event_name == 'release' && !github.event.release.prerelease }}
  291. - name: Download artifacts
  292. uses: actions/download-artifact@v4
  293. with:
  294. path: ./dist
  295. - name: Prepare Artifacts
  296. run: chmod +x ./dist/nginx-ui-*/nginx-ui*
  297. - name: Set up Docker Buildx
  298. id: buildx
  299. uses: docker/setup-buildx-action@v3
  300. - name: Login to DockerHub
  301. uses: docker/login-action@v3
  302. with:
  303. username: ${{ secrets.DOCKERHUB_USER }}
  304. password: ${{ secrets.DOCKERHUB_TOKEN }}
  305. - name: Prepare Dockerfile
  306. run: |
  307. cp ./Dockerfile ./dist
  308. cp -rp ./resources ./dist
  309. - name: Build and push
  310. uses: docker/build-push-action@v6
  311. with:
  312. context: ./dist
  313. file: ./dist/Dockerfile
  314. platforms: ${{ env.PLATFORMS }}
  315. push: ${{ github.event_name != 'pull_request' }}
  316. tags: ${{ steps.meta.outputs.tags }}
  317. labels: ${{ steps.meta.outputs.labels }}
  318. - name: Prepare Demo Dockerfile
  319. run: |
  320. cp ./demo.Dockerfile ./dist
  321. cp -rp ./resources ./dist
  322. - name: Build and push demo
  323. uses: docker/build-push-action@v6
  324. if: github.ref == 'refs/heads/dev'
  325. with:
  326. context: ./dist
  327. file: ./dist/demo.Dockerfile
  328. platforms: ${{ env.PLATFORMS }}
  329. push: 'true'
  330. tags: |
  331. uozi/nginx-ui-demo:latest