Skip to content

Commit 63e25cc

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

File tree

1 file changed

+98
-21
lines changed

1 file changed

+98
-21
lines changed

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

+98-21
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,23 @@ 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+
echo "CUDA_CORE_ARTIFACT_BASENAME=cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}" >> $GITHUB_ENV
81+
echo "CUDA_CORE_ARTIFACT_NAME=${CUDA_CORE_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
7982
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
83+
echo "CUDA_BINDINGS_ARTIFACT_BASENAME=cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ matrix.cuda-version }}-${{ matrix.host-platform }}" >> $GITHUB_ENV
84+
echo "CUDA_BINDINGS_ARTIFACT_NAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
8185
echo "CUDA_BINDINGS_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_bindings/dist")" >> $GITHUB_ENV
8286
echo "CIBW_BUILD=${CIBW_BUILD}" >> $GITHUB_ENV
83-
87+
88+
# When the CI is run due to merging to main, we want it to populate GHA Cache not Artifacts,
89+
# so that CI workflows running on every branch have a fallback to use.
90+
if [[ "${{ github.ref_name}}" == main ]]; then
91+
echo "USE_CACHE=1" >> $GITHUB_ENV
92+
else
93+
echo "USE_CACHE=0" >> $GITHUB_ENV
94+
fi
95+
8496
- name: Dump environment
85-
shell: bash --noprofile --norc -xeuo pipefail {0}
8697
run: |
8798
env
8899
@@ -97,7 +108,6 @@ jobs:
97108
output-dir: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
98109

