From 05e330f33f47cf7b8f0dc37861a9890dca2707d1 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 15 Jul 2024 19:48:49 +0200 Subject: [PATCH] Configure CI to run tests and build/publish new release --- .forgejo/workflows/release.yaml | 86 +++++++++++++++++++++++++++++++++ .forgejo/workflows/tests.yaml | 14 ++++++ .gitignore | 2 + .pre-commit-config.yaml | 2 +- build.sh | 63 ++++++++++++++++++++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 .forgejo/workflows/release.yaml create mode 100644 .forgejo/workflows/tests.yaml create mode 100644 .gitignore create mode 100755 build.sh diff --git a/.forgejo/workflows/release.yaml b/.forgejo/workflows/release.yaml new file mode 100644 index 0000000..be6dc42 --- /dev/null +++ b/.forgejo/workflows/release.yaml @@ -0,0 +1,86 @@ +--- +name: Build and publish Debian package +on: [create] +jobs: + build: + runs-on: docker + container: + image: docker.io/brenard/debian-python-deb:latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Build Debian package + env: + MAINTAINER_NAME: ${{ vars.MAINTAINER_NAME }} + MAINTAINER_EMAIL: ${{ vars.MAINTAINER_EMAIL }} + DEBIAN_CODENAME: ${{ vars.DEBIAN_CODENAME }} + run: | + echo "${{ secrets.GPG_KEY }}" | base64 -d | gpg --import + ./build.sh + - 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 check-*/check_* release/ + md5sum release/* > md5sum.txt + sha512sum release/* > sha512sum.txt + mv md5sum.txt sha512sum.txt release/ + { + echo 'release_note<> "$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 new file mode 100644 index 0000000..e06551d --- /dev/null +++ b/.forgejo/workflows/tests.yaml @@ -0,0 +1,14 @@ +--- +name: Run tests +on: [push] +jobs: + tests: + runs-on: docker + container: + image: docker.io/brenard/python-pre-commit:latest + options: "--workdir /src" + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Run pre-commit + run: pre-commit run --all-files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b35e5c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +/.env diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ee9ee93..b581460 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,7 +11,7 @@ repos: - --skip="./.*,*.csv,*.json,*.ini,*.subject,*.txt,*.html,*.log,*.conf" - --quiet-level=2 - --ignore-regex=.*codespell-ignore$ - #- --write-changes # Uncomment to write changes + # - --write-changes # Uncomment to write changes exclude_types: [csv, json] - repo: https://github.com/pre-commit/mirrors-prettier rev: v2.7.1 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..33b594a --- /dev/null +++ b/build.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +QUIET_ARG="" +[[ "$1" == "--quiet" ]] && QUIET_ARG="--quiet" + +# Enter source directory +cd "$( dirname "$0" )" || exit + +CHECK_FILE="$( find "." -name 'check_*' ! -name '*~' -type f -executable | head -n 1 )" +PACKAGE_NAME="$( basename "$CHECK_FILE" | tr '_' '-' )" + +echo "Clean previous build..." +rm -fr dist + +echo "Detect version using git describe..." +VERSION="$( git describe --tags|sed 's/^[^0-9]*//' )" + +echo "Create building environemt..." +BDIR="dist/$PACKAGE_NAME-$VERSION" +mkdir -p "$BDIR" +RSYNC_ARG="" +[[ -z "$QUIET_ARG" ]] && RSYNC_ARG="-v" +rsync -a "$RSYNC_ARG" debian/ "$BDIR/debian/" +cp "$CHECK_FILE" "$BDIR/" + +echo "Set VERSION=$VERSION in gitdch using sed..." +sed -i "s/^VERSION *=.*$/VERSION = '$VERSION'/" "$BDIR/$( basename "$CHECK_FILE" )" + +if [[ -z "$DEBIAN_CODENAME" ]]; then + echo "Retrieve debian codename using lsb_release..." + DEBIAN_CODENAME=$( lsb_release -c -s ) +else + echo "Use debian codename from environment ($DEBIAN_CODENAME)" +fi + +echo "Generate debian changelog using gitdch..." +GITDCH_ARGS=('--verbose') +[[ -n "$QUIET_ARG" ]] && GITDCH_ARGS=('--warning') +if [[ -n "$MAINTAINER_NAME" ]]; then + echo "Use maintainer name from environment ($MAINTAINER_NAME)" + GITDCH_ARGS+=("--maintainer-name" "${MAINTAINER_NAME}") +fi +if [[ -n "$MAINTAINER_EMAIL" ]]; then + echo "Use maintainer email from environment ($MAINTAINER_EMAIL)" + GITDCH_ARGS+=("--maintainer-email" "$MAINTAINER_EMAIL") +fi +gitdch \ + --package-name "$PACKAGE_NAME" \ + --version "${VERSION}" \ + --code-name "$DEBIAN_CODENAME" \ + --output "$BDIR"/debian/changelog \ + --release-notes dist/release_notes.md \ + "${GITDCH_ARGS[@]}" + +if [[ -n "$MAINTAINER_NAME" ]] && [[ -n "$MAINTAINER_EMAIL" ]]; then + echo "Set Maintainer field in debian control file ($MAINTAINER_NAME <$MAINTAINER_EMAIL>)..." + sed -i "s/^Maintainer: .*$/Maintainer: $MAINTAINER_NAME <$MAINTAINER_EMAIL>/" \ + "$BDIR"/debian/control +fi + +echo "Build debian package..." +cd "$BDIR" || exit +dpkg-buildpackage