|
| 1 | +name: Build Changed Samples |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'l*/**' |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + paths: |
| 11 | + - 'l*/**' |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + build_all: |
| 15 | + description: 'Build all samples instead of only changed ones' |
| 16 | + required: false |
| 17 | + default: 'false' |
| 18 | + type: boolean |
| 19 | + |
| 20 | +env: |
| 21 | + NCS_TOOLCHAIN_VERSION: latest |
| 22 | + |
| 23 | +jobs: |
| 24 | + detect-changes: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + outputs: |
| 27 | + changed_dirs: ${{ steps.changes.outputs.dirs }} |
| 28 | + has_changes: ${{ steps.changes.outputs.has_changes }} |
| 29 | + steps: |
| 30 | + - name: Checkout |
| 31 | + uses: actions/checkout@v4 |
| 32 | + with: |
| 33 | + fetch-depth: 0 |
| 34 | + |
| 35 | + - name: Detect changed sample directories |
| 36 | + id: changes |
| 37 | + run: | |
| 38 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ "${{ inputs.build_all }}" == "true" ]; then |
| 39 | + # Build all samples if manually triggered with build_all |
| 40 | + DIRS=$(find . -maxdepth 2 -type d -name 'l*_e*' -o -name 'l*_e*_sol' | sed 's|^\./||' | sort -u | tr '\n' ' ') |
| 41 | + echo "Building all samples" |
| 42 | + elif [ "${{ github.event_name }}" == "pull_request" ]; then |
| 43 | + # Get changed files in PR |
| 44 | + CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }}) |
| 45 | + DIRS=$(echo "$CHANGED_FILES" | grep -E '^l[0-9]+/' | cut -d'/' -f1,2 | sort -u | tr '\n' ' ') |
| 46 | + else |
| 47 | + # Get changed files in push (compare with previous commit) |
| 48 | + CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || git diff --name-only HEAD) |
| 49 | + DIRS=$(echo "$CHANGED_FILES" | grep -E '^l[0-9]+/' | cut -d'/' -f1,2 | sort -u | tr '\n' ' ') |
| 50 | + fi |
| 51 | + |
| 52 | + if [ -z "$DIRS" ]; then |
| 53 | + echo "No sample directories changed" |
| 54 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 55 | + echo "dirs=" >> $GITHUB_OUTPUT |
| 56 | + else |
| 57 | + echo "Changed directories: $DIRS" |
| 58 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 59 | + echo "dirs=$DIRS" >> $GITHUB_OUTPUT |
| 60 | + fi |
| 61 | +
|
| 62 | + build: |
| 63 | + needs: detect-changes |
| 64 | + if: needs.detect-changes.outputs.has_changes == 'true' |
| 65 | + runs-on: ubuntu-latest |
| 66 | + container: |
| 67 | + image: ghcr.io/zephyrproject-rtos/ci:v0.26.13 |
| 68 | + steps: |
| 69 | + - name: Checkout |
| 70 | + uses: actions/checkout@v4 |
| 71 | + with: |
| 72 | + path: ncs-inter |
| 73 | + |
| 74 | + - name: Initialize west workspace |
| 75 | + run: | |
| 76 | + west init -l ncs-inter |
| 77 | + west update --narrow -o=--depth=1 |
| 78 | +
|
| 79 | + - name: Install nRF Connect SDK toolchain |
| 80 | + run: | |
| 81 | + pip install --upgrade pip |
| 82 | + pip install west |
| 83 | +
|
| 84 | + - name: Run Twister on changed samples |
| 85 | + run: | |
| 86 | + DIRS="${{ needs.detect-changes.outputs.changed_dirs }}" |
| 87 | + TWISTER_ARGS="" |
| 88 | + |
| 89 | + for dir in $DIRS; do |
| 90 | + if [ -d "ncs-inter/$dir" ]; then |
| 91 | + TWISTER_ARGS="$TWISTER_ARGS -T ncs-inter/$dir" |
| 92 | + fi |
| 93 | + done |
| 94 | + |
| 95 | + if [ -n "$TWISTER_ARGS" ]; then |
| 96 | + echo "Running: west twister $TWISTER_ARGS -v --inline-logs --integration --build-only" |
| 97 | + west twister $TWISTER_ARGS -v --inline-logs --integration --build-only |
| 98 | + else |
| 99 | + echo "No valid directories to build" |
| 100 | + fi |
| 101 | +
|
| 102 | + - name: Upload build artifacts |
| 103 | + if: always() |
| 104 | + uses: actions/upload-artifact@v4 |
| 105 | + with: |
| 106 | + name: twister-results |
| 107 | + path: | |
| 108 | + twister-out/ |
| 109 | + if-no-files-found: ignore |
| 110 | + |
| 111 | + skip-notification: |
| 112 | + needs: detect-changes |
| 113 | + if: needs.detect-changes.outputs.has_changes == 'false' |
| 114 | + runs-on: ubuntu-latest |
| 115 | + steps: |
| 116 | + - name: No changes detected |
| 117 | + run: echo "No sample directories were changed. Skipping build." |
0 commit comments