|
26 | 26 |
|
27 | 27 |
|
28 | 28 | jobs: |
| 29 | + detect: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + outputs: |
| 32 | + single_target: ${{ steps.check.outputs.single_target }} |
| 33 | + target_names: ${{ steps.check.outputs.target_names }} |
| 34 | + steps: |
| 35 | + - uses: actions/checkout@v4 |
| 36 | + with: |
| 37 | + fetch-depth: 0 |
| 38 | + - name: Detect single-target PR |
| 39 | + id: check |
| 40 | + run: | |
| 41 | + if [ "${{ github.event_name }}" != "pull_request" ]; then |
| 42 | + echo "single_target=false" >> $GITHUB_OUTPUT |
| 43 | + exit 0 |
| 44 | + fi |
| 45 | +
|
| 46 | + CHANGED=$(git diff --name-only origin/${{ github.base_ref }}...HEAD) |
| 47 | +
|
| 48 | + if [ -z "$CHANGED" ]; then |
| 49 | + echo "single_target=false" >> $GITHUB_OUTPUT |
| 50 | + exit 0 |
| 51 | + fi |
| 52 | +
|
| 53 | + # Fail fast if any changed file is outside src/main/target/<dir>/ |
| 54 | + NON_TARGET=$(echo "$CHANGED" | grep -Ev '^src/main/target/[^/]+/.+' | head -1) |
| 55 | + if [ -n "$NON_TARGET" ]; then |
| 56 | + echo "Non-target file changed: $NON_TARGET — full build required" |
| 57 | + echo "single_target=false" >> $GITHUB_OUTPUT |
| 58 | + exit 0 |
| 59 | + fi |
| 60 | +
|
| 61 | + # Count how many distinct target directories were touched |
| 62 | + TARGET_DIRS=$(echo "$CHANGED" | sed -n 's|^src/main/target/\([^/]*\)/.*|\1|p' | sort -u) |
| 63 | + DIR_COUNT=$(echo "$TARGET_DIRS" | wc -l) |
| 64 | + if [ "$DIR_COUNT" != "1" ]; then |
| 65 | + echo "$DIR_COUNT target directories changed — full build required" |
| 66 | + echo "single_target=false" >> $GITHUB_OUTPUT |
| 67 | + exit 0 |
| 68 | + fi |
| 69 | +
|
| 70 | + TARGET_DIR="$TARGET_DIRS" |
| 71 | + CMAKE="src/main/target/${TARGET_DIR}/CMakeLists.txt" |
| 72 | + if [ ! -f "$CMAKE" ]; then |
| 73 | + echo "No CMakeLists.txt for $TARGET_DIR — full build required" |
| 74 | + echo "single_target=false" >> $GITHUB_OUTPUT |
| 75 | + exit 0 |
| 76 | + fi |
| 77 | +
|
| 78 | + TARGET_NAMES=$(grep -oP '(?<=\()[^)]+' "$CMAKE" | tr '\n' ' ' | xargs) |
| 79 | + if [ -z "$TARGET_NAMES" ]; then |
| 80 | + echo "No target names in CMakeLists.txt — full build required" |
| 81 | + echo "single_target=false" >> $GITHUB_OUTPUT |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | +
|
| 85 | + echo "Single-target PR: building $TARGET_NAMES" |
| 86 | + echo "single_target=true" >> $GITHUB_OUTPUT |
| 87 | + echo "target_names=$TARGET_NAMES" >> $GITHUB_OUTPUT |
| 88 | +
|
29 | 89 | build: |
| 90 | + needs: [detect] |
| 91 | + if: needs.detect.outputs.single_target != 'true' |
30 | 92 | runs-on: ubuntu-latest |
31 | 93 | strategy: |
32 | 94 | matrix: |
@@ -66,9 +128,42 @@ jobs: |
66 | 128 | path: ./build/*.hex |
67 | 129 | retention-days: 1 |
68 | 130 |
|
| 131 | + build-single-target: |
| 132 | + needs: [detect] |
| 133 | + if: needs.detect.outputs.single_target == 'true' |
| 134 | + runs-on: ubuntu-latest |
| 135 | + steps: |
| 136 | + - uses: actions/checkout@v4 |
| 137 | + - name: Install dependencies |
| 138 | + run: sudo apt-get update && sudo apt-get -y install ninja-build |
| 139 | + - name: Setup environment |
| 140 | + env: |
| 141 | + ACTIONS_ALLOW_UNSECURE_COMMANDS: true |
| 142 | + run: | |
| 143 | + COMMIT_ID=${{ github.event.pull_request.head.sha }} |
| 144 | + COMMIT_ID=${COMMIT_ID:-${{ github.sha }}} |
| 145 | + BUILD_SUFFIX=ci-$(date '+%Y%m%d')-$(git rev-parse --short ${COMMIT_ID}) |
| 146 | + VERSION=$(grep project CMakeLists.txt|awk -F VERSION '{ gsub(/[ \t)]/, "", $2); print $2 }') |
| 147 | + echo "BUILD_SUFFIX=${BUILD_SUFFIX}" >> $GITHUB_ENV |
| 148 | + echo "BUILD_NAME=inav-${VERSION}-${BUILD_SUFFIX}" >> $GITHUB_ENV |
| 149 | + echo "NUM_CORES=$(grep processor /proc/cpuinfo | wc -l)" >> $GITHUB_ENV |
| 150 | + - uses: actions/cache@v4 |
| 151 | + with: |
| 152 | + path: downloads |
| 153 | + key: ${{ runner.os }}-downloads-${{ hashFiles('CMakeLists.txt') }}-${{ hashFiles('**/cmake/*')}} |
| 154 | + - name: Build targets (${{ needs.detect.outputs.target_names }}) |
| 155 | + run: mkdir -p build && cd build && cmake -DWARNINGS_AS_ERRORS=ON -DBUILD_SUFFIX=${{ env.BUILD_SUFFIX }} -DMAIN_COMPILE_OPTIONS=-pipe -G Ninja .. && ninja -j${{ env.NUM_CORES }} ${{ needs.detect.outputs.target_names }} |
| 156 | + - name: Upload artifacts |
| 157 | + uses: actions/upload-artifact@v4 |
| 158 | + with: |
| 159 | + name: matrix-${{ env.BUILD_NAME }}.single |
| 160 | + path: ./build/*.hex |
| 161 | + retention-days: 1 |
| 162 | + |
69 | 163 | upload-artifacts: |
70 | 164 | runs-on: ubuntu-latest |
71 | | - needs: [build] |
| 165 | + needs: [build, build-single-target] |
| 166 | + if: always() && !cancelled() && (needs.build.result == 'success' || needs.build-single-target.result == 'success') |
72 | 167 | steps: |
73 | 168 | - uses: actions/checkout@v4 |
74 | 169 | - name: Setup environment |
|
0 commit comments