From 78137b7588c51ae3f5f85402228e120296864c5a Mon Sep 17 00:00:00 2001
From: magmaus3 <magmaus3@disroot.org>
Date: Mon, 5 May 2025 16:33:46 +0200
Subject: [PATCH] feat: add alpine ci

---
 .forgejo/workflows/build-alpine.yml | 67 ++++++++++++++++++++++++++---
 alpine/APKBUILD                     |  7 +++
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/.forgejo/workflows/build-alpine.yml b/.forgejo/workflows/build-alpine.yml
index b1757a60..fadb98bc 100644
--- a/.forgejo/workflows/build-alpine.yml
+++ b/.forgejo/workflows/build-alpine.yml
@@ -1,6 +1,15 @@
 on: 
-  - workflow-dispatch
-  - push
+  workflow-dispatch:
+  push:
+    paths-ignore:
+      - "*.md"
+      - "**/*.md"
+      - ".gitlab-ci.yml"
+      - ".gitignore"
+      - "renovate.json"
+      - "debian/**"
+      - "docker/**"
+      - "docs/**"
 
 jobs:
   build:
@@ -13,7 +22,15 @@ jobs:
         run: |
           apk update 
           apk upgrade 
-          apk add nodejs git alpine-sdk
+          apk add nodejs git alpine-sdk curl
+
+      - name: cache alpine repo 
+        uses: actions/cache@v3
+        with:
+          path: |
+            /var/cache/apk
+          key: alpine-cache
+
       - uses: actions/checkout@v4
         name: checkout the alpine dir  
         with: 
@@ -35,6 +52,7 @@ jobs:
           echo $HOME
           echo 'PACKAGER_PRIVKEY="/root/.abuild/ci@continuwuity.rsa"' > ~/.abuild/abuild.conf 
           ls ~/.abuild
+          cp /root/.abuild/*.rsa.pub /etc/apk/keys
 
       - name: go go gadget abuild 
         run: |
@@ -42,8 +60,43 @@ jobs:
           # modify the APKBUILD to use the current branch instead of the release
           # note that it seems to require the repo to be public (as you'll get 
           # a 404 even if the token is provided)
-          export ARCHIVE_URL="${{ github.server_url }}/${{ github.repository }}/archive/${{ github.ref_name }}.tar.gz"
-          echo $ARCHIVE_URL
-          sed -i '/^source=/c\source="'"$ARCHIVE_URL" APKBUILD 
+          export ARCHIVE_URL="https:${{ github.repository }}/archive/${{ github.ref_name }}.tar.gz"
+          export COMMIT_DATE=$(date -d @$(git show --no-patch --format=%ct HEAD) +%Y%m%d%H%M)
+          echo $ARCHIVE_URL $COMMIT_DATE
+
+          # modify the APKBUILD to use the git archive tarball and version 
+          #
+          sed -i '/^source=/c\source="'"$ARCHIVE_URL" APKBUILD
+          sed -i "/^pkgver=/s/$/_git${COMMIT_DATE}/" APKBUILD 
           abuild -F checksum
-          abuild -Fr
+          abuild -FrK
+
+      # - name: Cache Rust registry
+      #   uses: actions/cache@v3
+      #   with:
+      #     path: |
+      #       .cargo/git
+      #       .cargo/git/checkouts
+      #       .cargo/registry
+      #       .cargo/registry/src
+      #     key: rust-registry-image-${{hashFiles('**/Cargo.lock') }}
+      # - name: Cache cargo target
+      #   id: cache-cargo-target
+      #   uses: actions/cache@v3
+      #   with:
+      #     path: |
+      #       cargo-target-${{ matrix.slug }}
+      #     key: cargo-target-${{ matrix.slug }}-${{hashFiles('**/Cargo.lock') }}-${{steps.rust-toolchain.outputs.rustc_version}}
+
+
+      - name: upload to registry 
+        run: |
+          curl --header 'Authorization: token ${{ secrets.FORGEJO_TOKEN }}' \
+            --upload-file /root/packages/**/**/*.apk \
+            ${{ env.GITHUB_API_URL }}/packages/${{ env.GITHUB_REPOSITORY_OWNER }}/alpine/edge/git 
+      # - name: 'debug state on failure (note: only for local testing)'
+      #   if: always()
+      #   run: |
+      #     env | tee /tmp/env
+      #     sleep 1d || true
+
diff --git a/alpine/APKBUILD b/alpine/APKBUILD
index 97f84f65..3b9653b3 100644
--- a/alpine/APKBUILD
+++ b/alpine/APKBUILD
@@ -23,9 +23,16 @@ source="https://forgejo.ellis.link/continuwuation/continuwuity/archive/v0.5.0-rc
 continuwuity.initd
 continuwuity.confd
 "
+_giturl="https://forgejo.ellis.link/continuwuation/continuwuity"
+_gitbranch="main"
 builddir="$srcdir/continuwuity"
 options="net !check"
 
+#snapshot() {
+#    # used for building from git 
+#    git clone --depth=1 $_giturl -b $_gitbranch   
+#}
+
 prepare() {
 	default_prepare
 	cd $srcdir/continuwuity