-
Notifications
You must be signed in to change notification settings - Fork 0
243 lines (205 loc) · 8.53 KB
/
cache-bitnet-cpp.yml
File metadata and controls
243 lines (205 loc) · 8.53 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
name: Cache BitNet.cpp Libraries
on:
schedule:
# Build cache daily at 2 AM UTC
- cron: '0 2 * * *'
workflow_dispatch:
inputs:
bitnet_cpp_tag:
description: 'BitNet.cpp tag to cache'
required: false
default: 'latest'
type: string
force_rebuild:
description: 'Force rebuild even if cache exists'
required: false
default: false
type: boolean
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}/bitnet-cpp-cache
jobs:
determine-versions:
name: Determine BitNet.cpp Versions to Cache
runs-on: ubuntu-22.04
timeout-minutes: 10
outputs:
versions: ${{ steps.versions.outputs.versions }}
steps:
- name: Checkout repository
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
- name: Determine versions to cache
id: versions
run: |
# Get the pinned version from ci/fetch_bitnet_cpp.sh
PINNED_VERSION=$(grep -o 'BITNET_CPP_TAG="[^"]*"' ci/fetch_bitnet_cpp.sh | cut -d'"' -f2 || echo "main")
# Get latest release version
LATEST_VERSION=$(curl -s https://api.github.com/repos/microsoft/BitNet/releases/latest | jq -r '.tag_name // "main"')
# Manual override from workflow input
MANUAL_VERSION="${{ github.event.inputs.bitnet_cpp_tag }}"
# Create version list (remove duplicates)
VERSIONS=$(echo -e "$PINNED_VERSION\n$LATEST_VERSION\n$MANUAL_VERSION" | grep -v "^$" | sort -u | jq -R . | jq -s .)
echo "Versions to cache: $VERSIONS"
echo "versions=$VERSIONS" >> $GITHUB_OUTPUT
build-cache:
name: Build Cache for ${{ matrix.version }}
needs: determine-versions
runs-on: ubuntu-22.04
timeout-minutes: 60
strategy:
fail-fast: false
matrix:
version: ${{ fromJson(needs.determine-versions.outputs.versions) }}
platform: [linux/amd64, linux/arm64]
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3
- name: Log in to Container Registry
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract platform info
id: platform
run: |
PLATFORM_PAIR=${{ matrix.platform }}
PLATFORM_ARCH=${PLATFORM_PAIR##*/}
echo "arch=$PLATFORM_ARCH" >> $GITHUB_OUTPUT
echo "pair=$PLATFORM_PAIR" >> $GITHUB_OUTPUT
- name: Generate cache key
id: cache-key
run: |
# Create deterministic cache key based on version and platform
VERSION="${{ matrix.version }}"
PLATFORM="${{ steps.platform.outputs.arch }}"
# Get commit SHA for the version (if it's a tag)
if [[ "$VERSION" != "main" && "$VERSION" != "latest" ]]; then
COMMIT_SHA=$(curl -s "https://api.github.com/repos/microsoft/BitNet/git/refs/tags/$VERSION" | jq -r '.object.sha // "unknown"')
else
COMMIT_SHA=$(curl -s "https://api.github.com/repos/microsoft/BitNet/commits/main" | jq -r '.sha // "unknown"')
fi
CACHE_KEY="${VERSION}-${COMMIT_SHA:0:8}-${PLATFORM}"
echo "key=$CACHE_KEY" >> $GITHUB_OUTPUT
echo "sha=$COMMIT_SHA" >> $GITHUB_OUTPUT
- name: Check if cache already exists
id: cache-check
run: |
IMAGE_TAG="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.cache-key.outputs.key }}"
# Check if image already exists
if docker manifest inspect "$IMAGE_TAG" >/dev/null 2>&1; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "Cache already exists for $IMAGE_TAG"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "Cache does not exist for $IMAGE_TAG"
fi
- name: Build BitNet.cpp cache image
if: steps.cache-check.outputs.exists == 'false' || github.event.inputs.force_rebuild == 'true'
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5
with:
context: .
file: ci/Dockerfile.bitnet-cpp-cache
platforms: ${{ matrix.platform }}
push: true
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.cache-key.outputs.key }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.version }}-${{ steps.platform.outputs.arch }}-latest
build-args: |
BITNET_CPP_VERSION=${{ matrix.version }}
BITNET_CPP_SHA=${{ steps.cache-key.outputs.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
labels: |
org.opencontainers.image.title=BitNet.cpp Cache
org.opencontainers.image.description=Pre-built BitNet.cpp libraries for cross-validation
org.opencontainers.image.version=${{ matrix.version }}
org.opencontainers.image.revision=${{ steps.cache-key.outputs.sha }}
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
bitnet.cpp.version=${{ matrix.version }}
bitnet.cpp.sha=${{ steps.cache-key.outputs.sha }}
bitnet.cpp.platform=${{ steps.platform.outputs.arch }}
- name: Test cached image
if: steps.cache-check.outputs.exists == 'false' || github.event.inputs.force_rebuild == 'true'
run: |
IMAGE_TAG="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.cache-key.outputs.key }}"
# Test that the cached libraries work
docker run --rm --platform=${{ matrix.platform }} "$IMAGE_TAG" /bin/bash -c "
echo 'Testing cached BitNet.cpp libraries...'
ls -la /opt/bitnet_cpp/
ls -la /opt/bitnet_cpp/lib/
ls -la /opt/bitnet_cpp/include/
# Test that we can link against the libraries
echo 'int main() { return 0; }' > test.cpp
g++ -I/opt/bitnet_cpp/include -L/opt/bitnet_cpp/lib test.cpp -o test || echo 'Link test failed (expected for minimal test)'
echo 'Cache test completed successfully'
"
cleanup-old-cache:
name: Cleanup Old Cache Images
needs: [determine-versions, build-cache]
runs-on: ubuntu-22.04
timeout-minutes: 15
if: always()
permissions:
contents: read
packages: write
steps:
- name: Log in to Container Registry
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Cleanup old cache images
run: |
# Keep only the last 10 cache images per platform to save storage
echo "Cleaning up old cache images..."
# This would require GitHub CLI or API calls to list and delete old images
# For now, we'll rely on GitHub's automatic cleanup policies
echo "Cleanup will be handled by GitHub's retention policies"
update-ci-cache-config:
name: Update CI Cache Configuration
needs: [determine-versions, build-cache]
runs-on: ubuntu-22.04
timeout-minutes: 10
if: success()
steps:
- name: Checkout repository
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Update cache configuration
run: |
# Update ci/cache-config.json with latest cache information
mkdir -p ci
cat > ci/cache-config.json << EOF
{
"last_updated": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"registry": "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}",
"versions": $(echo '${{ needs.determine-versions.outputs.versions }}' | jq .),
"platforms": ["linux/amd64", "linux/arm64"],
"cache_ttl_hours": 168,
"description": "Pre-built BitNet.cpp libraries for faster CI builds"
}
EOF
echo "Updated cache configuration:"
cat ci/cache-config.json
- name: Commit cache configuration
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
if git diff --quiet ci/cache-config.json; then
echo "No changes to cache configuration"
else
git add ci/cache-config.json
git commit -m "Update BitNet.cpp cache configuration [skip ci]"
git push
fi