178 lines
5.6 KiB
YAML
178 lines
5.6 KiB
YAML
name: Rust Cross-Platform Build
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
push:
|
|
branches: [ "development", "main", "feature/*", bug/*, enhancement/* ]
|
|
pull_request:
|
|
branches: [ "development", "main" ]
|
|
|
|
env:
|
|
CARGO_TERM_COLOR: always
|
|
REGISTRY: git.triggermeelmo.com
|
|
IMAGE_NAME: donpat1to/watcher-agent
|
|
TAG: development
|
|
|
|
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: ${{ needs.detect-project.outputs.project-dir }}/target/x86_64-unknown-linux-gnu/release/${{ needs.detect-project.outputs.project-name }}
|
|
|
|
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: ${{ needs.detect-project.outputs.project-dir }}/target/x86_64-pc-windows-gnu/release/${{ needs.detect-project.outputs.project-name }}.exe
|
|
|
|
docker-build:
|
|
name: Build Linux Docker Image
|
|
needs: [native-build, windows-cross, detect-project]
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Download Linux artifact
|
|
id: download-linux
|
|
uses: actions/download-artifact@v3
|
|
with:
|
|
name: linux-binary
|
|
path: linux-bin
|
|
|
|
- name: Verify artifacts
|
|
run: |
|
|
echo "Linux binary:"
|
|
ls -la linux-bin/
|
|
|
|
- name: Login to Docker Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ secrets.DOCKER_USERNAME }}
|
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
|
|
|
- name: Linux build using Docker Buildx
|
|
run: |
|
|
docker buildx create --use
|
|
docker buildx build \
|
|
--platform linux/amd64 \
|
|
-f Dockerfile.linux \
|
|
--build-arg BINARY_NAME=${{ needs.detect-project.outputs.project-name }} \
|
|
--load \
|
|
-t ${{ env.IMAGE_NAME }}:linux-${{ env.TAG }} .
|
|
|
|
- name: Save Docker image as artifact
|
|
run: |
|
|
docker save ${{ env.IMAGE_NAME }}:linux-${{ env.TAG }} -o linux-image.tar
|
|
|
|
- name: Upload Docker image artifact
|
|
uses: actions/upload-artifact@v3
|
|
with:
|
|
name: linux-docker-image
|
|
path: linux-image.tar
|
|
|
|
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 |