|
| 1 | +name: Build and Push Dev Container |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - '.devcontainer/**' |
| 9 | + - '.github/workflows/build-container.yml' |
| 10 | + schedule: |
| 11 | + # Toutes les 24h à 2h du matin UTC |
| 12 | + - cron: '0 2 * * *' |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +env: |
| 16 | + REGISTRY: ghcr.io |
| 17 | + IMAGE_NAME: ${{ github.repository_owner }}/devcontainer-claude-code |
| 18 | + |
| 19 | +jobs: |
| 20 | + check-updates: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + outputs: |
| 23 | + should_build: ${{ steps.check.outputs.should_build }} |
| 24 | + claude_version: ${{ steps.versions.outputs.claude_version }} |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Get current versions |
| 30 | + id: versions |
| 31 | + run: | |
| 32 | + # Récupérer la dernière version de Claude Code |
| 33 | + CLAUDE_VERSION=$(npm view @anthropic-ai/claude-code version 2>/dev/null || echo "unknown") |
| 34 | + echo "claude_version=$CLAUDE_VERSION" >> $GITHUB_OUTPUT |
| 35 | + echo "Claude Code version: $CLAUDE_VERSION" |
| 36 | +
|
| 37 | + - name: Check if rebuild is needed |
| 38 | + id: check |
| 39 | + run: | |
| 40 | + # Pour les push, toujours rebuild |
| 41 | + if [[ "${{ github.event_name }}" == "push" ]]; then |
| 42 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 43 | + echo "Push event - will rebuild" |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | +
|
| 47 | + # Pour les workflow_dispatch, toujours rebuild |
| 48 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 49 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 50 | + echo "Manual trigger - will rebuild" |
| 51 | + exit 0 |
| 52 | + fi |
| 53 | +
|
| 54 | + # Pour le cron, vérifier si l'image existe et comparer les versions |
| 55 | + echo "Checking existing image..." |
| 56 | +
|
| 57 | + # Tenter de récupérer les labels de l'image existante |
| 58 | + TOKEN=$(echo ${{ secrets.GITHUB_TOKEN }} | base64) |
| 59 | +
|
| 60 | + MANIFEST=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 61 | + "https://ghcr.io/v2/${{ env.IMAGE_NAME }}/manifests/latest" 2>/dev/null || echo "") |
| 62 | +
|
| 63 | + if [[ -z "$MANIFEST" || "$MANIFEST" == *"error"* ]]; then |
| 64 | + echo "No existing image found - will build" |
| 65 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 66 | + exit 0 |
| 67 | + fi |
| 68 | +
|
| 69 | + # Récupérer la version de Claude Code dans l'image existante via le cache |
| 70 | + CACHE_FILE=".claude-version-cache" |
| 71 | + CURRENT_CLAUDE_VERSION="${{ steps.versions.outputs.claude_version }}" |
| 72 | +
|
| 73 | + # Utiliser le cache GitHub pour stocker la dernière version buildée |
| 74 | + if [[ -f "$CACHE_FILE" ]]; then |
| 75 | + CACHED_VERSION=$(cat "$CACHE_FILE") |
| 76 | + if [[ "$CACHED_VERSION" != "$CURRENT_CLAUDE_VERSION" ]]; then |
| 77 | + echo "Claude Code version changed: $CACHED_VERSION -> $CURRENT_CLAUDE_VERSION" |
| 78 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 79 | + exit 0 |
| 80 | + fi |
| 81 | + fi |
| 82 | +
|
| 83 | + # Vérifier le hash du Dockerfile |
| 84 | + DOCKERFILE_HASH=$(sha256sum .devcontainer/Dockerfile | cut -d' ' -f1) |
| 85 | + CACHE_DOCKERFILE=".dockerfile-hash-cache" |
| 86 | +
|
| 87 | + if [[ -f "$CACHE_DOCKERFILE" ]]; then |
| 88 | + CACHED_HASH=$(cat "$CACHE_DOCKERFILE") |
| 89 | + if [[ "$CACHED_HASH" != "$DOCKERFILE_HASH" ]]; then |
| 90 | + echo "Dockerfile changed" |
| 91 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 92 | + exit 0 |
| 93 | + fi |
| 94 | + else |
| 95 | + echo "No cache found - will build" |
| 96 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 97 | + exit 0 |
| 98 | + fi |
| 99 | +
|
| 100 | + echo "No changes detected - skipping build" |
| 101 | + echo "should_build=false" >> $GITHUB_OUTPUT |
| 102 | +
|
| 103 | + - name: Cache version info |
| 104 | + if: steps.check.outputs.should_build == 'true' |
| 105 | + uses: actions/cache@v4 |
| 106 | + with: |
| 107 | + path: | |
| 108 | + .claude-version-cache |
| 109 | + .dockerfile-hash-cache |
| 110 | + key: build-cache-${{ github.run_id }} |
| 111 | + restore-keys: | |
| 112 | + build-cache- |
| 113 | +
|
| 114 | + build-and-push: |
| 115 | + needs: check-updates |
| 116 | + if: needs.check-updates.outputs.should_build == 'true' |
| 117 | + runs-on: ubuntu-latest |
| 118 | + permissions: |
| 119 | + contents: read |
| 120 | + packages: write |
| 121 | + |
| 122 | + steps: |
| 123 | + - name: Checkout |
| 124 | + uses: actions/checkout@v4 |
| 125 | + |
| 126 | + - name: Set up QEMU |
| 127 | + uses: docker/setup-qemu-action@v3 |
| 128 | + |
| 129 | + - name: Set up Docker Buildx |
| 130 | + uses: docker/setup-buildx-action@v3 |
| 131 | + |
| 132 | + - name: Log in to Container Registry |
| 133 | + uses: docker/login-action@v3 |
| 134 | + with: |
| 135 | + registry: ${{ env.REGISTRY }} |
| 136 | + username: ${{ github.actor }} |
| 137 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 138 | + |
| 139 | + - name: Extract metadata |
| 140 | + id: meta |
| 141 | + uses: docker/metadata-action@v5 |
| 142 | + with: |
| 143 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 144 | + tags: | |
| 145 | + type=raw,value=latest |
| 146 | + type=raw,value={{date 'YYYYMMDD'}} |
| 147 | + type=sha,prefix= |
| 148 | +
|
| 149 | + - name: Build and push |
| 150 | + uses: docker/build-push-action@v5 |
| 151 | + with: |
| 152 | + context: .devcontainer |
| 153 | + file: .devcontainer/Dockerfile |
| 154 | + push: true |
| 155 | + tags: ${{ steps.meta.outputs.tags }} |
| 156 | + labels: | |
| 157 | + ${{ steps.meta.outputs.labels }} |
| 158 | + org.opencontainers.image.claude-code-version=${{ needs.check-updates.outputs.claude_version }} |
| 159 | + platforms: linux/amd64,linux/arm64 |
| 160 | + cache-from: type=gha |
| 161 | + cache-to: type=gha,mode=max |
| 162 | + |
| 163 | + - name: Update version cache |
| 164 | + run: | |
| 165 | + echo "${{ needs.check-updates.outputs.claude_version }}" > .claude-version-cache |
| 166 | + sha256sum .devcontainer/Dockerfile | cut -d' ' -f1 > .dockerfile-hash-cache |
| 167 | +
|
| 168 | + - name: Save cache |
| 169 | + uses: actions/cache@v4 |
| 170 | + with: |
| 171 | + path: | |
| 172 | + .claude-version-cache |
| 173 | + .dockerfile-hash-cache |
| 174 | + key: build-cache-${{ github.run_id }} |
0 commit comments