-
Notifications
You must be signed in to change notification settings - Fork 0
212 lines (184 loc) · 6.77 KB
/
build-container.yml
File metadata and controls
212 lines (184 loc) · 6.77 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
name: Build and Push Dev Containers
on:
push:
branches:
- main
paths:
- '.devcontainer/**'
- '.github/workflows/build-container.yml'
workflow_dispatch:
env:
REGISTRY: ghcr.io
jobs:
discover-containers:
runs-on: ubuntu-latest
outputs:
containers: ${{ steps.find.outputs.containers }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Find all Dockerfiles
id: find
run: |
# Find all template directories containing a Dockerfile
CONTAINERS=$(find .devcontainer -mindepth 3 -name "Dockerfile" -printf '%h\n' | \
sed 's|.devcontainer/||' | sed 's|/.devcontainer||' | \
jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "containers=$CONTAINERS" >> $GITHUB_OUTPUT
echo "Found containers: $CONTAINERS"
check-updates:
runs-on: ubuntu-latest
outputs:
should_build: ${{ steps.check.outputs.should_build }}
claude_version: ${{ steps.versions.outputs.claude_version }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Restore cache
uses: actions/cache@v4
with:
path: |
.claude-version-cache
.dockerfile-hash-cache
key: build-cache-${{ github.run_id }}
restore-keys: |
build-cache-
- name: Get current versions
id: versions
run: |
# Get latest Claude Code version
CLAUDE_VERSION=$(npm view @anthropic-ai/claude-code version 2>/dev/null || echo "unknown")
echo "claude_version=$CLAUDE_VERSION" >> $GITHUB_OUTPUT
echo "Claude Code version: $CLAUDE_VERSION"
- name: Check if rebuild is needed
id: check
run: |
# For push events, always rebuild
if [[ "${{ github.event_name }}" == "push" ]]; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "Push event - will rebuild"
exit 0
fi
# For workflow_dispatch, always rebuild
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "Manual trigger - will rebuild"
exit 0
fi
# For cron, check versions and hashes
echo "Checking for changes..."
CURRENT_CLAUDE_VERSION="${{ steps.versions.outputs.claude_version }}"
CACHE_FILE=".claude-version-cache"
# Check if Claude Code version changed
if [[ -f "$CACHE_FILE" ]]; then
CACHED_VERSION=$(cat "$CACHE_FILE")
if [[ "$CACHED_VERSION" != "$CURRENT_CLAUDE_VERSION" ]]; then
echo "Claude Code version changed: $CACHED_VERSION -> $CURRENT_CLAUDE_VERSION"
echo "should_build=true" >> $GITHUB_OUTPUT
exit 0
fi
else
echo "No version cache found - will build"
echo "should_build=true" >> $GITHUB_OUTPUT
exit 0
fi
# Check hash of all Dockerfiles
DOCKERFILE_HASH=$(find .devcontainer -name "Dockerfile" -exec sha256sum {} \; | sort | sha256sum | cut -d' ' -f1)
CACHE_DOCKERFILE=".dockerfile-hash-cache"
if [[ -f "$CACHE_DOCKERFILE" ]]; then
CACHED_HASH=$(cat "$CACHE_DOCKERFILE")
if [[ "$CACHED_HASH" != "$DOCKERFILE_HASH" ]]; then
echo "Dockerfiles changed"
echo "should_build=true" >> $GITHUB_OUTPUT
exit 0
fi
else
echo "No Dockerfile cache found - will build"
echo "should_build=true" >> $GITHUB_OUTPUT
exit 0
fi
echo "No changes detected - skipping build"
echo "should_build=false" >> $GITHUB_OUTPUT
build-and-push:
needs: [discover-containers, check-updates]
if: needs.check-updates.outputs.should_build == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
matrix:
container: ${{ fromJson(needs.discover-containers.outputs.containers) }}
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set image name
id: image
run: |
CONTAINER="${{ matrix.container }}"
OWNER="${{ github.repository_owner }}"
# Generate image name based on folder
case "$CONTAINER" in
"base")
IMAGE_NAME="devcontainer-claude-code"
;;
"base-with-bmad")
IMAGE_NAME="devcontainer-claude-code-bmad"
;;
*)
IMAGE_NAME="devcontainer-claude-code-${CONTAINER}"
;;
esac
echo "name=${OWNER}/${IMAGE_NAME}" >> $GITHUB_OUTPUT
echo "Image name: ${OWNER}/${IMAGE_NAME}"
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }}
tags: |
type=raw,value=latest
type=raw,value={{date 'YYYYMMDD'}}
type=sha,prefix=
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .devcontainer/${{ matrix.container }}/.devcontainer
file: .devcontainer/${{ matrix.container }}/.devcontainer/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: |
${{ steps.meta.outputs.labels }}
org.opencontainers.image.claude-code-version=${{ needs.check-updates.outputs.claude_version }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha,scope=${{ matrix.container }}
cache-to: type=gha,mode=max,scope=${{ matrix.container }}
update-cache:
needs: [check-updates, build-and-push]
if: needs.check-updates.outputs.should_build == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Update version cache
run: |
echo "${{ needs.check-updates.outputs.claude_version }}" > .claude-version-cache
find .devcontainer -name "Dockerfile" -exec sha256sum {} \; | sort | sha256sum | cut -d' ' -f1 > .dockerfile-hash-cache
- name: Save cache
uses: actions/cache@v4
with:
path: |
.claude-version-cache
.dockerfile-hash-cache
key: build-cache-${{ github.run_id }}