-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaction.yml
More file actions
296 lines (249 loc) · 10.5 KB
/
action.yml
File metadata and controls
296 lines (249 loc) · 10.5 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
name: Kaggle Script Action
author: Kevin Kibe
description: Run model training and CI/CD workflows on Kaggle kernels with free GPU/TPU resources.
inputs:
username:
description: kaggle username
required: true
key:
description: kaggle token
required: true
title:
description: The title of the kernel
required: true
custom_script:
description: "Custom script content to run in the notebook"
required: true
default: |
print('Success')
enable_gpu:
description: Whether or not kernel should run on a GPU.
required: false
default: "false"
enable_tpu:
description: Whether or not kernel should run on a TPU.
required: false
default: "false"
enable_internet:
description: Whether or not kernel should be able to access the internet.
required: false
default: "true"
dataset_sources:
description: A list of data sources that kernel should use. Each dataset is specified as {username}/{dataset-slug}.
required: false
default: ""
competition_sources:
description: A list of competition data sources that kernel should use.
required: false
default: ""
kernel_sources:
description: A list of kernel data sources that kernel should use. Each dataset is specified as {username}/{kernel-slug}.
required: false
default: ""
sleep_time:
description: Duration (in seconds) before checking the status of kernel execution completion
required: false
default: 15
working_subdir:
description: Subdirectory inside /kaggle/working/$REPO_NAME where dependencies are installed and the custom script is run
required: false
default: ""
outputs:
automatic_releases_tag:
description: The release tag this action just processed
value: ""
upload_url:
description: The URL for uploading additional assets to the release
value: ""
runs:
using: composite
steps:
- name: Install kaggle CLI tools
shell: bash
run: |
python -m pip install --upgrade kaggle --user
- name: Setup kaggle.json
shell: bash
run: |
log_message() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
log_message "Creating the Kaggle configuration directory at ~/.kaggle..."
mkdir -p ~/.kaggle
log_message "Writing credentials to kaggle.json..."
if [ -z "${{ inputs.username }}" ]; then
echo "::error::Username is required but was empty"
exit 1
fi
if [ -z "${{ inputs.key }}" ]; then
echo "::error::Kaggle key is required but was empty"
exit 1
fi
echo "{\"username\":\"${{ inputs.username }}\",\"key\":\"${{ inputs.key }}\"}" > ~/.kaggle/kaggle.json
log_message "Setting appropriate permissions for kaggle.json (read and write for user only)..."
chmod 600 ~/.kaggle/kaggle.json
log_message "Kaggle configuration setup complete."
- name: Set up Notebook Kernel
shell: bash
run: |
log_message() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
log_message "Setting up the notebook for Kaggle execution..."
action_path=$(realpath "${{ github.action_path }}")
OUTPUT_NOTEBOOK="$action_path/script.ipynb"
log_message "Notebook will be saved as: $OUTPUT_NOTEBOOK"
REPO_URL=$(git config --get remote.origin.url)
REPO_NAME=$(basename "$REPO_URL" .git)
BRANCH_NAME="${GITHUB_HEAD_REF:-$(git rev-parse --abbrev-ref HEAD)}"
log_message "Cloning repository URL: $REPO_URL on branch: $BRANCH_NAME"
CUSTOM_SCRIPT="${{ inputs.custom_script }}"
escaped_custom_script=$(echo "$CUSTOM_SCRIPT" | awk '{printf "%s\\n", $0}')
WORKING_SUBDIR="${{ inputs.working_subdir }}"
TARGET_DIR="/kaggle/working/$REPO_NAME${WORKING_SUBDIR:+/$WORKING_SUBDIR}"
dependencies="!cd $TARGET_DIR && pip install -r requirements.txt"
log_message "Adding dependency installation script to notebook: $dependencies"
log_message "Adding custom script to notebook: $CUSTOM_SCRIPT"
cat <<EOM > "$OUTPUT_NOTEBOOK"
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!git clone --branch $BRANCH_NAME $REPO_URL.git"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"$dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!cd $TARGET_DIR && $escaped_custom_script"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
EOM
log_message "Notebook '$OUTPUT_NOTEBOOK' created successfully"
- name: Setup test
shell: bash
run: |
log_message() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
action_path=$(realpath "${{ github.action_path }}")
OUTPUT_NOTEBOOK="$action_path/script.ipynb"
log_message "Navigating to the action path: '$action_path'"
cd "$action_path"
log_message "Initializing Kaggle kernel metadata..."
kaggle kernels init -p "$action_path"
json_path="$action_path/kernel-metadata.json"
log_message "Kernel metadata template created at: $json_path"
metadata=$(jq -r '. | tojson' < "$json_path")
kernel_name="${{ inputs.title }}"
kernel_slug=$(echo "$kernel_name" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g')
log_message "Updating metadata with specified inputs..."
metadata=$(echo "$metadata" | jq --arg title "${{ inputs.title }}" \
--arg id "${{ inputs.username }}/$kernel_slug" \
--arg code_file "$OUTPUT_NOTEBOOK" \
--argjson enable_gpu "${{ inputs.enable_gpu }}" \
--argjson enable_internet "${{ inputs.enable_internet }}" \
--argjson enable_tpu "${{ inputs.enable_tpu }}" \
'. |
.id = $id |
.title = $title |
.code_file = $code_file |
.language = "python" |
.kernel_type = "notebook" |
.enable_gpu = $enable_gpu |
.enable_internet = $enable_internet |
.enable_tpu = $enable_tpu')
echo "Saving updated metadata back to $json_path"
echo "$metadata" | jq '.' > "$json_path"
echo "Final kernel metadata content:"
cat "$json_path"
log_message "Pushing Kaggle kernel with updated metadata..."
kaggle kernels push -p "$action_path"
log_message "Kaggle kernel setup and push completed successfully."
- name: Check status
shell: bash
run: |
username="${{ inputs.username }}"
raw_kernel_name="${{ inputs.title }}"
kernel_slug=$(echo "$raw_kernel_name" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g')
kernel_name="${username}/${kernel_slug}"
action_path="${{ github.action_path }}"
action_path=$(realpath "${{ github.action_path }}")
echo "Checking status for Kaggle kernel: '$kernel_name'"
log_message() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
while true; do
log_message "Retrieving kernel status..."
status=$(kaggle kernels status "$kernel_name" 2>&1)
if [[ "$status" == *"403"* || "$status" == *"denied"* ]]; then
log_message "::error::Access denied or kernel not found. Check if the API key has permissions and if the kernel name is correct."
exit 1
elif [[ "$status" == *"error"* ]]; then
log_message "::error::FAIL: Script(s) execution failed."
exit 1
elif [[ "$status" == *"cancel"* ]]; then
log_message "::error::FAIL: Script(s) failed. The Kaggle kernel has been canceled."
exit 1
elif [[ "$status" == *"complete"* ]]; then
log_message "SUCCESS: Script execution completed successfully!"
break
else
log_message "Kernel is still running. Rechecking in "${{ inputs.sleep_time }}" seconds..."
sleep "${{ inputs.sleep_time }}"
fi
done
log_file="./$kernel_slug.log"
log_message "::group::Full log"
log_message "Fetching full kernel output log for '$kernel_name':"
kaggle kernels output "$kernel_name" -p . 2>&1
if [ -f "$log_file" ]; then
tail -n 50 "$log_file" | jq -c '.[]' | while IFS= read -r entry; do
data_field=$(echo "$entry" | jq -r '.data')
[[ -n "$data_field" ]] && log_message "$data_field"
if echo "$data_field" | grep -Ei "Error|Traceback|Exception|404 Client Error|failed|FAIL"; then
log_message "::error::Error found in log: $data_field"
exit 1
fi
done
else
log_message "Log file '$log_file' not found."
fi
log_message "::endgroup::"
if [[ "$status" == *"403"* || "$status" == *"denied"* || "$status" == *"error"* || "$status" == *"cancel"* || "$status" == *"failed"* ]]; then
exit 1
fi
branding:
icon: upload
color: green