-
Notifications
You must be signed in to change notification settings - Fork 1
169 lines (150 loc) · 5.67 KB
/
pr_build.yaml
File metadata and controls
169 lines (150 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Build Changed Packages (PR)
on:
pull_request:
branches:
- main
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# First job: detect which packages have changed
detect-changes:
runs-on: ubuntu-latest
outputs:
packages: ${{ steps.changes.outputs.packages }}
has_changes: ${{ steps.changes.outputs.has_changes }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect changed packages
id: changes
run: |
# Get list of changed files compared to base branch
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
echo "Changed files:"
echo "$CHANGED_FILES"
# Find unique package directories that have changes
# A package is any directory containing a Dockerfile
PACKAGES=""
for dir in */; do
dir_name="${dir%/}"
# Skip hidden directories and non-package directories
[[ "$dir_name" == .* ]] && continue
[[ "$dir_name" == "scripts" ]] && continue
# Check if this directory has a Dockerfile
if [ -f "${dir_name}/Dockerfile" ]; then
# Check if any files in this package changed
if echo "$CHANGED_FILES" | grep -q "^${dir_name}/"; then
if [ -n "$PACKAGES" ]; then
PACKAGES="${PACKAGES},\"${dir_name}\""
else
PACKAGES="\"${dir_name}\""
fi
echo "Package changed: ${dir_name}"
fi
fi
done
if [ -n "$PACKAGES" ]; then
echo "packages=[${PACKAGES}]" >> $GITHUB_OUTPUT
echo "has_changes=true" >> $GITHUB_OUTPUT
else
echo "packages=[]" >> $GITHUB_OUTPUT
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "No package changes detected"
fi
# Second job: build changed packages
build:
needs: detect-changes
if: needs.detect-changes.outputs.has_changes == 'true'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
package: ${{ fromJson(needs.detect-changes.outputs.packages) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get latest tag and short SHA
id: version
run: |
# Get short SHA
SHORT_SHA=$(git rev-parse --short HEAD)
echo "short_sha=${SHORT_SHA}" >> $GITHUB_OUTPUT
# Find the latest tag for this package (format: package/X.Y.Z)
LATEST_TAG=$(git tag -l "${{ matrix.package }}/*" --sort=-v:refname | head -1)
if [ -n "$LATEST_TAG" ]; then
# Extract version from tag (e.g., "nvidia_tuned/1.2.0" -> "1.2.0")
VERSION=$(echo "$LATEST_TAG" | cut -d'/' -f2)
echo "Found latest tag: $LATEST_TAG (version: $VERSION)"
else
# No existing tag, use 0.0.0 as base
VERSION="0.0.0"
echo "No existing tags found for ${{ matrix.package }}, using $VERSION"
fi
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "full_tag=${VERSION}-dev.${SHORT_SHA}" >> $GITHUB_OUTPUT
- name: Build and push Docker image
uses: ./.github/actions/build-package
with:
package_name: ${{ matrix.package }}
tags: |
type=raw,value=${{ steps.version.outputs.full_tag }}
registry: ${{ env.REGISTRY }}
image_name: ${{ env.IMAGE_NAME }}
generate_attestation: 'false'
save_image_info: 'true'
# Summary job to report status
build-summary:
needs: [detect-changes, build]
if: always()
runs-on: ubuntu-latest
steps:
- name: Download all image info artifacts
if: needs.build.result == 'success'
uses: actions/download-artifact@v4
with:
pattern: image-info-*
path: /tmp/image-info
merge-multiple: true
- name: Check build results and list images
run: |
if [ "${{ needs.detect-changes.outputs.has_changes }}" == "false" ]; then
echo "✅ No packages changed - nothing to build"
exit 0
fi
if [ "${{ needs.build.result }}" == "success" ]; then
echo "✅ All changed packages built and pushed successfully"
echo ""
echo "## Images Pushed"
echo ""
# List all pushed images from artifacts
for file in /tmp/image-info/*.txt; do
[ -f "$file" ] || continue
IMAGE=$(cat "$file")
echo "- \`${IMAGE}\`"
done
elif [ "${{ needs.build.result }}" == "skipped" ]; then
echo "⏭️ Build was skipped"
else
echo "❌ Some packages failed to build"
exit 1
fi