From e2d060518e39364aad03c071cf427cd7b591ba71 Mon Sep 17 00:00:00 2001
From: Benjamin Renard <brenard@easter-eggs.com>
Date: Sun, 23 Feb 2025 10:46:28 +0100
Subject: [PATCH] Configure CI to build & publish Debian package on release

---
 .chglog.yml                     |  7 +++
 .forgejo/workflows/release.yaml | 91 +++++++++++++++++++++++++++++++++
 .forgejo/workflows/tests.yaml   |  6 ++-
 .gitignore                      |  2 +
 nfpm.yaml                       | 23 +++++++++
 5 files changed, 127 insertions(+), 2 deletions(-)
 create mode 100644 .chglog.yml
 create mode 100644 .forgejo/workflows/release.yaml
 create mode 100644 nfpm.yaml

diff --git a/.chglog.yml b/.chglog.yml
new file mode 100644
index 0000000..cc31273
--- /dev/null
+++ b/.chglog.yml
@@ -0,0 +1,7 @@
+conventional-commits: false
+deb:
+    distribution: stable
+    urgency: stable
+debug: false
+owner: Benjamin Renard <brenard@zionetrix.net>
+package-name: aptly-publish
diff --git a/.forgejo/workflows/release.yaml b/.forgejo/workflows/release.yaml
new file mode 100644
index 0000000..460f1cd
--- /dev/null
+++ b/.forgejo/workflows/release.yaml
@@ -0,0 +1,91 @@
+---
+name: Build and publish Debian package
+on: [create]
+jobs:
+  build:
+    runs-on: docker
+    container:
+      image: docker.io/brenard/nfpm:latest
+    steps:
+      - name: Check out repository code
+        uses: actions/checkout@v4
+        with:
+          fetch-depth: 0
+      - name: Build Debian package
+        run: |
+          echo "${{ secrets.GPG_KEY }}" | base64 -d > key.gpg
+          VERSION="$( git describe --tags | sed 's/^[^0-9]*//' )"
+          yq eval '.version = strenv(VERSION)' nfpm.yaml -i
+          yq eval '.deb.signature.key_file = "key.gpg"' nfpm.yaml -i
+          yq eval '.maintainer = "${{ vars.MAINTAINER_NAME }} <${{ vars.MAINTAINER_EMAIL }}>"' nfpm.yaml -i
+          yq eval '.deb.distribution = "${{ vars.DEBIAN_CODENAME }}"' .chglog.yml -i
+          chglog init
+          mkdir dist
+          chglog format --template release -o dist/release_notes.md
+          nfpm -f nfpm.yaml package --packager deb --target dist
+          cp aptly-publish dist
+      - name: Upload Debian package files
+        uses: actions/upload-artifact@v3
+        with:
+          name: dist
+          path: |
+            dist
+
+  publish-forgejo:
+    runs-on: docker
+    container:
+      image: docker.io/brenard/debian-python-deb:latest
+    needs:
+      - build
+    steps:
+      - name: Download Debian package files
+        uses: actions/download-artifact@v3
+        with:
+          name: dist
+
+      - name: Create the release
+        id: create-release
+        shell: bash
+        run: |
+          mkdir release
+          mv *.deb release/
+          mv aptly-publish release/
+          md5sum release/* > md5sum.txt
+          sha512sum release/* > sha512sum.txt
+          mv md5sum.txt sha512sum.txt release/
+          {
+            echo 'release_note<<EOF'
+            cat release_notes.md | sed 's/"/\\"/g'
+            echo 'EOF'
+          } >> "$GITHUB_OUTPUT"
+
+      - name: Publish release on Forgejo
+        uses: actions/forgejo-release@v2
+        with:
+          direction: upload
+          url: https://gitea.zionetrix.net
+          token: ${{ secrets.forgejo_token }}
+          release-dir: release
+          release-notes: ${{ steps.create-release.outputs.release_note }}
+
+  publish-aptly:
+    runs-on: docker
+    container:
+      image: docker.io/brenard/aptly-publish:latest
+    needs:
+      - build
+    steps:
+      - name: "Download Debian package files"
+        uses: actions/download-artifact@v3
+        with:
+          name: dist
+
+      - name: "Publish Debian package on Aptly repository"
+        uses: https://gitea.zionetrix.net/bn8/aptly-publish@master
+        with:
+          api_url: ${{ vars.apt_api_url }}
+          api_username: ${{ vars.apt_api_username }}
+          api_password: ${{ secrets.apt_api_password }}
+          repo_name: ${{ vars.apt_repo_name }}
+          path: "./"
+          source_name: ${{ vars.apt_source_name }}
diff --git a/.forgejo/workflows/tests.yaml b/.forgejo/workflows/tests.yaml
index d5f692a..621367f 100644
--- a/.forgejo/workflows/tests.yaml
+++ b/.forgejo/workflows/tests.yaml
@@ -5,13 +5,15 @@ jobs:
   test-precommit:
     runs-on: docker
     container:
-      image: docker.io/brenard/python-pre-commit:latest
+      image: docker.io/library/node:16-alpine
     steps:
       - name: Check out repository code
         uses: actions/checkout@v4
         with:
           fetch-depth: 0
       - name: Install python dependencies
-        run: python3 -m pip install --break-system-packages debian-parser requests urllib3
+        run: apk -Uuv add python3 py3-requests py3-urllib3 py3-pip py3-debian py3-chardet
+      - name: Install pre-commit
+        run: pip install pre-commit
       - name: Run pre-commit
         run: pre-commit run --all-files
diff --git a/.gitignore b/.gitignore
index 3d0d534..444786d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
 *~
 /venv
 /dist
+/changelog.yml
+*.gpg
diff --git a/nfpm.yaml b/nfpm.yaml
new file mode 100644
index 0000000..42de359
--- /dev/null
+++ b/nfpm.yaml
@@ -0,0 +1,23 @@
+# yaml-language-server: $schema=https://nfpm.goreleaser.com/static/schema.json
+# vim: set ts=2 sw=2 tw=0 fo=cnqoj
+
+name: aptly-publish
+arch: all
+platform: linux
+version: 2025.2.1
+section: devel
+priority: optional
+depends:
+  - python3
+  - python3-requests
+  - python3-urllib3
+  - python3-debian
+maintainer: Benjamin Renard <brenard@zionetrix.net>
+description: Tool to publish one (or more) Debian package on a Aptly repository using its API
+homepage: https://gitea.zionetrix.net/bn8/aptly-publish
+license: GPL-3
+# Changelog YAML file, see: https://github.com/goreleaser/chglog
+changelog: changelog.yml
+contents:
+  - src: ./aptly-publish
+    dst: /usr/bin