99110
- name: List the cuda.core artifacts directory
100-
shell: bash --noprofile --norc -xeuo pipefail {0}
101111
run: |
102112
if [[ "${{ matrix.host-platform }}" == win* ]]; then
103113
export CHOWN=chown
@@ -108,19 +118,29 @@ jobs:
108118
ls -lahR ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
109119
110120
- name: Check cuda.core wheel
111-
shell: bash --noprofile --norc -xeuo pipefail {0}
112121
run: |
113122
pip install twine
114123
twine check ${{ env.CUDA_CORE_ARTIFACTS_DIR }}/*.whl
124+
# unconditionally prepare the cache in case needed later
125+
tar -c -f "${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz" -C "${{ env.CUDA_CORE_ARTIFACTS_DIR }}" .
126+
du -h "${{ env.CUDA_CORE_ARTIFACT_BASENAME }}.tar.gz"
115127
116128
- name: Upload cuda.core build artifacts
129+
if: ${{ env.USE_CACHE == '0' }}
117130
uses: actions/upload-artifact@v4
118131
with:
119132
name: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
120133
path: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}/*.whl
121134
if-no-files-found: error
122135
overwrite: 'true'
123136

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

148168
- name: List the cuda.bindings artifacts directory
149-
shell: bash --noprofile --norc -xeuo pipefail {0}
150169
run: |
151170
if [[ "${{ matrix.host-platform }}" == win* ]]; then
152171
export CHOWN=chown
@@ -156,20 +175,30 @@ jobs:
156175
$CHOWN -R $(whoami) ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
157176
ls -lahR ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
158177
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
178+
- name: Check cuda.bindings wheel
179+
run: |
180+
# TODO: enable this after NVIDIA/cuda-python#297 is resolved
181+
# twine check ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}/*.whl
182+
# unconditionally prepare the cache in case needed later
183+
tar -c -f "${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz" -C "${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}" .
184+
du -h "${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz"
164185
165186
- name: Upload cuda.bindings build artifacts
187+
if: ${{ env.USE_CACHE == '0' }}
166188
uses: actions/upload-artifact@v4
167189
with:
168190
name: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
169191
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}/*.whl
170192
if-no-files-found: error
171193
overwrite: 'true'
172194

195+
- name: Cache cuda.bindings build artifacts
196+
if: ${{ env.USE_CACHE == '1' }}
197+
uses: actions/cache/save@v4
198+
with:
199+
key: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
200+
path: ${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}.tar.gz
201+
173202
- name: Pass environment variables to the next runner
174203
id: pass_env
175204
run: |
@@ -221,9 +250,11 @@ jobs:
221250
NVIDIA_VISIBLE_DEVICES: ${{ env.NVIDIA_VISIBLE_DEVICES }}
222251
needs:
223252
- build
253+
defaults:
254+
run:
255+
shell: bash --noprofile --norc -xeuo pipefail {0}
224256
steps:
225257
- name: Ensure GPU is working
226-
shell: bash --noprofile --norc -xeuo pipefail {0}
227258
run: nvidia-smi
228259

229260
- name: Checkout ${{ github.event.repository.name }}
@@ -232,7 +263,6 @@ jobs:
232263
fetch-depth: 0
233264

234265
- name: Set environment variables
235-
shell: bash --noprofile --norc -xeuo pipefail {0}
236266
run: |
237267
PYTHON_VERSION_FORMATTED=$(echo '${{ matrix.python-version }}' | tr -d '.')
238268
if [[ "${{ matrix.host-platform }}" == linux* ]]; then
@@ -251,32 +281,81 @@ jobs:
251281
fi
252282
253283
# 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
284+
echo "CUDA_CORE_ARTIFACT_BASENAME=cuda-core-python${PYTHON_VERSION_FORMATTED}-${{ matrix.host-platform }}" >> $GITHUB_ENV
285+
echo "CUDA_CORE_ARTIFACT_NAME=${CUDA_CORE_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
255286
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
287+
echo "CUDA_BINDINGS_ARTIFACT_BASENAME=cuda-bindings-python${PYTHON_VERSION_FORMATTED}-cuda${{ matrix.cuda-version }}-${{ matrix.host-platform }}" >> $GITHUB_ENV
288+
echo "CUDA_BINDINGS_ARTIFACT_NAME=${CUDA_BINDINGS_ARTIFACT_BASENAME}-${{ github.sha }}" >> $GITHUB_ENV
257289
echo "CUDA_BINDINGS_ARTIFACTS_DIR=$(realpath "$REPO_DIR/cuda_bindings/dist")" >> $GITHUB_ENV
258290
echo "SKIP_CUDA_BINDINGS_TEST=${SKIP_CUDA_BINDINGS_TEST}" >> $GITHUB_ENV
259291
292+
# We'll try GHA Artifacts first, and then fall back to GHA Cache
260293
- name: Download cuda.bindings build artifacts
294+
id: cuda-bindings-download
261295
uses: actions/download-artifact@v4
262296
with:
263297
name: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
264298
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}
265299

300+
- name: Restore cuda.bindings cache
301+
if: ${{ failure() && steps.cuda-bindings-download.conclusion == 'failure' }}
302+
id: cuda-bindings-cache
303+
uses: actions/cache/restore@v4
304+
with:
305+
key: ${{ env.CUDA_BINDINGS_ARTIFACT_NAME }}
306+
path: ${{ env.CUDA_BINDINGS_ARTIFACTS_BASENAME }}.tar.gz
307+
restore-keys: ${{ env.CUDA_BINDINGS_ARTIFACT_BASENAME }}
308+
fail-on-cache-miss: true
309+
310+
- name: Report cache restore status (hit)
311+
if: ${{ steps.cuda-bindings-cache.conclusion != 'skipped' &&
312+
steps.cuda-bindings-cache.outputs.cache-hit == 'true' }}
313+
run: |
314+
echo "cache is found"
315+
CACHE_DIR="${{ env.CUDA_BINDINGS_ARTIFACTS_DIR }}"
316+
CACHE_ARCHIVE="${{ env.CUDA_BINDINGS_ARTIFACTS_BASENAME }}.tar.gz"
317+
ls -l $CACHE_ARCHIVE
318+
mkdir -p $CACHE_DIR
319+
du -h $CACHE_ARCHIVE &&
320+
tar -x -f $CACHE_ARCHIVE -C $CACHE_DIR &&
321+
rm -f $CACHE_ARCHIVE || echo "WARNING: cache could not be retrieved."
322+
266323
- name: Display structure of downloaded cuda.bindings artifacts
267-
shell: bash --noprofile --norc -xeuo pipefail {0}
268324
run: |
269325
pwd
270326
ls -lahR $CUDA_BINDINGS_ARTIFACTS_DIR
271327
272328
- name: Download cuda.core build artifacts
329+
id: cuda-core-download
273330
uses: actions/download-artifact@v4
274331
with:
275332
name: ${{ env.CUDA_CORE_ARTIFACT_NAME }}
276333
path: ${{ env.CUDA_CORE_ARTIFACTS_DIR }}
277334

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

296375
- name: Run cuda.bindings tests
297376
if: ${{ env.SKIP_CUDA_BINDINGS_TEST == '0' }}
298-
shell: bash --noprofile --norc -xeuo pipefail {0}
299377
run: |
300378
ls $CUDA_PATH
301379
@@ -311,7 +389,6 @@ jobs:
311389
popd
312390
313391
- name: Run cuda.core tests
314-
shell: bash --noprofile --norc -xeuo pipefail {0}
315392
run: |
316393
if [[ ${{ matrix.python-version }} == "3.13" ]]; then
317394
# TODO: remove this hack once cuda-python has a cp313 build

0 commit comments

Comments
 (0)