Skip to content

Commit 99077b6

Browse files
committed
upload artifacts to GHA Cache when merged to main
1 parent aba2cd7 commit 99077b6

File tree

1 file changed

+122
-19
lines changed

1 file changed

+122
-19
lines changed

.github/workflows/build-and-test.yml

Lines changed: 122 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ jobs:
4242
# (matrix.host-platform == 'win-64' && 'windows-amd64-cpu8') }}
4343
outputs:
4444
BUILD_CTK_VER: ${{ steps.pass_env.outputs.CUDA_VERSION }}
45+
defaults:
46+
run:
47+
shell: bash --noprofile --norc -xeuo pipefail {0}
4548
steps:
4649
- name: Checkout ${{ github.event.repository.name }}
4750
uses: actions/checkout@v4
@@ -62,7 +65,6 @@ jobs:
6265
uses: ilammy/msvc-dev-cmd@v1
6366

6467
- name: Set environment variables
65-
shell: bash --noprofile --norc -xeuo pipefail {0}
6668
run: |
6769
PYTHON_VERSION_FORMATTED=$(echo '${{ matrix.python-version }}' | tr -d '.')
6870
if [[ "${{ matrix.host-platform }}" == linux* ]]; then
@@ -75,14 +77,25 @@ jobs:
7577
fi
7678
7779
echo "PARALLEL_LEVEL=$(nproc)" >> $GITHUB_ENV
78-
echo "CUDA_CORE_ARTIFACT_NAME=cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}-${{ github.sha }}" >> $GITHUB_ENV
80+
CUDA_CORE_ARTIFACT_BASENAME="cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}"
81+
echo "CUDA_CORE_ARTIFACT_BASENAME=${CUDA_CORE_ARTIFACT_BASENAME}" >> $GITHUB_ENV
82+
echo "CUDA_CORE_ARTIFACT_NAME=${CUDA_CORE_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
7983
echo "CUDA_CORE_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_core/dist")" >> $GITHUB_ENV
80-
echo "CUDA_BINDINGS_ARTIFACT_NAME=cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ matrix.cuda-version }}-${{ matrix.host-platform }}-${{ github.sha }}" >> $GITHUB_ENV
84+
CUDA_BINDINGS_ARTIFACT_BASENAME="cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ matrix.cuda-version }}-${{ matrix.host-platform }}"
85+
echo "CUDA_BINDINGS_ARTIFACT_BASENAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}" >> $GITHUB_ENV
86+
echo "CUDA_BINDINGS_ARTIFACT_NAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
8187
echo "CUDA_BINDINGS_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_bindings/dist")" >> $GITHUB_ENV
8288
echo "CIBW_BUILD=${CIBW_BUILD}" >> $GITHUB_ENV
83-
89+
90+
# When the CI is run due to merging to main, we want it to populate GHA Cache not Artifacts,
91+
# so that CI workflows running on every branch have a fallback to use.
92+
if [[ "${{ github.ref_name}}" == main ]]; then
93+
echo "USE_CACHE=1" >> $GITHUB_ENV
94+
else
95+
echo "USE_CACHE=0" >> $GITHUB_ENV
96+
fi
97+
8498
- name: Dump environment
85-
shell: bash --noprofile --norc -xeuo pipefail {0}
8699
run: |
87100
env
88101
@@ -97,7 +110,6 @@ jobs:
97110
output-dir: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
98111

