build-rpm.yml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. name: Build RPM Package
  2. on:
  3. push: {}
  4. workflow_dispatch:
  5. inputs:
  6. version:
  7. description: 'Package version without "v" prefix (e.g. 2.6.1). Leave empty to derive from last tag.'
  8. required: false
  9. default: ''
  10. permissions:
  11. contents: write
  12. jobs:
  13. build-rpm:
  14. name: Build RPM – ${{ matrix.distro }}
  15. runs-on: ubuntu-latest
  16. container:
  17. image: ${{ matrix.container }}
  18. options: --user root
  19. strategy:
  20. fail-fast: false
  21. matrix:
  22. include:
  23. - distro: centos-stream-9
  24. container: quay.io/centos/centos:stream9
  25. - distro: fedora-40
  26. container: registry.fedoraproject.org/fedora:40
  27. steps:
  28. - name: Checkout repository
  29. uses: actions/checkout@v7
  30. with:
  31. fetch-depth: 0
  32. - name: Install git for workflow steps
  33. run: |
  34. dnf -y install git
  35. - name: Mark repository safe for git
  36. run: |
  37. git config --global --add safe.directory "$GITHUB_WORKSPACE"
  38. - name: Determine package version
  39. id: version
  40. env:
  41. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  42. run: |
  43. EXAMPLE_VERSION=$(grep -E '^\s*VERSION=' example.env | head -n1 | cut -d'=' -f2- | tr -d '"\r' | xargs || true)
  44. if [ -n "${{ github.event.inputs.version }}" ]; then
  45. VERSION="${{ github.event.inputs.version }}"
  46. elif [ -n "$EXAMPLE_VERSION" ]; then
  47. VERSION="$EXAMPLE_VERSION"
  48. elif echo "${{ github.ref }}" | grep -q '^refs/tags/v'; then
  49. VERSION="${GITHUB_REF_NAME#v}"
  50. else
  51. LATEST_RELEASE_TAG=$(curl -sSfL \
  52. -H "Authorization: Bearer $GITHUB_TOKEN" \
  53. -H "Accept: application/vnd.github+json" \
  54. "https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/latest" 2>/dev/null \
  55. | grep -m1 '"tag_name"' \
  56. | sed -E 's/.*"tag_name"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/')
  57. if [ -n "$LATEST_RELEASE_TAG" ]; then
  58. VERSION="${LATEST_RELEASE_TAG#v}"
  59. else
  60. LAST_TAG=$(git describe --tags --match 'v*' --abbrev=0 2>/dev/null || echo "v0.0.0")
  61. SHORT_SHA=$(git rev-parse --short HEAD)
  62. VERSION="${LAST_TAG#v}+git${SHORT_SHA}"
  63. fi
  64. fi
  65. RPM_VERSION=$(printf '%s' "$VERSION" | sed -E 's/\+/git./g; s/[^A-Za-z0-9_.-]+/./g; s/^\.+//; s/\.+$//; s/\.{2,}/./g')
  66. RPM_VERSION=${RPM_VERSION:-0.0.0}
  67. echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
  68. echo "rpm_version=${RPM_VERSION}" >> "$GITHUB_OUTPUT"
  69. echo "Packaging version: ${VERSION} (RPM: ${RPM_VERSION})"
  70. - name: Install build dependencies
  71. run: |
  72. dnf -y update
  73. dnf -y install \
  74. git ca-certificates \
  75. rpm-build createrepo redhat-rpm-config \
  76. python3 python3-pip python3-devel \
  77. openssl-devel libffi-devel libpq-devel openldap-devel \
  78. gcc make
  79. - name: Create RPM build tree
  80. run: |
  81. mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
  82. tar czf rpmbuild/SOURCES/squidstats-${{ steps.version.outputs.rpm_version }}.tar.gz \
  83. --transform 's?^?squidstats-${{ steps.version.outputs.rpm_version }}/?' \
  84. app.py config.py manage_db.py alembic.ini babel.cfg example.env requirements.txt \
  85. debian/conffiles debian/control debian/rules debian/squidstats.service \
  86. database parsers routes services static templates tools translations utils
  87. - name: Write RPM spec file
  88. run: |
  89. CHANGELOG_DATE=$(LC_ALL=C date -u '+%a %b %d %Y')
  90. cat > rpmbuild/SPECS/squidstats.spec <<'EOF'
  91. Name: squidstats
  92. Version: ${{ steps.version.outputs.rpm_version }}
  93. Release: 1%{?dist}
  94. %global debug_package %{nil}
  95. Summary: Web-based Squid proxy statistics and monitoring dashboard
  96. License: MIT
  97. URL: https://github.com/kaelthasmanu/SquidStats
  98. Source0: %{name}-%{version}.tar.gz
  99. BuildArch: x86_64
  100. BuildRequires: python3, python3-pip, python3-devel, openssl-devel, libffi-devel, libpq-devel, openldap-devel, gcc, make
  101. Requires: python3, python3-pip, shadow-utils
  102. Suggests: squid
  103. %description
  104. SquidStats is a Flask web application that provides real-time analysis and statistics for Squid proxy servers, including user monitoring, bandwidth tracking, access log analysis, and reporting.
  105. %prep
  106. %setup -q
  107. %build
  108. # No build step.
  109. %install
  110. rm -rf %{buildroot}
  111. mkdir -p %{buildroot}/opt/SquidStats/app
  112. mkdir -p %{buildroot}/opt/SquidStats
  113. mkdir -p %{buildroot}/etc/squidstats
  114. mkdir -p %{buildroot}/var/log/squidstats
  115. mkdir -p %{buildroot}/var/lib/squidstats
  116. cp -a app.py config.py manage_db.py alembic.ini babel.cfg requirements.txt %{buildroot}/opt/SquidStats/app/
  117. cp -a database parsers routes services static templates tools translations utils %{buildroot}/opt/SquidStats/app/
  118. cp -a example.env %{buildroot}/opt/SquidStats/app/.env
  119. install -D -m 0644 debian/squidstats.service %{buildroot}/usr/lib/systemd/system/squidstats.service
  120. %files
  121. %defattr(-,root,root,-)
  122. /opt/SquidStats
  123. /opt/SquidStats/app/.env
  124. /var/log/squidstats
  125. /var/lib/squidstats
  126. /usr/lib/systemd/system/squidstats.service
  127. %changelog
  128. * CHANGELOG_DATE Kaelthas <kaelthasmanu@users.noreply.github.com> - ${{ steps.version.outputs.rpm_version }}-1
  129. - Built by GitHub Actions RPM workflow.
  130. EOF
  131. sed -i "s/CHANGELOG_DATE/${CHANGELOG_DATE}/" rpmbuild/SPECS/squidstats.spec
  132. grep -E '^(Name|Version|Release|Source0):|^\* ' rpmbuild/SPECS/squidstats.spec
  133. - name: Build RPM package with rpmbuild
  134. run: |
  135. set -e
  136. SOURCE_ARCHIVE="rpmbuild/SOURCES/squidstats-${{ steps.version.outputs.rpm_version }}.tar.gz"
  137. test -s "$SOURCE_ARCHIVE"
  138. tar -tzf "$SOURCE_ARCHIVE" | grep -Fx "squidstats-${{ steps.version.outputs.rpm_version }}/example.env"
  139. rpmbuild --define "_topdir $PWD/rpmbuild" -ba rpmbuild/SPECS/squidstats.spec 2>&1 | tee build.log
  140. mkdir -p dist
  141. shopt -s nullglob
  142. # SOLO COPIAR ARCHIVOS BINARIOS .rpm, EXCLUIR .src.rpm
  143. RPM_FILES=(rpmbuild/RPMS/*/*.rpm)
  144. if [ ${#RPM_FILES[@]} -eq 0 ]; then
  145. echo "No RPM binary files were built. Listing rpmbuild output:"
  146. find rpmbuild -type f | sort
  147. exit 1
  148. fi
  149. cp "${RPM_FILES[@]}" dist/
  150. ls -lh dist
  151. for RPM in "${RPM_FILES[@]}"; do
  152. rpm -qip "$RPM"
  153. rpm -qpl "$RPM" | grep -Fx '/opt/SquidStats/app/.env'
  154. done
  155. - name: Collect artifacts
  156. run: |
  157. mkdir -p dist
  158. ls -lh dist
  159. - name: Upload RPM artifact
  160. uses: actions/upload-artifact@v7.0.1
  161. with:
  162. name: squidstats-${{ steps.version.outputs.rpm_version }}-${{ matrix.distro }}
  163. path: dist/*.rpm
  164. retention-days: 30
  165. if-no-files-found: ignore
  166. - name: Install GitHub CLI
  167. if: github.event_name != 'pull_request'
  168. run: |
  169. dnf -y install dnf-plugins-core 'dnf-command(config-manager)'
  170. dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
  171. dnf -y install gh
  172. - name: Upload RPM to existing GitHub Release
  173. if: github.event_name != 'pull_request'
  174. env:
  175. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  176. run: |
  177. set -e
  178. REPO="${GITHUB_REPOSITORY}"
  179. if echo "${GITHUB_REF}" | grep -q '^refs/tags/v'; then
  180. RELEASE_ID=$(gh api repos/$REPO/releases/tags/${GITHUB_REF_NAME} --jq '.id' 2>/dev/null || true)
  181. TAG_NAME="${GITHUB_REF_NAME}"
  182. else
  183. RELEASE_ID=$(gh api repos/$REPO/releases/latest --jq '.id' 2>/dev/null || true)
  184. TAG_NAME=$(gh api repos/$REPO/releases/latest --jq '.tag_name' 2>/dev/null || true)
  185. fi
  186. if [ -z "$RELEASE_ID" ]; then
  187. echo "Failed to detect the existing release for $REPO." >&2
  188. exit 1
  189. fi
  190. shopt -s nullglob
  191. RPM_FILES=(dist/*.rpm)
  192. if [ ${#RPM_FILES[@]} -eq 0 ]; then
  193. echo "No RPM artifacts found in dist; skipping GitHub Release upload."
  194. exit 0
  195. fi
  196. for RPM in "${RPM_FILES[@]}"; do
  197. ASSET_NAME=$(basename "$RPM")
  198. EXISTING_ASSET_ID=$(gh api repos/$REPO/releases/$RELEASE_ID/assets --jq ".[] | select(.name==\"$ASSET_NAME\") | .id" 2>/dev/null || true)
  199. if [ -n "$EXISTING_ASSET_ID" ]; then
  200. gh api repos/$REPO/releases/assets/$EXISTING_ASSET_ID -X DELETE
  201. fi
  202. gh release upload --repo "$REPO" --clobber "$TAG_NAME" "$RPM"
  203. done