Files
watcheragent/.github/workflows/build.yml
2025-08-02 17:31:40 +02:00

195 lines
5.9 KiB
YAML

name: Rust Cross-Platform Build
on:
workflow_dispatch:
push:
branches: [ "development", "main", "feature/*", bug/* ]
pull_request:
branches: [ "development", "main" ]
env:
CARGO_TERM_COLOR: always
DOCKER_IMAGE_NAME: "watcher-agent"
jobs:
detect-project:
name: Detect Rust Project
runs-on: ubuntu-latest
outputs:
project-dir: ${{ steps.detect.outputs.project-dir }}
project-name: ${{ steps.detect.outputs.project-name }}
steps:
- uses: actions/checkout@v4
- name: Find Cargo.toml
id: detect
run: |
# Look for Cargo.toml in root or subdirectories
if [ -f "Cargo.toml" ]; then
echo "project-dir=." >> $GITHUB_OUTPUT
PROJECT_NAME=$(grep -m1 '^name =' "Cargo.toml" | cut -d'"' -f2)
echo "project-name=${PROJECT_NAME}" >> $GITHUB_OUTPUT
else
for dir in */; do
if [ -f "$dir/Cargo.toml" ]; then
echo "project-dir=${dir%/}" >> $GITHUB_OUTPUT
PROJECT_NAME=$(grep -m1 '^name =' "$dir/Cargo.toml" | cut -d'"' -f2)
echo "project-name=${PROJECT_NAME}" >> $GITHUB_OUTPUT
break
fi
done
fi
if [ -z "$PROJECT_NAME" ]; then
echo "No Rust project found!"
exit 1
fi
native-build:
name: Native Linux Build
needs: detect-project
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-unknown-linux-gnu
profile: minimal
override: true
- name: Build Linux binary
id: build
working-directory: ${{ needs.detect-project.outputs.project-dir }}
run: |
set -e
cargo build --release --target x86_64-unknown-linux-gnu
binary_name=$(grep -m1 '^name =' Cargo.toml | cut -d'"' -f2)
echo "binary_name=$binary_name" >> $GITHUB_OUTPUT
- name: Strip debug symbols
if: success()
working-directory: ${{ needs.detect-project.outputs.project-dir }}
run: |
set -e
strip target/x86_64-unknown-linux-gnu/release/$(grep -m1 '^name =' Cargo.toml | cut -d'"' -f2)
- name: Upload Linux artifact
uses: actions/upload-artifact@v3
with:
name: linux-binary
path: ${{ steps.build-linux.outputs.binary_path }}
- name: Cleanup on failure
if: failure()
run: |
echo "Cleaning up Docker images..."
docker system prune -a -f || echo "Docker cleanup failed"
windows-cross:
name: Windows Cross-Compile
needs: detect-project
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v3
- name: Install Rust and Windows target
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-pc-windows-gnu
profile: minimal
override: true
- name: Install mingw cross-compiler
run: |
set -e
sudo apt-get update && sudo apt-get install -y gcc-mingw-w64-x86-64
- name: Build Windows binary
working-directory: ${{ needs.detect-project.outputs.project-dir }}
id: build
run: |
set -e
cargo build --release --target x86_64-pc-windows-gnu
binary_name=$(grep -m1 '^name =' Cargo.toml | cut -d'"' -f2)
x86_64-w64-mingw32-strip target/x86_64-pc-windows-gnu/release/$binary_name.exe
echo "binary_name=$binary_name" >> $GITHUB_OUTPUT
- name: Upload Windows artifact
uses: actions/upload-artifact@v3
with:
name: windows-binary
path: ${{ steps.build-windows.outputs.binary_path }}
- name: Cleanup on failure
if: failure()
run: |
echo "Cleaning up Docker images..."
docker system prune -a -f || echo "Docker cleanup failed"
docker-build:
name: Build Docker Images
needs: [native-build, windows-cross]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Download Linux artifact
uses: actions/download-artifact@v3
with:
name: linux-binary
path: linux-bin
- name: Download Windows artifact
uses: actions/download-artifact@v3
with:
name: windows-binary
path: windows-bin
- name: Verify artifacts
run: |
echo "Linux binary:"
ls -la linux-bin/
echo "Windows binary:"
ls -la windows-bin/
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build Linux Docker image
if: ${{ always() && steps.download-linux.outcome == 'success' }}
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile.linux
platforms: linux/amd64
push: false
tags: ${{ env.DOCKER_IMAGE_NAME }}:linux-latest
build-args: |
BINARY_PATH=linux-bin/${{ needs.detect-project.outputs.project-name }}
- name: Build Windows Docker image
if: ${{ always() && steps.download-windows.outcome == 'success' }}
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile.windows
platforms: windows-amd64
push: false
tags: ${{ env.DOCKER_IMAGE_NAME }}:windows-latest
build-args: |
BINARY_PATH=windows-bin/${{ needs.detect-project.outputs.project-name }}.exe
cleanup:
name: Cleanup
if: always()
needs: [docker-build, native-build, windows-cross]
runs-on: ubuntu-latest
steps:
- name: Cleanup Docker images
if: contains(needs.*.result, 'failure')
run: docker system prune -a -f