release.yml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. name: Release
  2. on:
  3. release:
  4. types: [published]
  5. push:
  6. branches:
  7. - main
  8. pull_request:
  9. paths:
  10. - '.github/workflows/release.yml'
  11. concurrency:
  12. group: ${{ github.workflow }}-${{ github.ref }}
  13. cancel-in-progress: true
  14. env:
  15. GOPROXY: "https://proxy.golang.org"
  16. permissions:
  17. contents: write
  18. jobs:
  19. build:
  20. name: Build ${{ matrix.goos }}/${{ matrix.goarch }}${{ matrix.suffix }}
  21. runs-on: ubuntu-latest
  22. strategy:
  23. fail-fast: false
  24. matrix:
  25. include:
  26. - {goos: linux, goarch: amd64}
  27. - {goos: linux, goarch: arm64}
  28. - {goos: linux, goarch: "386"}
  29. - {goos: darwin, goarch: amd64}
  30. - {goos: darwin, goarch: arm64}
  31. - {goos: windows, goarch: amd64}
  32. - {goos: windows, goarch: arm64}
  33. - {goos: windows, goarch: "386"}
  34. - {goos: windows, goarch: amd64, suffix: "_mws", tags: minwinsvc}
  35. - {goos: windows, goarch: arm64, suffix: "_mws", tags: minwinsvc}
  36. - {goos: windows, goarch: "386", suffix: "_mws", tags: minwinsvc}
  37. steps:
  38. - name: Checkout code
  39. uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
  40. - name: Setup Go
  41. uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
  42. with:
  43. go-version: 1.25.x
  44. - name: Determine version
  45. id: version
  46. run: |
  47. if [ "${{ github.event_name }}" = "release" ]; then
  48. echo "version=${{ github.event.release.tag_name }}" | sed 's/version=v/version=/' >> "$GITHUB_OUTPUT"
  49. echo "release_tag=${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT"
  50. elif [ "${{ github.event_name }}" = "push" ] && [ "${{ github.ref }}" = "refs/heads/main" ]; then
  51. echo "version=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
  52. echo "release_tag=latest-commit-build" >> "$GITHUB_OUTPUT"
  53. else
  54. echo "version=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
  55. echo "release_tag=release-archive-testing" >> "$GITHUB_OUTPUT"
  56. fi
  57. - name: Build binary
  58. env:
  59. GOOS: ${{ matrix.goos }}
  60. GOARCH: ${{ matrix.goarch }}
  61. CGO_ENABLED: "0"
  62. run: |
  63. BINARY_NAME="gogs"
  64. if [ "${{ matrix.goos }}" = "windows" ]; then
  65. BINARY_NAME="gogs.exe"
  66. fi
  67. TAGS_FLAG=""
  68. if [ -n "${{ matrix.tags }}" ]; then
  69. TAGS_FLAG="-tags ${{ matrix.tags }}"
  70. fi
  71. go build -v \
  72. -ldflags "
  73. -X \"gogs.io/gogs/internal/conf.BuildTime=$(date -u '+%Y-%m-%d %I:%M:%S %Z')\"
  74. -X \"gogs.io/gogs/internal/conf.BuildCommit=$(git rev-parse HEAD)\"
  75. " \
  76. $TAGS_FLAG \
  77. -trimpath -o "$BINARY_NAME"
  78. - name: Prepare archive contents
  79. run: |
  80. mkdir -p dist/gogs
  81. BINARY_NAME="gogs"
  82. if [ "${{ matrix.goos }}" = "windows" ]; then
  83. BINARY_NAME="gogs.exe"
  84. fi
  85. cp "$BINARY_NAME" dist/gogs/
  86. cp LICENSE README.md README_ZH.md dist/gogs/
  87. cp -r scripts dist/gogs/
  88. - name: Create archives
  89. working-directory: dist
  90. run: |
  91. VERSION="${{ steps.version.outputs.version }}"
  92. ARCHIVE_BASE="gogs_${VERSION}_${{ matrix.goos }}_${{ matrix.goarch }}${{ matrix.suffix }}"
  93. zip -r "${ARCHIVE_BASE}.zip" gogs
  94. if [ "${{ matrix.goos }}" = "linux" ]; then
  95. tar -czvf "${ARCHIVE_BASE}.tar.gz" gogs
  96. fi
  97. - name: Upload to release
  98. env:
  99. GH_TOKEN: ${{ github.token }}
  100. run: |
  101. RELEASE_TAG="${{ steps.version.outputs.release_tag }}"
  102. if [ "${{ github.event_name }}" != "release" ]; then
  103. git tag -f "$RELEASE_TAG"
  104. git push origin "$RELEASE_TAG" --force || true
  105. RELEASE_TITLE="Release Archive Testing"
  106. RELEASE_NOTES="Automated testing release for workflow development."
  107. if [ "$RELEASE_TAG" = "latest-commit-build" ]; then
  108. RELEASE_TITLE="Latest Commit Build"
  109. RELEASE_NOTES="Automated build from the latest commit on main branch. This release is updated automatically with every push to main."
  110. fi
  111. gh release view "$RELEASE_TAG" || gh release create "$RELEASE_TAG" --title "$RELEASE_TITLE" --notes "$RELEASE_NOTES" --prerelease
  112. fi
  113. PATTERN="_${{ matrix.goos }}_${{ matrix.goarch }}${{ matrix.suffix }}\."
  114. gh release view "$RELEASE_TAG" --json assets --jq ".assets[].name" | grep "$PATTERN" | while read -r asset; do
  115. gh release delete-asset "$RELEASE_TAG" "$asset" --yes || true
  116. done
  117. gh release upload "$RELEASE_TAG" dist/gogs_*.zip --clobber
  118. if [ "${{ matrix.goos }}" = "linux" ]; then
  119. gh release upload "$RELEASE_TAG" dist/gogs_*.tar.gz --clobber
  120. fi
  121. notify-failure:
  122. name: Notify on failure
  123. runs-on: ubuntu-latest
  124. needs: [build]
  125. if: ${{ failure() && github.event_name == 'push' && github.ref == 'refs/heads/main' }}
  126. steps:
  127. - name: Send email on failure
  128. uses: unknwon/send-email-on-failure@89339a1bc93f4ad1d30f3b7e4911fcba985c9adb # v1
  129. with:
  130. smtp_username: ${{ secrets.SMTP_USERNAME }}
  131. smtp_password: ${{ secrets.SMTP_PASSWORD }}