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

193 lines
6.1 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: |
for dir in */; do
if [ -f "$dir/Cargo.toml" ]; then
PROJECT_DIR="$dir"
PROJECT_NAME=$(grep -m1 '^name =' "$dir/Cargo.toml" | cut -d'"' -f2)
echo "project-dir=${PROJECT_DIR%/}" >> $GITHUB_OUTPUT
echo "project-name=${PROJECT_NAME}" >> $GITHUB_OUTPUT
break
fi
done
if [ -z "$PROJECT_DIR" ]; 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
if: success()
uses: actions/upload-artifact@v3
with:
name: ${{ needs.detect-project.outputs.project-dir }}-linux
path: ${{ needs.detect-project.outputs.project-dir }}/target/x86_64-unknown-linux-gnu/release/${{ steps.build.outputs.binary_name }}
- 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
if: success()
uses: actions/upload-artifact@v3
with:
name: ${{ needs.detect-project.outputs.project-dir }}-windows
path: ${{ needs.detect-project.outputs.project-dir }}/target/x86_64-pc-windows-gnu/release/${{ steps.build.outputs.binary_name }}.exe
- 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: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}
- name: Build and push Linux Docker image
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile.linux
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.DOCKER_IMAGE_NAME }}:linux-latest
${{ env.DOCKER_IMAGE_NAME }}:linux-${{ github.sha }}
build-args: |
BINARY_PATH=linux-bin/${{ needs.detect-project.outputs.project-name }}
- name: Build and push Windows Docker image
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile.windows
platforms: windows-amd64
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.DOCKER_IMAGE_NAME }}:windows-latest
${{ env.DOCKER_IMAGE_NAME }}:windows-${{ github.sha }}
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