refactor: update CI workflow and Dockerfile for improved build process
This commit is contained in:
@@ -1,64 +1,164 @@
|
|||||||
name: Rust CI
|
name: Build & Publish
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: ["main"]
|
||||||
pull_request:
|
paths:
|
||||||
|
- "CHANGELOG.md"
|
||||||
|
workflow_dispatch: {}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-test:
|
check:
|
||||||
runs-on: ubuntu-latest
|
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:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Rust (stable)
|
|
||||||
uses: dtolnay/rust-toolchain@stable
|
|
||||||
with:
|
with:
|
||||||
components: rustfmt, clippy
|
fetch-depth: 2
|
||||||
|
|
||||||
- name: Cache cargo
|
- name: Repo meta (owner/repo)
|
||||||
uses: actions/cache@v4
|
id: meta
|
||||||
with:
|
shell: bash
|
||||||
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' }}
|
|
||||||
run: |
|
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
|
mkdir -p dist
|
||||||
cp target/release/black_hole_share dist/
|
# Clean source snapshot of the repository at current commit
|
||||||
tar -czf dist/black_hole_share-linux.tar.gz -C dist black_hole_share
|
git archive --format=tar.gz \
|
||||||
|
--prefix="${REPO}-${PKG_VERSION}/" \
|
||||||
|
-o "dist/${REPO}-${PKG_VERSION}-source.tar.gz" \
|
||||||
|
HEAD
|
||||||
|
|
||||||
- name: Publish to Gitea Packages (generic)
|
ls -lh dist
|
||||||
if: ${{ gitea.event_name == 'push' && gitea.ref_name == 'main' }}
|
|
||||||
env:
|
# OPTIONAL: build binary and package it too
|
||||||
GITEA_TOKEN: ${{ secrets.GITEA }}
|
- name: Build (release)
|
||||||
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -e
|
||||||
|
cargo build --release
|
||||||
|
|
||||||
OWNER="${GITEA_REPOSITORY_OWNER:-icsboyx}"
|
- name: Collect binary
|
||||||
SHA="${GITEA_SHA:-$(git rev-parse HEAD)}"
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -e
|
||||||
|
REPO="${{ needs.check.outputs.repo }}"
|
||||||
|
PKG_VERSION="${{ needs.check.outputs.pkg_version }}"
|
||||||
|
|
||||||
PKG_NAME="bhs"
|
mkdir -p dist
|
||||||
PKG_VERSION="${SHA}"
|
cp "target/release/${REPO}" "dist/${REPO}-${PKG_VERSION}-linux-x86_64"
|
||||||
FILE="dist/black_hole_share-linux.tar.gz"
|
|
||||||
|
|
||||||
curl -L --fail \
|
chmod +x "dist/${REPO}-${PKG_VERSION}-linux-x86_64"
|
||||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
ls -lh dist
|
||||||
--upload-file "${FILE}" \
|
|
||||||
"https://git.qosnet.it/api/packages/${OWNER}/generic/${PKG_NAME}/${PKG_VERSION}/black_hole_share-linux.tar.gz"
|
- 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
|
||||||
|
|||||||
@@ -29,5 +29,5 @@ RUN ls -al ./
|
|||||||
RUN cargo build --release
|
RUN cargo build --release
|
||||||
RUN cp ./target/release/black_hole_share /usr/local/bin/black_hole_share
|
RUN cp ./target/release/black_hole_share /usr/local/bin/black_hole_share
|
||||||
|
|
||||||
WORKDIR /data
|
WORKDIR /
|
||||||
CMD [ "black_hole_share" ]
|
CMD [ "black_hole_share" ]
|
||||||
Reference in New Issue
Block a user