|
| 1 | +name: "Build Kernel" |
| 2 | +description: "Reusable workflow to build Linux kernels" |
| 3 | +inputs: |
| 4 | + kernel_version: |
| 5 | + description: 'Kernel version to build' |
| 6 | + required: true |
| 7 | + default: '6.12.46' |
| 8 | + kernel_arch: |
| 9 | + description: 'Kernel architecture to build' |
| 10 | + required: true |
| 11 | + default: 'x86_64' |
| 12 | + kernel_nproc: |
| 13 | + description: 'Number of parallel build processes' |
| 14 | + required: false |
| 15 | + default: '4' |
| 16 | + |
| 17 | +runs: |
| 18 | + using: composite |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 22 | + |
| 23 | + - name: Set up Docker Buildx |
| 24 | + uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 |
| 25 | + |
| 26 | + - name: Calculate kernel cache key |
| 27 | + id: cache-key |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + # Hash the kernel config and patches to create a unique cache key |
| 31 | + CONFIG_FILE="kernel/config-${{ inputs.kernel_version }}-${{ matrix.kernel_arch }}" |
| 32 | +
|
| 33 | + if [ ! -f "$CONFIG_FILE" ]; then |
| 34 | + echo "Error: Kernel config file $CONFIG_FILE not found" |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | +
|
| 38 | + # Calculate hash of config file and all patches |
| 39 | + CONFIG_HASH=$(sha256sum "$CONFIG_FILE" | cut -d' ' -f1) |
| 40 | + PATCHES_HASH=$(find kernel/patches -type f -name "*.patch" -exec sha256sum {} \; | sort | sha256sum | cut -d' ' -f1) |
| 41 | +
|
| 42 | + # Combine version, arch, config hash, and patches hash |
| 43 | + CACHE_KEY="kernel-${{ inputs.kernel_version }}-${{ matrix.kernel_arch }}-${CONFIG_HASH:0:8}-${PATCHES_HASH:0:8}" |
| 44 | +
|
| 45 | + echo "cache-key=${CACHE_KEY}" >> $GITHUB_OUTPUT |
| 46 | + echo "config-hash=${CONFIG_HASH:0:8}" >> $GITHUB_OUTPUT |
| 47 | + echo "patches-hash=${PATCHES_HASH:0:8}" >> $GITHUB_OUTPUT |
| 48 | +
|
| 49 | + echo "Kernel cache key: ${CACHE_KEY}" |
| 50 | + echo "Config hash: ${CONFIG_HASH:0:8}" |
| 51 | + echo "Patches hash: ${PATCHES_HASH:0:8}" |
| 52 | +
|
| 53 | + - name: Check cache for existing kernel |
| 54 | + id: cache-kernel |
| 55 | + uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0 |
| 56 | + with: |
| 57 | + path: _output/nerdbox-kernel-${{ matrix.kernel_arch }} |
| 58 | + key: ${{ steps.cache-key.outputs.cache-key }} |
| 59 | + lookup-only: true |
| 60 | + |
| 61 | + - name: Build kernel |
| 62 | + if: steps.cache-kernel.outputs.cache-hit != 'true' |
| 63 | + shell: bash |
| 64 | + run: | |
| 65 | + docker buildx bake kernel \ |
| 66 | + --set kernel.args.KERNEL_VERSION=${{ inputs.kernel_version }} \ |
| 67 | + --set kernel.args.KERNEL_ARCH=${{ inputs.kernel_arch }} \ |
| 68 | + --set kernel.args.KERNEL_NPROC=${{ inputs.kernel_nproc }} |
| 69 | +
|
| 70 | + - name: Verify kernel artifact |
| 71 | + if: steps.cache-kernel.outputs.cache-hit != 'true' |
| 72 | + shell: bash |
| 73 | + run: | |
| 74 | + KERNEL_FILE="_output/nerdbox-kernel-${{ inputs.kernel_arch }}" |
| 75 | + if [ ! -f "$KERNEL_FILE" ]; then |
| 76 | + echo "Error: Kernel file $KERNEL_FILE not found after build" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | +
|
| 80 | + echo "Kernel built successfully:" |
| 81 | + ls -lh "$KERNEL_FILE" |
| 82 | + file "$KERNEL_FILE" |
| 83 | +
|
| 84 | + - name: Save kernel to cache |
| 85 | + if: steps.cache-kernel.outputs.cache-hit != 'true' |
| 86 | + uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0 |
| 87 | + with: |
| 88 | + path: _output/nerdbox-kernel-${{ inputs.kernel_arch }} |
| 89 | + key: ${{ steps.cache-key.outputs.cache-key }} |
| 90 | + |
| 91 | + - name: Upload kernel artifact |
| 92 | + if: steps.cache-kernel.outputs.cache-hit != 'true' |
| 93 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 94 | + with: |
| 95 | + name: nerdbox-kernel-${{ inputs.kernel_version }}-${{ inputs.kernel_arch }} |
| 96 | + path: _output/nerdbox-kernel-${{ inputs.kernel_arch }} |
| 97 | + retention-days: 90 |
| 98 | + if-no-files-found: error |
| 99 | + |
| 100 | + - name: Cache summary |
| 101 | + shell: bash |
| 102 | + run: | |
| 103 | + echo "## Kernel Build Summary" >> $GITHUB_STEP_SUMMARY |
| 104 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 105 | + echo "- **Version**: ${{ inputs.kernel_version }}" >> $GITHUB_STEP_SUMMARY |
| 106 | + echo "- **Architecture**: ${{ inputs.kernel_arch }}" >> $GITHUB_STEP_SUMMARY |
| 107 | + echo "- **Cache Key**: \`${{ steps.cache-key.outputs.cache-key }}\`" >> $GITHUB_STEP_SUMMARY |
| 108 | + echo "- **Config Hash**: ${{ steps.cache-key.outputs.config-hash }}" >> $GITHUB_STEP_SUMMARY |
| 109 | + echo "- **Patches Hash**: ${{ steps.cache-key.outputs.patches-hash }}" >> $GITHUB_STEP_SUMMARY |
| 110 | + echo "- **Cache Hit**: ${{ steps.cache-kernel.outputs.cache-hit == 'true' && '✅ Yes (reused existing)' || '❌ No (built from scratch)' }}" >> $GITHUB_STEP_SUMMARY |
| 111 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 112 | + if [ -f "_output/nerdbox-kernel-${{ inputs.kernel_arch }}" ]; then |
| 113 | + echo "### Kernel Details" >> $GITHUB_STEP_SUMMARY |
| 114 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 115 | + ls -lh "_output/nerdbox-kernel-${{ inputs.kernel_arch }}" >> $GITHUB_STEP_SUMMARY |
| 116 | + file "_output/nerdbox-kernel-${{ inputs.kernel_arch }}" >> $GITHUB_STEP_SUMMARY |
| 117 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 118 | + fi |
0 commit comments