build.yml 9.7 KB

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