build.yml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. name: Build
  2. on:
  3. push:
  4. branches:
  5. - '*'
  6. paths:
  7. - "**/*.js"
  8. - "**/*.vue"
  9. - "frontend/package.json"
  10. - "frontend/.env*"
  11. - "**/*.go"
  12. - "go.mod"
  13. - "go.sum"
  14. - ".github/workflows/build*.yml"
  15. - "resources/docker/docker/*"
  16. - "resources/development/*"
  17. - "resources/demo/*"
  18. pull_request:
  19. types: [ opened, synchronize, reopened ]
  20. paths:
  21. - "**/*.js"
  22. - "**/*.vue"
  23. - "frontend/package.json"
  24. - "frontend/.env*"
  25. - "**/*.go"
  26. - "go.mod"
  27. - "go.sum"
  28. - ".github/workflows/*.yml"
  29. - "resources/docker/docker/*"
  30. - "resources/development/*"
  31. - "resources/demo/*"
  32. release:
  33. types:
  34. - published
  35. jobs:
  36. build_frontend:
  37. runs-on: ubuntu-latest
  38. steps:
  39. - name: Checkout
  40. uses: actions/checkout@v3
  41. - name: Set up nodejs
  42. uses: actions/setup-node@v3
  43. with:
  44. node-version: 18.x
  45. - name: Install dependencies
  46. run: yarn install
  47. working-directory: frontend
  48. - name: Update translations
  49. run: yarn gettext:compile
  50. working-directory: frontend
  51. - name: Build
  52. run: |
  53. npx browserslist@latest --update-db
  54. yarn build
  55. working-directory: frontend
  56. - name: Archive frontend artifacts
  57. uses: actions/upload-artifact@v3
  58. with:
  59. name: frontend-dist
  60. path: frontend/dist
  61. - name: Prepare publish
  62. if: github.event_name == 'release'
  63. run: |
  64. cp README*.md frontend/dist
  65. find frontend/dist -printf '%P\n' | tar -C frontend/dist --no-recursion -zcvf frontend-dist.tar.gz -T -
  66. - name: Publish
  67. uses: softprops/action-gh-release@v1
  68. if: github.event_name == 'release'
  69. with:
  70. files: frontend-dist.tar.gz
  71. build:
  72. runs-on: ubuntu-latest
  73. needs: build_frontend
  74. strategy:
  75. matrix:
  76. goos: [ linux, darwin ]
  77. goarch: [ amd64, 386, arm64 ]
  78. exclude:
  79. # Exclude i386 on darwin.
  80. - goarch: 386
  81. goos: darwin
  82. include:
  83. # BEGIN Linux ARM 5 6 7
  84. - goos: linux
  85. goarch: arm
  86. goarm: 7
  87. - goos: linux
  88. goarch: arm
  89. goarm: 6
  90. - goos: linux
  91. goarch: arm
  92. goarm: 5
  93. # END Linux ARM 5 6 7
  94. env:
  95. CGO_ENABLED: 1
  96. GOOS: ${{ matrix.goos }}
  97. GOARCH: ${{ matrix.goarch }}
  98. GOARM: ${{ matrix.goarm }}
  99. steps:
  100. - name: Checkout
  101. uses: actions/checkout@v3
  102. - name: Set up Go
  103. uses: actions/setup-go@v3
  104. with:
  105. go-version: ^1.20.1
  106. - name: Set up cache
  107. uses: actions/cache@v3
  108. with:
  109. path: |
  110. ~/.cache/go-build
  111. ~/go/pkg/mod
  112. key: ${{ runner.os }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}-go-${{ hashFiles('**/go.sum') }}
  113. restore-keys: |
  114. ${{ runner.os }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}-go-
  115. - name: Setup compiler environment
  116. id: info
  117. run: |
  118. export _NAME=nginx-ui-$(jq ".$GOOS[\"$GOARCH$GOARM\"].name" -r < .github/build/build_info.json)
  119. export _ARCH=$(jq ".$GOOS[\"$GOARCH$GOARM\"].arch" -r < .github/build/build_info.json)
  120. export _ABI=$(jq ".$GOOS[\"$GOARCH$GOARM\"].abi // \"\"" -r < .github/build/build_info.json)
  121. export _ARTIFACT=nginx-ui-$GOOS-$GOARCH$(if [[ "$GOARM" ]]; then echo "v$GOARM"; fi)
  122. echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, ABI: $_ABI, RELEASE_NAME: $_NAME, ARTIFACT_NAME: $_ARTIFACT"
  123. echo "ARCH_NAME=$_ARCH" >> $GITHUB_ENV
  124. echo "ABI=$_ABI" >> $GITHUB_ENV
  125. echo "DIST=$_NAME" >> $GITHUB_ENV
  126. echo "ARTIFACT=$_ARTIFACT" >> $GITHUB_ENV
  127. - name: Install musl cross compiler
  128. if: env.GOOS == 'linux'
  129. uses: 0xJacky/musl-cross-compilers@v0.6.5
  130. id: musl
  131. with:
  132. target: ${{ env.ARCH_NAME }}-linux-musl${{ env.ABI }}
  133. - name: Post install musl cross compiler
  134. if: env.GOOS == 'linux'
  135. run: |
  136. echo "PATH=${{ steps.musl.outputs.path }}:$PATH" >> $GITHUB_ENV
  137. echo "CC=${{ env.ARCH_NAME }}-linux-musl${{ env.ABI }}-gcc" >> $GITHUB_ENV
  138. echo "CXX=${{ env.ARCH_NAME }}-linux-musl${{ env.ABI }}-g++" >> $GITHUB_ENV
  139. echo "LD_FLAGS=-w --extldflags '-static'" >> $GITHUB_ENV
  140. - name: Install darwin cross compiler
  141. if: env.GOOS == 'darwin'
  142. run: |
  143. curl -L https://github.com/Hintay/crossosx/releases/latest/download/crossosx.tar.zst -o crossosx.tar.zst
  144. tar xvaf crossosx.tar.zst
  145. echo "LD_LIBRARY_PATH=$(pwd)/crossosx/lib/" >> $GITHUB_ENV
  146. echo "PATH=$(pwd)/crossosx/bin/:$PATH" >> $GITHUB_ENV
  147. echo "CC=${{ env.ARCH_NAME }}-clang" >> $GITHUB_ENV
  148. echo "CXX=${{ env.ARCH_NAME }}-clang++" >> $GITHUB_ENV
  149. echo "LD_FLAGS=-w" >> $GITHUB_ENV
  150. - name: Download frontend artifacts
  151. uses: actions/download-artifact@v3
  152. with:
  153. name: frontend-dist
  154. path: frontend/dist
  155. - name: Build
  156. run: |
  157. mkdir -p dist
  158. go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o dist/nginx-ui -v main.go
  159. - name: Archive backend artifacts
  160. uses: actions/upload-artifact@v3
  161. with:
  162. name: ${{ env.ARTIFACT }}
  163. path: dist/nginx-ui
  164. - name: Prepare publish
  165. if: github.event_name == 'release'
  166. run: |
  167. cp README*.md ./dist
  168. find dist -printf '%P\n' | tar -C dist --no-recursion -zcvf ${{ env.DIST }}.tar.gz -T -
  169. openssl dgst -sha512 ${{ env.DIST }}.tar.gz | sed 's/([^)]*)//g' | awk '{print $NF}' >> ${{ env.DIST }}.tar.gz.digest
  170. - name: Publish
  171. uses: softprops/action-gh-release@v1
  172. if: github.event_name == 'release'
  173. with:
  174. files: |
  175. ${{ env.DIST }}.tar.gz
  176. ${{ env.DIST }}.tar.gz.digest
  177. docker-build:
  178. if: github.event_name != 'pull_request'
  179. runs-on: ubuntu-latest
  180. needs: build
  181. env:
  182. PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/arm/v5
  183. steps:
  184. - name: Checkout
  185. uses: actions/checkout@v3
  186. - name: Get the version
  187. id: get_version
  188. run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
  189. - name: Download artifacts
  190. uses: actions/download-artifact@v3
  191. with:
  192. path: ./dist
  193. - name: Prepare Artifacts
  194. run: chmod +x ./dist/nginx-ui-*/nginx-ui
  195. - name: Set up Docker Buildx
  196. id: buildx
  197. uses: docker/setup-buildx-action@v2
  198. - name: Login to DockerHub
  199. uses: docker/login-action@v2
  200. with:
  201. username: ${{ secrets.DOCKERHUB_USER }}
  202. password: ${{ secrets.DOCKERHUB_TOKEN }}
  203. - name: Prepare Dockerfile
  204. if: github.event_name == 'release'
  205. run: |
  206. cp ./Dockerfile ./dist
  207. cp -rp ./resources ./dist
  208. - name: Build and push
  209. if: github.event_name == 'release'
  210. uses: docker/build-push-action@v3
  211. with:
  212. context: ./dist
  213. file: ./dist/Dockerfile
  214. platforms: ${{ env.PLATFORMS }}
  215. push: ${{ github.event_name != 'pull_request' }}
  216. tags: |
  217. uozi/nginx-ui:latest
  218. uozi/nginx-ui:${{ steps.get_version.outputs.VERSION }}
  219. - name: Prepare Demo Dockerfile
  220. run: |
  221. cp ./demo.Dockerfile ./dist
  222. cp -rp ./resources ./dist
  223. - name: Build and push demo
  224. uses: docker/build-push-action@v3
  225. with:
  226. context: ./dist
  227. file: ./dist/demo.Dockerfile
  228. platforms: ${{ env.PLATFORMS }}
  229. push: ${{ github.event_name != 'pull_request' }}
  230. tags: |
  231. uozi/nginx-ui-demo:latest