build.yml 9.3 KB

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