Skip to content

Commit eebef9f

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

File tree

1 file changed

+105
-21
lines changed

1 file changed

+105
-21
lines changed

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

Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ jobs:
4242
# (matrix.host-platform == 'win-64' && 'windows-amd64-cpu8') }}
4343
outputs:
4444
BUILD_CTK_VER: ${{ steps.pass_env.outputs.CUDA_VERSION }}
45+
USE_GH_CACHE: ${{ steps.pass_env.outputs.USE_CACHE }}
46+
defaults:
47+
run:
48+
shell: bash --noprofile --norc -xeuo pipefail {0}
4549
steps:
4650
- name: Checkout ${{ github.event.repository.name }}
4751
uses: actions/checkout@v4
@@ -62,7 +66,6 @@ jobs:
6266
uses: ilammy/msvc-dev-cmd@v1
6367

6468
- name: Set environment variables
65-
shell: bash --noprofile --norc -xeuo pipefail {0}
6669
run: |
6770
PYTHON_VERSION_FORMATTED=$(echo '${{ matrix.python-version }}' | tr -d '.')
6871
if [[ "${{ matrix.host-platform }}" == linux* ]]; then
@@ -75,14 +78,23 @@ jobs:
7578
fi
7679
7780
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
81+
echo "CUDA_CORE_ARTIFACT_BASENAME=cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}" >> $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+
echo "CUDA_BINDINGS_ARTIFACT_BASENAME=cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ matrix.cuda-version }}-${{ matrix.host-platform }}" >> $GITHUB_ENV
85+
echo "CUDA_BINDINGS_ARTIFACT_NAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
8186
echo "CUDA_BINDINGS_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_bindings/dist")" >> $GITHUB_ENV
8287
echo "CIBW_BUILD=${CIBW_BUILD}" >> $GITHUB_ENV
83-
88+
89+
# When the CI is run due to merging to main, we want it to populate GHA Cache not Artifacts,
90+
# so that CI workflows running on every branch have a fallback to use.
91+
if [[ "${{ github.ref_name}}" == main ]]; then
92+
echo "USE_CACHE=1" >> $GITHUB_ENV
93+
else
94+
echo "USE_CACHE=0" >> $GITHUB_ENV
95+
fi
96+
8497
- name: Dump environment
85-
shell: bash --noprofile --norc -xeuo pipefail {0}
8698
run: |
8799
env
88100
@@ -97,7 +109,6 @@ jobs:
97109
output-dir: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
98110

