| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- name: Build Debian Package
- on:
- push: {}
- workflow_dispatch:
- inputs:
- version:
- description: 'Package version without "v" prefix (e.g. 2.5.3). Leave empty to derive from last tag.'
- required: false
- default: ''
- permissions:
- contents: write # needed to attach assets to a GitHub Release
- jobs:
- build-deb:
- name: Build .deb – ${{ matrix.distro }}
- runs-on: ubuntu-latest
- strategy:
- fail-fast: false
- matrix:
- include:
- - distro: ubuntu-24.04
- container: ubuntu:24.04
- - distro: ubuntu-26.04
- container: ubuntu:26.04
- - distro: debian-12
- container: debian:12
- - distro: debian-13
- container: debian:13
- container:
- image: ${{ matrix.container }}
- options: --user root
- steps:
- # ── 0. Bootstrap shell tools not present in minimal images ──────────
- - name: Install base utilities
- run: |
- export DEBIAN_FRONTEND=noninteractive
- apt-get update -qq
- apt-get install -y --no-install-recommends \
- git ca-certificates curl
- # ── 1. Checkout ──────────────────────────────────────────────────────
- - name: Checkout repository
- uses: actions/checkout@v7
- with:
- fetch-depth: 0
- - name: Mark repository safe for git
- run: |
- git config --global --add safe.directory "$GITHUB_WORKSPACE"
- # ── 2. Resolve package version ──────────────────────────────────────
- - name: Determine package version
- id: version
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: |
- EXAMPLE_VERSION=$(grep -E '^\s*VERSION=' example.env | head -n1 | cut -d'=' -f2- | tr -d '"\r' | xargs || true)
- if [ -n "${{ github.event.inputs.version }}" ]; then
- VERSION="${{ github.event.inputs.version }}"
- elif [ -n "$EXAMPLE_VERSION" ]; then
- VERSION="$EXAMPLE_VERSION"
- elif echo "${{ github.ref }}" | grep -q '^refs/tags/v'; then
- VERSION="${GITHUB_REF_NAME#v}"
- else
- LATEST_RELEASE_TAG=$(curl -sSfL \
- -H "Authorization: Bearer $GITHUB_TOKEN" \
- -H "Accept: application/vnd.github+json" \
- "https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/latest" 2>/dev/null \
- | grep -m1 '"tag_name"' \
- | sed -E 's/.*"tag_name"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/')
- if [ -n "$LATEST_RELEASE_TAG" ]; then
- VERSION="${LATEST_RELEASE_TAG#v}"
- else
- # --match 'v*' ensures CI-generated tags (ci-...) are never used
- LAST_TAG=$(git describe --tags --match 'v*' --abbrev=0 2>/dev/null || echo "v0.0.0")
- SHORT_SHA=$(git rev-parse --short HEAD)
- VERSION="${LAST_TAG#v}+git${SHORT_SHA}"
- fi
- fi
- echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
- echo "deb_version=${VERSION}-1" >> "$GITHUB_OUTPUT"
- echo "Packaging version: ${VERSION}"
- # ── 3. Install build-time dependencies ──────────────────────────────
- - name: Install build dependencies
- run: |
- export DEBIAN_FRONTEND=noninteractive
- apt-get update -qq
- apt-get install -y --no-install-recommends \
- debhelper \
- devscripts \
- fakeroot \
- lintian \
- build-essential \
- python3 \
- python3-pip \
- python3-venv \
- python3-dev \
- libssl-dev \
- libffi-dev \
- libpq-dev \
- libldap2-dev \
- libsasl2-dev \
- pkg-config
- # ── 4. Stamp debian/changelog with the current version ──────────────
- - name: Update debian/changelog
- env:
- VERSION: ${{ steps.version.outputs.version }}
- DEB_VERSION: ${{ steps.version.outputs.deb_version }}
- run: |
- MAINTAINER="Kaelthas <kaelthasmanu@users.noreply.github.com>"
- DATE=$(date -R)
- # Collect commits since previous tag (max 30 lines)
- PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
- if [ -n "$PREV_TAG" ]; then
- CHANGES=$(git log --pretty=format:" * %s" "${PREV_TAG}..HEAD" | head -30)
- else
- CHANGES=" * Initial Debian packaging."
- fi
- [ -z "$CHANGES" ] && CHANGES=" * No notable changes."
- # Write a fresh changelog (prepend to any existing one)
- {
- printf 'squidstats (%s) stable; urgency=low\n\n' "$DEB_VERSION"
- echo "$CHANGES"
- printf '\n -- %s %s\n' "$MAINTAINER" "$DATE"
- } > debian/changelog.new
- # Append old entries if they exist
- if [ -f debian/changelog ]; then
- cat debian/changelog >> debian/changelog.new
- fi
- mv debian/changelog.new debian/changelog
- echo "=== debian/changelog (first 10 lines) ==="
- head -10 debian/changelog
- # ── 5. Build the binary .deb package ────────────────────────────────
- - name: Build Debian package
- run: |
- chmod +x debian/rules debian/postinst debian/prerm debian/postrm
- dpkg-buildpackage -b -us -uc -rfakeroot 2>&1 | tee build.log
- echo "=== Built packages ==="
- ls -lh ../*.deb || ls -lh *.deb
- # ── 6. Validate with lintian ─────────────────────────────────────────
- - name: Run lintian
- continue-on-error: true # advisory – does not fail the build
- run: |
- DEB=$(ls ../*.deb 2>/dev/null | head -1 || ls *.deb 2>/dev/null | head -1)
- echo "Linting: $DEB"
- lintian --profile debian --tag-display-limit 0 "$DEB" \
- 2>&1 | tee lintian.txt || true
- echo "=== Lintian summary ==="
- grep -E '^(E|W|I|P):' lintian.txt | sort | uniq -c | sort -rn | head -30 || true
- # ── 7. Show package information ──────────────────────────────────────
- - name: Package info
- run: |
- DEB=$(ls ../*.deb 2>/dev/null | head -1 || ls *.deb 2>/dev/null | head -1)
- echo "=== dpkg-deb info ==="
- dpkg-deb --info "$DEB"
- echo ""
- echo "=== Installed file list (first 40) ==="
- dpkg-deb --contents "$DEB" | head -40
- # ── 8. Move .deb to workspace for upload steps ───────────────────────
- - name: Collect artifacts
- run: |
- mkdir -p dist
- DEB=$(ls ../*.deb 2>/dev/null | head -1 || ls *.deb 2>/dev/null | head -1)
- if [ -z "$DEB" ]; then
- echo "No Debian package found to collect." >&2
- exit 1
- fi
- cp "$DEB" dist/squidstats_${{ steps.version.outputs.version }}_${{ matrix.distro }}.deb
- ls -lh dist/
- # ── 9. Upload .deb as workflow artifact ──────────────────────────────
- - name: Upload .deb artifact
- uses: actions/upload-artifact@v7.0.1
- with:
- name: squidstats-${{ steps.version.outputs.version }}-${{ matrix.distro }}
- path: dist/squidstats_${{ steps.version.outputs.version }}_${{ matrix.distro }}.deb
- retention-days: 30
- # ── 10. Upload lintian report ─────────────────────────────────────────
- - name: Upload lintian report
- if: always()
- uses: actions/upload-artifact@v7.0.1
- with:
- name: lintian-${{ matrix.distro }}
- path: lintian.txt
- retention-days: 7
- if-no-files-found: ignore
- - name: Install GitHub CLI
- run: |
- export DEBIAN_FRONTEND=noninteractive
- apt-get update -qq
- apt-get install -y --no-install-recommends gh
- - name: Upload .deb to existing GitHub Release
- env:
- GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: |
- set -e
- REPO="${GITHUB_REPOSITORY}"
- if echo "${GITHUB_REF}" | grep -q '^refs/tags/v'; then
- RELEASE_ID=$(gh api repos/$REPO/releases/tags/${GITHUB_REF_NAME} --jq '.id' 2>/dev/null || true)
- TAG_NAME="${GITHUB_REF_NAME}"
- else
- RELEASE_ID=$(gh api repos/$REPO/releases/latest --jq '.id' 2>/dev/null || true)
- TAG_NAME=$(gh api repos/$REPO/releases/latest --jq '.tag_name' 2>/dev/null || true)
- fi
- if [ -z "$RELEASE_ID" ]; then
- echo "Failed to detect the existing release for $REPO." >&2
- echo "gh version:" >&2
- gh --version >&2 || true
- echo "gh auth status:" >&2
- gh auth status >&2 || true
- echo "Create a release manually first and re-run the workflow." >&2
- exit 1
- fi
- echo "Using release: $TAG_NAME (ID: $RELEASE_ID)"
- ASSET_NAME="squidstats_${{ steps.version.outputs.version }}_${{ matrix.distro }}.deb"
- EXISTING_ASSET_ID=$(gh api repos/$REPO/releases/$RELEASE_ID/assets --jq ".[] | select(.name==\"$ASSET_NAME\") | .id" 2>/dev/null || true)
- if [ -n "$EXISTING_ASSET_ID" ]; then
- echo "Deleting existing asset $ASSET_NAME ($EXISTING_ASSET_ID)"
- gh api repos/$REPO/releases/assets/$EXISTING_ASSET_ID -X DELETE
- fi
- gh release upload --clobber "$TAG_NAME" dist/$ASSET_NAME
|