build-deb.yml 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. name: Build Debian 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 # needed to attach assets to a GitHub Release
  12. jobs:
  13. build-deb:
  14. name: Build .deb – ${{ matrix.distro }}
  15. runs-on: ubuntu-latest
  16. strategy:
  17. fail-fast: false
  18. matrix:
  19. include:
  20. - distro: ubuntu-24.04
  21. container: ubuntu:24.04
  22. - distro: ubuntu-26.04
  23. container: ubuntu:26.04
  24. - distro: debian-12
  25. container: debian:12
  26. - distro: debian-13
  27. container: debian:13
  28. container:
  29. image: ${{ matrix.container }}
  30. options: --user root
  31. steps:
  32. # ── 0. Bootstrap shell tools not present in minimal images ──────────
  33. - name: Install base utilities
  34. run: |
  35. export DEBIAN_FRONTEND=noninteractive
  36. apt-get update -qq
  37. apt-get install -y --no-install-recommends \
  38. git ca-certificates curl
  39. # ── 1. Checkout ──────────────────────────────────────────────────────
  40. - name: Checkout repository
  41. uses: actions/checkout@v7
  42. with:
  43. fetch-depth: 0
  44. - name: Mark repository safe for git
  45. run: |
  46. git config --global --add safe.directory "$GITHUB_WORKSPACE"
  47. # ── 2. Resolve package version ──────────────────────────────────────
  48. - name: Determine package version
  49. id: version
  50. env:
  51. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  52. run: |
  53. EXAMPLE_VERSION=$(grep -E '^\s*VERSION=' example.env | head -n1 | cut -d'=' -f2- | tr -d '"\r' | xargs || true)
  54. if [ -n "${{ github.event.inputs.version }}" ]; then
  55. VERSION="${{ github.event.inputs.version }}"
  56. elif [ -n "$EXAMPLE_VERSION" ]; then
  57. VERSION="$EXAMPLE_VERSION"
  58. elif echo "${{ github.ref }}" | grep -q '^refs/tags/v'; then
  59. VERSION="${GITHUB_REF_NAME#v}"
  60. else
  61. LATEST_RELEASE_TAG=$(curl -sSfL \
  62. -H "Authorization: Bearer $GITHUB_TOKEN" \
  63. -H "Accept: application/vnd.github+json" \
  64. "https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/latest" 2>/dev/null \
  65. | grep -m1 '"tag_name"' \
  66. | sed -E 's/.*"tag_name"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/')
  67. if [ -n "$LATEST_RELEASE_TAG" ]; then
  68. VERSION="${LATEST_RELEASE_TAG#v}"
  69. else
  70. # --match 'v*' ensures CI-generated tags (ci-...) are never used
  71. LAST_TAG=$(git describe --tags --match 'v*' --abbrev=0 2>/dev/null || echo "v0.0.0")
  72. SHORT_SHA=$(git rev-parse --short HEAD)
  73. VERSION="${LAST_TAG#v}+git${SHORT_SHA}"
  74. fi
  75. fi
  76. echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
  77. echo "deb_version=${VERSION}-1" >> "$GITHUB_OUTPUT"
  78. echo "Packaging version: ${VERSION}"
  79. # ── 3. Install build-time dependencies ──────────────────────────────
  80. - name: Install build dependencies
  81. run: |
  82. export DEBIAN_FRONTEND=noninteractive
  83. apt-get update -qq
  84. apt-get install -y --no-install-recommends \
  85. debhelper \
  86. devscripts \
  87. fakeroot \
  88. lintian \
  89. build-essential \
  90. python3 \
  91. python3-pip \
  92. python3-venv \
  93. python3-dev \
  94. libssl-dev \
  95. libffi-dev \
  96. libpq-dev \
  97. libldap2-dev \
  98. libsasl2-dev \
  99. pkg-config
  100. # ── 4. Stamp debian/changelog with the current version ──────────────
  101. - name: Update debian/changelog
  102. env:
  103. VERSION: ${{ steps.version.outputs.version }}
  104. DEB_VERSION: ${{ steps.version.outputs.deb_version }}
  105. run: |
  106. MAINTAINER="Kaelthas <kaelthasmanu@users.noreply.github.com>"
  107. DATE=$(date -R)
  108. # Collect commits since previous tag (max 30 lines)
  109. PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
  110. if [ -n "$PREV_TAG" ]; then
  111. CHANGES=$(git log --pretty=format:" * %s" "${PREV_TAG}..HEAD" | head -30)
  112. else
  113. CHANGES=" * Initial Debian packaging."
  114. fi
  115. [ -z "$CHANGES" ] && CHANGES=" * No notable changes."
  116. # Write a fresh changelog (prepend to any existing one)
  117. {
  118. printf 'squidstats (%s) stable; urgency=low\n\n' "$DEB_VERSION"
  119. echo "$CHANGES"
  120. printf '\n -- %s %s\n' "$MAINTAINER" "$DATE"
  121. } > debian/changelog.new
  122. # Append old entries if they exist
  123. if [ -f debian/changelog ]; then
  124. cat debian/changelog >> debian/changelog.new
  125. fi
  126. mv debian/changelog.new debian/changelog
  127. echo "=== debian/changelog (first 10 lines) ==="
  128. head -10 debian/changelog
  129. # ── 5. Build the binary .deb package ────────────────────────────────
  130. - name: Build Debian package
  131. run: |
  132. chmod +x debian/rules debian/postinst debian/prerm debian/postrm
  133. dpkg-buildpackage -b -us -uc -rfakeroot 2>&1 | tee build.log
  134. echo "=== Built packages ==="
  135. ls -lh ../*.deb || ls -lh *.deb
  136. # ── 6. Validate with lintian ─────────────────────────────────────────
  137. - name: Run lintian
  138. continue-on-error: true # advisory – does not fail the build
  139. run: |
  140. DEB=$(ls ../*.deb 2>/dev/null | head -1 || ls *.deb 2>/dev/null | head -1)
  141. echo "Linting: $DEB"
  142. lintian --profile debian --tag-display-limit 0 "$DEB" \
  143. 2>&1 | tee lintian.txt || true
  144. echo "=== Lintian summary ==="
  145. grep -E '^(E|W|I|P):' lintian.txt | sort | uniq -c | sort -rn | head -30 || true
  146. # ── 7. Show package information ──────────────────────────────────────
  147. - name: Package info
  148. run: |
  149. DEB=$(ls ../*.deb 2>/dev/null | head -1 || ls *.deb 2>/dev/null | head -1)
  150. echo "=== dpkg-deb info ==="
  151. dpkg-deb --info "$DEB"
  152. echo ""
  153. echo "=== Installed file list (first 40) ==="
  154. dpkg-deb --contents "$DEB" | head -40
  155. # ── 8. Move .deb to workspace for upload steps ───────────────────────
  156. - name: Collect artifacts
  157. run: |
  158. mkdir -p dist
  159. DEB=$(ls ../*.deb 2>/dev/null | head -1 || ls *.deb 2>/dev/null | head -1)
  160. if [ -z "$DEB" ]; then
  161. echo "No Debian package found to collect." >&2
  162. exit 1
  163. fi
  164. cp "$DEB" dist/squidstats_${{ steps.version.outputs.version }}_${{ matrix.distro }}.deb
  165. ls -lh dist/
  166. # ── 9. Upload .deb as workflow artifact ──────────────────────────────
  167. - name: Upload .deb artifact
  168. uses: actions/upload-artifact@v7.0.1
  169. with:
  170. name: squidstats-${{ steps.version.outputs.version }}-${{ matrix.distro }}
  171. path: dist/squidstats_${{ steps.version.outputs.version }}_${{ matrix.distro }}.deb
  172. retention-days: 30
  173. # ── 10. Upload lintian report ─────────────────────────────────────────
  174. - name: Upload lintian report
  175. if: always()
  176. uses: actions/upload-artifact@v7.0.1
  177. with:
  178. name: lintian-${{ matrix.distro }}
  179. path: lintian.txt
  180. retention-days: 7
  181. if-no-files-found: ignore
  182. - name: Install GitHub CLI
  183. run: |
  184. export DEBIAN_FRONTEND=noninteractive
  185. apt-get update -qq
  186. apt-get install -y --no-install-recommends gh
  187. - name: Upload .deb to existing GitHub Release
  188. env:
  189. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  190. run: |
  191. set -e
  192. REPO="${GITHUB_REPOSITORY}"
  193. if echo "${GITHUB_REF}" | grep -q '^refs/tags/v'; then
  194. RELEASE_ID=$(gh api repos/$REPO/releases/tags/${GITHUB_REF_NAME} --jq '.id' 2>/dev/null || true)
  195. TAG_NAME="${GITHUB_REF_NAME}"
  196. else
  197. RELEASE_ID=$(gh api repos/$REPO/releases/latest --jq '.id' 2>/dev/null || true)
  198. TAG_NAME=$(gh api repos/$REPO/releases/latest --jq '.tag_name' 2>/dev/null || true)
  199. fi
  200. if [ -z "$RELEASE_ID" ]; then
  201. echo "Failed to detect the existing release for $REPO." >&2
  202. echo "gh version:" >&2
  203. gh --version >&2 || true
  204. echo "gh auth status:" >&2
  205. gh auth status >&2 || true
  206. echo "Create a release manually first and re-run the workflow." >&2
  207. exit 1
  208. fi
  209. echo "Using release: $TAG_NAME (ID: $RELEASE_ID)"
  210. ASSET_NAME="squidstats_${{ steps.version.outputs.version }}_${{ matrix.distro }}.deb"
  211. EXISTING_ASSET_ID=$(gh api repos/$REPO/releases/$RELEASE_ID/assets --jq ".[] | select(.name==\"$ASSET_NAME\") | .id" 2>/dev/null || true)
  212. if [ -n "$EXISTING_ASSET_ID" ]; then
  213. echo "Deleting existing asset $ASSET_NAME ($EXISTING_ASSET_ID)"
  214. gh api repos/$REPO/releases/assets/$EXISTING_ASSET_ID -X DELETE
  215. fi
  216. gh release upload --clobber "$TAG_NAME" dist/$ASSET_NAME