From 70d7b08b7d4309d280af7db2d8799a74986b35aa Mon Sep 17 00:00:00 2001 From: icsboyx Date: Fri, 9 Jan 2026 21:25:23 +0100 Subject: [PATCH] refactor: update CI workflow and Dockerfile for improved build process --- .gitea/workflows/build.yaml | 192 +++++++++++++++++++++++++++--------- Dockerfile | 2 +- 2 files changed, 147 insertions(+), 47 deletions(-) diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 817f223..2629bd7 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -1,64 +1,164 @@ -name: Rust CI +name: Build & Publish on: push: - branches: [main] - pull_request: + branches: ["main"] + paths: + - "CHANGELOG.md" + workflow_dispatch: {} jobs: - build-test: + check: runs-on: ubuntu-latest - + outputs: + should_build: ${{ steps.version_check.outputs.should_build }} + version: ${{ steps.version_check.outputs.version }} + pkg_version: ${{ steps.version_check.outputs.pkg_version }} + short_sha: ${{ steps.version_check.outputs.short_sha }} + owner: ${{ steps.meta.outputs.owner }} + repo: ${{ steps.meta.outputs.repo }} steps: - name: Checkout uses: actions/checkout@v4 - - - name: Install Rust (stable) - uses: dtolnay/rust-toolchain@stable with: - components: rustfmt, clippy + fetch-depth: 2 - - name: Cache cargo - uses: actions/cache@v4 - with: - path: | - ~/.cargo/registry - ~/.cargo/git - target - key: cargo-${{ runner.os }}-${{ hashFiles('**/Cargo.lock') }} - - - name: Build (release) - run: cargo build --release - - - name: Upload artifact (binary) - uses: actions/upload-artifact@v3 - with: - name: bhs-linux - path: target/release/black_hole_share - - - name: Package for Gitea Packages - if: ${{ gitea.event_name == 'push' && gitea.ref_name == 'main' }} + - name: Repo meta (owner/repo) + id: meta + shell: bash run: | - set -euo pipefail + set -e + # Gitea Actions is GitHub-compatible; this usually exists. + FULL="${GITHUB_REPOSITORY:-}" + if [ -z "$FULL" ]; then + echo "GITHUB_REPOSITORY is empty. Set it in runner env or switch to explicit OWNER/REPO vars." + exit 1 + fi + OWNER="${FULL%%/*}" + REPO="${FULL##*/}" + echo "owner=$OWNER" >> "$GITHUB_OUTPUT" + echo "repo=$REPO" >> "$GITHUB_OUTPUT" + + - name: Check version change in CHANGELOG + id: version_check + shell: bash + run: | + set -e + + OLD=$(git show HEAD~1:CHANGELOG.md | grep '^## \[' | head -1 || true) + NEW=$(grep '^## \[' CHANGELOG.md | head -1 || true) + + echo "Old: $OLD" + echo "New: $NEW" + + # Extract x.y.z from: ## [x.y.z] - YYYY-MM-DD + VERSION=$(echo "$NEW" | sed -n 's/^## \[\([0-9]\+\.[0-9]\+\.[0-9]\+\)\].*$/\1/p') + + if [ -z "$VERSION" ]; then + echo "Could not parse version from CHANGELOG.md (expected: ## [x.y.z] - YYYY-MM-DD)" + exit 1 + fi + + SHORT_SHA="$(git rev-parse --short=7 HEAD)" + PKG_VERSION="${VERSION}+g${SHORT_SHA}" + + echo "Parsed VERSION=$VERSION" + echo "SHORT_SHA=$SHORT_SHA" + echo "PKG_VERSION=$PKG_VERSION" + + if [ "$OLD" = "$NEW" ]; then + echo "should_build=false" >> "$GITHUB_OUTPUT" + else + echo "should_build=true" >> "$GITHUB_OUTPUT" + fi + + echo "version=$VERSION" >> "$GITHUB_OUTPUT" + echo "short_sha=$SHORT_SHA" >> "$GITHUB_OUTPUT" + echo "pkg_version=$PKG_VERSION" >> "$GITHUB_OUTPUT" + + build_publish: + needs: check + if: needs.check.outputs.should_build == 'true' + runs-on: ubuntu-latest + container: + image: archlinux:latest + + steps: + - name: Install deps (Arch) + run: | + pacman -Syu --noconfirm --needed \ + base-devel git ca-certificates curl tar gzip \ + rust cargo + + - name: Checkout + uses: actions/checkout@v4 + + - name: Create source tarball (code) + shell: bash + run: | + set -e + OWNER="${{ needs.check.outputs.owner }}" + REPO="${{ needs.check.outputs.repo }}" + PKG_VERSION="${{ needs.check.outputs.pkg_version }}" + mkdir -p dist - cp target/release/black_hole_share dist/ - tar -czf dist/black_hole_share-linux.tar.gz -C dist black_hole_share + # Clean source snapshot of the repository at current commit + git archive --format=tar.gz \ + --prefix="${REPO}-${PKG_VERSION}/" \ + -o "dist/${REPO}-${PKG_VERSION}-source.tar.gz" \ + HEAD - - name: Publish to Gitea Packages (generic) - if: ${{ gitea.event_name == 'push' && gitea.ref_name == 'main' }} - env: - GITEA_TOKEN: ${{ secrets.GITEA }} + ls -lh dist + + # OPTIONAL: build binary and package it too + - name: Build (release) + shell: bash run: | - set -euo pipefail + set -e + cargo build --release - OWNER="${GITEA_REPOSITORY_OWNER:-icsboyx}" - SHA="${GITEA_SHA:-$(git rev-parse HEAD)}" + - name: Collect binary + shell: bash + run: | + set -e + REPO="${{ needs.check.outputs.repo }}" + PKG_VERSION="${{ needs.check.outputs.pkg_version }}" - PKG_NAME="bhs" - PKG_VERSION="${SHA}" - FILE="dist/black_hole_share-linux.tar.gz" + mkdir -p dist + cp "target/release/${REPO}" "dist/${REPO}-${PKG_VERSION}-linux-x86_64" - curl -L --fail \ - -H "Authorization: token ${GITEA_TOKEN}" \ - --upload-file "${FILE}" \ - "https://git.qosnet.it/api/packages/${OWNER}/generic/${PKG_NAME}/${PKG_VERSION}/black_hole_share-linux.tar.gz" + chmod +x "dist/${REPO}-${PKG_VERSION}-linux-x86_64" + ls -lh dist + + - name: Upload to Gitea Generic Packages + shell: bash + env: + GITEA_BASE_URL: ${{ vars.GITEA_BASE_URL }} + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + run: | + set -e + OWNER="${{ needs.check.outputs.owner }}" + REPO="${{ needs.check.outputs.repo }}" + PKG_VERSION="${{ needs.check.outputs.pkg_version }}" + + if [ -z "${GITEA_BASE_URL:-}" ]; then + echo "Missing vars.GITEA_BASE_URL (example: https://gitea.example.com)" + exit 1 + fi + if [ -z "${GITEA_TOKEN:-}" ]; then + echo "Missing secrets.GITEA_TOKEN" + exit 1 + fi + + # Choose a package name (keep stable). Here: repo name. + PACKAGE_NAME="$REPO" + + for FILE in dist/*; do + FILENAME="$(basename "$FILE")" + URL="${GITEA_BASE_URL}/api/packages/${OWNER}/generic/${PACKAGE_NAME}/${PKG_VERSION}/${FILENAME}" + echo "Uploading $FILENAME -> $URL" + curl -fsS -X PUT \ + -H "Authorization: token ${GITEA_TOKEN}" \ + --upload-file "$FILE" \ + "$URL" + done diff --git a/Dockerfile b/Dockerfile index cad59d8..8f4ca90 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,5 +29,5 @@ RUN ls -al ./ RUN cargo build --release RUN cp ./target/release/black_hole_share /usr/local/bin/black_hole_share -WORKDIR /data +WORKDIR / CMD [ "black_hole_share" ] \ No newline at end of file