99112
- name: List the cuda.core artifacts directory
100-
shell: bash --noprofile --norc -xeuo pipefail {0}
101113
run: |
102114
if [[ "${{ matrix.host-platform }}" == win* ]]; then
103115
export CHOWN=chown
@@ -108,19 +120,40 @@ jobs:
108120
ls -lahR ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
109121
110122
- name: Check cuda.core wheel
111-
shell: bash --noprofile --norc -xeuo pipefail {0}
112123
run: |
113124
pip install twine
114125
twine check ${{ env.CUDA_CORE_ARTIFACTS_DIR }}/*.whl
115126
116127
- name: Upload cuda.core build artifacts
128+
if: ${{ env.USE_CACHE == '0' }}
117129
uses: actions/upload-artifact@v4
118130
with:
119131
name: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
120132
path: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}/*.whl
121133
if-no-files-found: error
122134
overwrite: 'true'
123135

136+
- name: Prepare cuda.core cache
137+
if: ${{ env.USE_CACHE == '1' }}
138+
run: |
139+
if [[ "${{ env.USE_CACHE }}" == 1 ]]; then
140+
# this file is uploaded to GHA Cache
141+
tar -c -f "${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz" -C "${{ env.CUDA_CORE_ARTIFACTS_DIR }}" .
142+
du -h "${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz"
143+
# check if the previous runs from the same PR have populated the cache, if so need to clean it up
144+
CACHE_KEY=${{ env.CUDA_CORE_ARTIFACT_NAME }}
145+
if [ $(gh cache list | grep $CACHE_KEY | wc -l) == "1" ]; then
146+
gh cache delete $CACHE_KEY
147+
fi
148+
fi
149+
150+
- name: Cache cuda.core build artifacts
151+
if: ${{ env.USE_CACHE == '1' }}
152+
uses: actions/cache/save@v4
153+
with:
154+
key: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
155+
path: ${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz
156+
124157
- name: Set up mini CTK
125158
uses: ./.github/actions/fetch_ctk
126159
continue-on-error: false
@@ -146,7 +179,6 @@ jobs:
146179
output-dir: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
147180

148181
- name: List the cuda.bindings artifacts directory
149-
shell: bash --noprofile --norc -xeuo pipefail {0}
150182
run: |
151183
if [[ "${{ matrix.host-platform }}" == win* ]]; then
152184
export CHOWN=chown
@@ -158,18 +190,39 @@ jobs:
158190
159191
# TODO: enable this after NVIDIA/cuda-python#297 is resolved
160192
# - name: Check cuda.bindings wheel
161-
# shell: bash --noprofile --norc -xeuo pipefail {0}
162193
# run: |
163194
# twine check ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}/*.whl
164195

196+
- name: Prepare cuda.bindings cache
197+
if: ${{ env.USE_CACHE == '1' }}
198+
run: |
199+
if [[ "${{ env.USE_CACHE }}" == 1 ]]; then
200+
# this file is uploaded to GHA Cache
201+
tar -c -f "${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz" -C "${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}" .
202+
du -h "${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz"
203+
# check if the previous runs from the same PR have populated the cache, if so need to clean it up
204+
CACHE_KEY=${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
205+
if [ $(gh cache list | grep $CACHE_KEY | wc -l) == "1" ]; then
206+
gh cache delete $CACHE_KEY
207+
fi
208+
fi
209+
165210
- name: Upload cuda.bindings build artifacts
211+
if: ${{ env.USE_CACHE == '0' }}
166212
uses: actions/upload-artifact@v4
167213
with:
168214
name: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
169215
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}/*.whl
170216
if-no-files-found: error
171217
overwrite: 'true'
172218

219+
- name: Cache cuda.bindings build artifacts
220+
if: ${{ env.USE_CACHE == '1' }}
221+
uses: actions/cache/save@v4
222+
with:
223+
key: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
224+
path: ${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz
225+
173226
- name: Pass environment variables to the next runner
174227
id: pass_env
175228
run: |
@@ -205,7 +258,7 @@ jobs:
205258
runner: H100
206259
name: Test (${{ matrix.host-platform }}, Python ${{ matrix.python-version }}, CUDA ${{ matrix.cuda-version }}, Runner ${{ matrix.runner }})
207260
# The build stage could fail but we want the CI to keep moving.
208-
if: ${{ github.repository_owner == 'nvidia' && always() }}
261+
if: ${{ github.repository_owner == 'nvidia' && !cancelled() }}
209262
permissions:
210263
id-token: write # This is required for configure-aws-credentials
211264
contents: read # This is required for actions/checkout
@@ -221,9 +274,11 @@ jobs:
221274
NVIDIA_VISIBLE_DEVICES: ${{ env.NVIDIA_VISIBLE_DEVICES }}
222275
needs:
223276
- build
277+
defaults:
278+
run:
279+
shell: bash --noprofile --norc -xeuo pipefail {0}
224280
steps:
225281
- name: Ensure GPU is working
226-
shell: bash --noprofile --norc -xeuo pipefail {0}
227282
run: nvidia-smi
228283

229284
- name: Checkout ${{ github.event.repository.name }}
@@ -232,7 +287,6 @@ jobs:
232287
fetch-depth: 0
233288

234289
- name: Set environment variables
235-
shell: bash --noprofile --norc -xeuo pipefail {0}
236290
run: |
237291
PYTHON_VERSION_FORMATTED=$(echo '${{ matrix.python-version }}' | tr -d '.')
238292
if [[ "${{ matrix.host-platform }}" == linux* ]]; then
@@ -251,32 +305,83 @@ jobs:
251305
fi
252306
253307
# make outputs from the previous job as env vars
254-
echo "CUDA_CORE_ARTIFACT_NAME=cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}-${{ github.sha }}" >> $GITHUB_ENV
308+
CUDA_CORE_ARTIFACT_BASENAME="cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}"
309+
echo "CUDA_CORE_ARTIFACT_BASENAME=${CUDA_CORE_ARTIFACT_BASENAME}" >> $GITHUB_ENV
310+
echo "CUDA_CORE_ARTIFACT_NAME=${CUDA_CORE_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
255311
echo "CUDA_CORE_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_core/dist")" >> $GITHUB_ENV
256-
echo "CUDA_BINDINGS_ARTIFACT_NAME=cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ needs.build.outputs.BUILD_CTK_VER }}-${{ matrix.host-platform }}-${{ github.sha }}" >> $GITHUB_ENV
312+
CUDA_BINDINGS_ARTIFACT_BASENAME="cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ matrix.cuda-version }}-${{ matrix.host-platform }}"
313+
echo "CUDA_BINDINGS_ARTIFACT_BASENAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}" >> $GITHUB_ENV
314+
echo "CUDA_BINDINGS_ARTIFACT_NAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
257315
echo "CUDA_BINDINGS_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_bindings/dist")" >> $GITHUB_ENV
258316
echo "SKIP_CUDA_BINDINGS_TEST=${SKIP_CUDA_BINDINGS_TEST}" >> $GITHUB_ENV
259317
318+
# We'll try GHA Artifacts first, and then fall back to GHA Cache
260319
- name: Download cuda.bindings build artifacts
320+
id: cuda-bindings-download
261321
uses: actions/download-artifact@v4
262322
with:
263323
name: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
264324
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
265325

326+
- name: Restore cuda.bindings cache
327+
if: ${{ failure() && steps.cuda-bindings-download.conclusion == 'failure' }}
328+
id: cuda-bindings-cache
329+
uses: actions/cache/restore@v4
330+
with:
331+
key: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
332+
path: ${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz
333+
restore-keys: ${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}
334+
fail-on-cache-miss: true
335+
336+
- name: Report cache restore status (hit)
337+
if: ${{ steps.cuda-bindings-cache.conclusion != 'skipped' &&
338+
steps.cuda-bindings-cache.outputs.cache-hit == 'true' }}
339+
run: |
340+
echo "cache is found"
341+
CACHE_DIR="${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}"
342+
CACHE_ARCHIVE="${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz"
343+
ls -l $CACHE_ARCHIVE
344+
mkdir -p $CACHE_DIR
345+
du -h $CACHE_ARCHIVE &&
346+
tar -x -f $CACHE_ARCHIVE -C $CACHE_DIR &&
347+
rm -f $CACHE_ARCHIVE || echo "WARNING: cache could not be retrieved."
348+
266349
- name: Display structure of downloaded cuda.bindings artifacts
267-
shell: bash --noprofile --norc -xeuo pipefail {0}
268350
run: |
269351
pwd
270352
ls -lahR $CUDA_BINDINGS_ARTIFACTS_DIR
271353
272354
- name: Download cuda.core build artifacts
355+
id: cuda-core-download
273356
uses: actions/download-artifact@v4
274357
with:
275358
name: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
276359
path: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
277360

361+
- name: Restore cuda.core cache
362+
if: ${{ failure() && steps.cuda-core-download.conclusion == 'failure' }}
363+
id: cuda-core-cache
364+
uses: actions/cache/restore@v4
365+
with:
366+
key: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
367+
path: ${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz
368+
restore-keys: ${{ env.CUDA_CORE_ARTIFACT_BASENAME }}
369+
fail-on-cache-miss: true
370+
371+
- name: Report cache restore status (hit)
372+
if: ${{ steps.cuda-core-cache.conclusion != 'skipped' &&
373+
steps.cuda-core-cache.outputs.cache-hit == 'true' }}
374+
run: |
375+
echo "cache is found"
376+
CACHE_DIR="${{ env.CUDA_CORE_ARTIFACTS_DIR }}"
377+
CACHE_ARCHIVE="${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz"
378+
ls -l $CACHE_ARCHIVE
379+
mkdir -p $CACHE_DIR
380+
du -h $CACHE_ARCHIVE &&
381+
tar -x -f $CACHE_ARCHIVE -C $CACHE_DIR &&
382+
rm -f $CACHE_ARCHIVE || echo "WARNING: cache could not be retrieved."
383+
278384
- name: Display structure of downloaded cuda.core build artifacts
279-
shell: bash --noprofile --norc -xeuo pipefail {0}
280385
run: |
281386
pwd
282387
ls -lahR $CUDA_CORE_ARTIFACTS_DIR
@@ -295,7 +400,6 @@ jobs:
295400

296401
- name: Run cuda.bindings tests
297402
if: ${{ env.SKIP_CUDA_BINDINGS_TEST == '0' }}
298-
shell: bash --noprofile --norc -xeuo pipefail {0}
299403
run: |
300404
ls $CUDA_PATH
301405
@@ -311,7 +415,6 @@ jobs:
311415
popd
312416
313417
- name: Run cuda.core tests
314-
shell: bash --noprofile --norc -xeuo pipefail {0}
315418
run: |
316419
if [[ ${{ matrix.python-version }} == "3.13" ]]; then
317420
# TODO: remove this hack once cuda-python has a cp313 build
@@ -336,7 +439,7 @@ jobs:
336439
doc:
337440
name: Docs
338441
# The build stage could fail but we want the CI to keep moving.
339-
if: ${{ github.repository_owner == 'nvidia' && always() }}
442+
if: ${{ github.repository_owner == 'nvidia' && !cancelled() }}
340443
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
341444
permissions:
342445
id-token: write

0 commit comments

Comments
 (0)