99111
- name: List the cuda.core artifacts directory
100-
shell: bash --noprofile --norc -xeuo pipefail {0}
101112
run: |
102113
if [[ "${{ matrix.host-platform }}" == win* ]]; then
103114
export CHOWN=chown
@@ -108,19 +119,29 @@ jobs:
108119
ls -lahR ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
109120
110121
- name: Check cuda.core wheel
111-
shell: bash --noprofile --norc -xeuo pipefail {0}
112122
run: |
113123
pip install twine
114124
twine check ${{ env.CUDA_CORE_ARTIFACTS_DIR }}/*.whl
125+
# unconditionally prepare the cache in case needed later
126+
tar -c -f "${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz" -C "${{ env.CUDA_CORE_ARTIFACTS_DIR }}" .
127+
du -h "${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz"
115128
116129
- name: Upload cuda.core build artifacts
130+
if: ${{ env.USE_CACHE == '0' }}
117131
uses: actions/upload-artifact@v4
118132
with:
119133
name: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
120134
path: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}/*.whl
121135
if-no-files-found: error
122136
overwrite: 'true'
123137

138+
- name: Cache cuda.core build artifacts
139+
if: ${{ env.USE_CACHE == '1' }}
140+
uses: actions/cache/save@v4
141+
with:
142+
key: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
143+
path: ${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz
144+
124145
- name: Set up mini CTK
125146
uses: ./.github/actions/fetch_ctk
126147
continue-on-error: false
@@ -146,7 +167,6 @@ jobs:
146167
output-dir: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
147168

148169
- name: List the cuda.bindings artifacts directory
149-
shell: bash --noprofile --norc -xeuo pipefail {0}
150170
run: |
151171
if [[ "${{ matrix.host-platform }}" == win* ]]; then
152172
export CHOWN=chown
@@ -156,24 +176,35 @@ jobs:
156176
$CHOWN -R $(whoami) ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
157177
ls -lahR ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
158178
159-
# TODO: enable this after NVIDIA/cuda-python#297 is resolved
160-
# - name: Check cuda.bindings wheel
161-
# shell: bash --noprofile --norc -xeuo pipefail {0}
162-
# run: |
163-
# twine check ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}/*.whl
179+
- name: Check cuda.bindings wheel
180+
run: |
181+
# TODO: enable this after NVIDIA/cuda-python#297 is resolved
182+
# twine check ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}/*.whl
183+
# unconditionally prepare the cache in case needed later
184+
tar -c -f "${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz" -C "${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}" .
185+
du -h "${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz"
164186
165187
- name: Upload cuda.bindings build artifacts
188+
if: ${{ env.USE_CACHE == '0' }}
166189
uses: actions/upload-artifact@v4
167190
with:
168191
name: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
169192
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}/*.whl
170193
if-no-files-found: error
171194
overwrite: 'true'
172195

196+
- name: Cache cuda.bindings build artifacts
197+
if: ${{ env.USE_CACHE == '1' }}
198+
uses: actions/cache/save@v4
199+
with:
200+
key: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
201+
path: ${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz
202+
173203
- name: Pass environment variables to the next runner
174204
id: pass_env
175205
run: |
176206
echo "CUDA_VERSION=${{ matrix.cuda-version }}" >> $GITHUB_OUTPUT
207+
echo "USE_CACHE=${{ env.USE_CACHE }}" >> $GITHUB_OUTPUT
177208
178209
test:
179210
strategy:
@@ -221,9 +252,11 @@ jobs:
221252
NVIDIA_VISIBLE_DEVICES: ${{ env.NVIDIA_VISIBLE_DEVICES }}
222253
needs:
223254
- build
255+
defaults:
256+
run:
257+
shell: bash --noprofile --norc -xeuo pipefail {0}
224258
steps:
225259
- name: Ensure GPU is working
226-
shell: bash --noprofile --norc -xeuo pipefail {0}
227260
run: nvidia-smi
228261

229262
- name: Checkout ${{ github.event.repository.name }}
@@ -232,7 +265,6 @@ jobs:
232265
fetch-depth: 0
233266

234267
- name: Set environment variables
235-
shell: bash --noprofile --norc -xeuo pipefail {0}
236268
run: |
237269
PYTHON_VERSION_FORMATTED=$(echo '${{ matrix.python-version }}' | tr -d '.')
238270
if [[ "${{ matrix.host-platform }}" == linux* ]]; then
@@ -251,32 +283,85 @@ jobs:
251283
fi
252284
253285
# 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
286+
echo "CUDA_CORE_ARTIFACT_BASENAME=cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}" >> $GITHUB_ENV
287+
echo "CUDA_CORE_ARTIFACT_NAME=${CUDA_CORE_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
255288
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
289+
echo "CUDA_BINDINGS_ARTIFACT_BASENAME=cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ matrix.cuda-version }}-${{ matrix.host-platform }}" >> $GITHUB_ENV
290+
echo "CUDA_BINDINGS_ARTIFACT_NAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
257291
echo "CUDA_BINDINGS_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_bindings/dist")" >> $GITHUB_ENV
258292
echo "SKIP_CUDA_BINDINGS_TEST=${SKIP_CUDA_BINDINGS_TEST}" >> $GITHUB_ENV
259293
294+
# When the CI is run due to merging to main, we want it to populate GHA Cache not Artifacts,
295+
# so that CI workflows running on every branch have a fallback to use.
296+
echo "USE_CACHE=${{ needs.build.outputs.USE_GH_CACHE }}" >> $GITHUB_ENV
297+
298+
# We'll try GHA Artifacts first, and then fall back to GHA Cache
260299
- name: Download cuda.bindings build artifacts
300+
id: cuda-bindings-download
261301
uses: actions/download-artifact@v4
262302
with:
263303
name: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
264304
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
265305

306+
- name: Restore cuda.bindings cache
307+
if: ${{ failure() && steps.cuda-bindings-download.conclusion == 'failure' }}
308+
id: cuda-bindings-cache
309+
uses: actions/cache/restore@v4
310+
with:
311+
key: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
312+
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_BASENAME }}.tar.gz
313+
restore-keys: ${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}
314+
fail-on-cache-miss: true
315+
316+
- name: Report cache restore status (hit)
317+
if: ${{ steps.cuda-bindings-cache.conclusion != 'skipped' &&
318+
steps.cuda-bindings-cache.outputs.cache-hit == 'true' }}
319+
run: |
320+
echo "cache is found"
321+
CACHE_DIR="${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}"
322+
CACHE_ARCHIVE="${{ env.CUDA_BINDINGS_ARTIFACTS_BASENAME }}.tar.gz"
323+
ls -l $CACHE_ARCHIVE
324+
mkdir -p $CACHE_DIR
325+
du -h $CACHE_ARCHIVE &&
326+
tar -x -f $CACHE_ARCHIVE -C $CACHE_DIR &&
327+
rm -f $CACHE_ARCHIVE || echo "WARNING: cache could not be retrieved."
328+
266329
- name: Display structure of downloaded cuda.bindings artifacts
267-
shell: bash --noprofile --norc -xeuo pipefail {0}
268330
run: |
269331
pwd
270332
ls -lahR $CUDA_BINDINGS_ARTIFACTS_DIR
271333
272334
- name: Download cuda.core build artifacts
335+
id: cuda-core-download
273336
uses: actions/download-artifact@v4
274337
with:
275338
name: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
276339
path: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
277340

341+
- name: Restore cuda.core cache
342+
if: ${{ failure() && steps.cuda-core-download.conclusion == 'failure' }}
343+
id: cuda-core-cache
344+
uses: actions/cache/restore@v4
345+
with:
346+
key: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
347+
path: ${{ env.CUDA_CORE_ARTIFACTS_BASENAME }}.tar.gz
348+
restore-keys: ${{ env.CUDA_CORE_ARTIFACT_BASENAME }}
349+
fail-on-cache-miss: true
350+
351+
- name: Report cache restore status (hit)
352+
if: ${{ steps.cuda-core-cache.conclusion != 'skipped' &&
353+
steps.cuda-core-cache.outputs.cache-hit == 'true' }}
354+
run: |
355+
echo "cache is found"
356+
CACHE_DIR="${{ env.CUDA_CORE_ARTIFACTS_DIR }}"
357+
CACHE_ARCHIVE="${{ env.CUDA_CORE_ARTIFACTS_BASENAME }}.tar.gz"
358+
ls -l $CACHE_ARCHIVE
359+
mkdir -p $CACHE_DIR
360+
du -h $CACHE_ARCHIVE &&
361+
tar -x -f $CACHE_ARCHIVE -C $CACHE_DIR &&
362+
rm -f $CACHE_ARCHIVE || echo "WARNING: cache could not be retrieved."
363+
278364
- name: Display structure of downloaded cuda.core build artifacts
279-
shell: bash --noprofile --norc -xeuo pipefail {0}
280365
run: |
281366
pwd
282367
ls -lahR $CUDA_CORE_ARTIFACTS_DIR
@@ -295,7 +380,6 @@ jobs:
295380

296381
- name: Run cuda.bindings tests
297382
if: ${{ env.SKIP_CUDA_BINDINGS_TEST == '0' }}
298-
shell: bash --noprofile --norc -xeuo pipefail {0}
299383
run: |
300384
ls $CUDA_PATH
301385
@@ -311,7 +395,6 @@ jobs:
311395
popd
312396
313397
- name: Run cuda.core tests
314-
shell: bash --noprofile --norc -xeuo pipefail {0}
315398
run: |
316399
if [[ ${{ matrix.python-version }} == "3.13" ]]; then
317400
# TODO: remove this hack once cuda-python has a cp313 build
@@ -348,3 +431,4 @@ jobs:
348431
./.github/workflows/build-docs.yml
349432
with:
350433
build_ctk_ver: ${{ needs.build.outputs.BUILD_CTK_VER }}
434+
use_gh_cache: ${{ needs.build.outputs.USE_GH_CACHE }}

0 commit comments

Comments
 (0)