build.yml 9.3 KB

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