|
| 1 | +name: 'Build Service Images' |
| 2 | +description: 'Build and publish Chroma service images via docker-bake.hcl, with a short-circuit that skips the build when every target already exists in every configured registry.' |
| 3 | +inputs: |
| 4 | + COMMIT_SHA: |
| 5 | + description: 'Full commit SHA to build. Defaults to the currently-checked-out commit.' |
| 6 | + required: false |
| 7 | + default: '' |
| 8 | + PUSH: |
| 9 | + description: 'Flag for whether to push built images, must be a string set to "true" to push' |
| 10 | + required: false |
| 11 | + default: 'false' |
| 12 | + FORCE: |
| 13 | + description: 'If "true", skip the short-circuit check and always rebuild/push.' |
| 14 | + required: false |
| 15 | + default: 'false' |
| 16 | + AWS_REGION: |
| 17 | + description: 'AWS region of ECR' |
| 18 | + required: true |
| 19 | + AWS_ECR_OIDC_ARN: |
| 20 | + description: 'AWS ARN of the OIDC role to assume for logging into ECR' |
| 21 | + required: true |
| 22 | + GCP_WORKLOAD_IDENTITY_PROVIDER: |
| 23 | + description: 'GCP workload identity provider' |
| 24 | + required: true |
| 25 | + GCP_GITHUB_ACTIONS_SERVICE_ACCOUNT_EMAIL: |
| 26 | + description: 'GCP service account email' |
| 27 | + required: true |
| 28 | + GCP_ARTIFACT_REGISTRY_REGION: |
| 29 | + description: 'GCP artifact registry region' |
| 30 | + required: true |
| 31 | + GCP_ARTIFACT_REGISTRY_PROJECT_ID: |
| 32 | + description: 'GCP artifact registry project ID' |
| 33 | + required: true |
| 34 | + GCP_ARTIFACT_REGISTRY_NAME: |
| 35 | + description: 'GCP artifact registry name' |
| 36 | + required: true |
| 37 | + DOCKERHUB_USERNAME: |
| 38 | + description: 'DockerHub username for authentication' |
| 39 | + required: true |
| 40 | + DOCKERHUB_TOKEN: |
| 41 | + description: 'DockerHub token for authentication' |
| 42 | + required: true |
| 43 | + ADDRESS_SANITIZER: |
| 44 | + description: 'Enable Address Sanitizer for builds. Set to "1" to enable.' |
| 45 | + required: false |
| 46 | + default: '' |
| 47 | + ENABLE_AVX512: |
| 48 | + description: 'Enable AVX512 for builds. Set to "1" to enable.' |
| 49 | + required: false |
| 50 | + default: '' |
| 51 | + |
| 52 | +outputs: |
| 53 | + skipped: |
| 54 | + description: 'Whether the build+push was skipped because all target images already exist.' |
| 55 | + value: ${{ steps.short-circuit.outputs.skipped }} |
| 56 | + commit_short_sha: |
| 57 | + description: 'Short commit SHA used for image tags.' |
| 58 | + value: ${{ steps.short-shas.outputs.COMMIT_SHORT_SHA }} |
| 59 | + |
| 60 | +runs: |
| 61 | + using: "composite" |
| 62 | + steps: |
| 63 | + - name: Setup Blacksmith Docker cache |
| 64 | + uses: useblacksmith/setup-docker-builder@v1 |
| 65 | + |
| 66 | + - name: Resolve commit SHA |
| 67 | + shell: bash |
| 68 | + id: short-shas |
| 69 | + env: |
| 70 | + COMMIT_SHA_INPUT: ${{ inputs.COMMIT_SHA }} |
| 71 | + run: | |
| 72 | + set -euo pipefail |
| 73 | + if [[ -n "$COMMIT_SHA_INPUT" ]]; then |
| 74 | + COMMIT_SHA="$COMMIT_SHA_INPUT" |
| 75 | + else |
| 76 | + COMMIT_SHA=$(git rev-parse HEAD) |
| 77 | + fi |
| 78 | + echo "COMMIT_SHORT_SHA=$(echo "$COMMIT_SHA" | cut -c1-7)" >> "$GITHUB_OUTPUT" |
| 79 | +
|
| 80 | + - name: Configure AWS Credentials |
| 81 | + if: inputs.PUSH == 'true' |
| 82 | + uses: aws-actions/configure-aws-credentials@v3 |
| 83 | + with: |
| 84 | + role-to-assume: ${{ inputs.AWS_ECR_OIDC_ARN }} |
| 85 | + aws-region: ${{ inputs.AWS_REGION }} |
| 86 | + |
| 87 | + - name: Login to Amazon ECR |
| 88 | + if: inputs.PUSH == 'true' |
| 89 | + id: login-ecr |
| 90 | + uses: aws-actions/amazon-ecr-login@v2 |
| 91 | + |
| 92 | + - name: Authenticate to Google Cloud |
| 93 | + if: inputs.PUSH == 'true' |
| 94 | + id: auth-gcp |
| 95 | + uses: google-github-actions/auth@v2 |
| 96 | + with: |
| 97 | + workload_identity_provider: ${{ inputs.GCP_WORKLOAD_IDENTITY_PROVIDER }} |
| 98 | + service_account: ${{ inputs.GCP_GITHUB_ACTIONS_SERVICE_ACCOUNT_EMAIL }} |
| 99 | + |
| 100 | + - name: Configure Docker for GCP Artifact Registry |
| 101 | + if: inputs.PUSH == 'true' |
| 102 | + shell: bash |
| 103 | + env: |
| 104 | + GCP_AR_REGION: ${{ inputs.GCP_ARTIFACT_REGISTRY_REGION }} |
| 105 | + run: gcloud auth configure-docker "${GCP_AR_REGION}-docker.pkg.dev" --quiet |
| 106 | + |
| 107 | + - name: Login to DockerHub |
| 108 | + if: inputs.PUSH == 'true' |
| 109 | + uses: docker/login-action@v3 |
| 110 | + with: |
| 111 | + username: ${{ inputs.DOCKERHUB_USERNAME }} |
| 112 | + password: ${{ inputs.DOCKERHUB_TOKEN }} |
| 113 | + |
| 114 | + - name: Resolve registries |
| 115 | + shell: bash |
| 116 | + id: get-registries |
| 117 | + env: |
| 118 | + PUSH: ${{ inputs.PUSH }} |
| 119 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 120 | + GCP_AR_REGION: ${{ inputs.GCP_ARTIFACT_REGISTRY_REGION }} |
| 121 | + GCP_AR_PROJECT: ${{ inputs.GCP_ARTIFACT_REGISTRY_PROJECT_ID }} |
| 122 | + GCP_AR_NAME: ${{ inputs.GCP_ARTIFACT_REGISTRY_NAME }} |
| 123 | + run: | |
| 124 | + set -euo pipefail |
| 125 | +
|
| 126 | + if [[ "$PUSH" == "true" ]]; then |
| 127 | + printf '%s\n' "REGISTRY_AWS=${ECR_REGISTRY}" >> "$GITHUB_OUTPUT" |
| 128 | + printf '%s\n' "REGISTRY_GCP=${GCP_AR_REGION}-docker.pkg.dev/${GCP_AR_PROJECT}/${GCP_AR_NAME}" >> "$GITHUB_OUTPUT" |
| 129 | + printf '%s\n' "REGISTRY_DOCKERHUB=chromadb" >> "$GITHUB_OUTPUT" |
| 130 | + else |
| 131 | + printf '%s\n' "REGISTRY_AWS=local" >> "$GITHUB_OUTPUT" |
| 132 | + printf '%s\n' "REGISTRY_GCP=local" >> "$GITHUB_OUTPUT" |
| 133 | + printf '%s\n' "REGISTRY_DOCKERHUB=local" >> "$GITHUB_OUTPUT" |
| 134 | + fi |
| 135 | +
|
| 136 | + # Derives the set of image refs to probe from `docker buildx bake |
| 137 | + # --print`, so adding a new target to docker-bake.hcl doesn't require |
| 138 | + # editing this step. LOCAL_BUILD=false ensures the printed tags are |
| 139 | + # registry-qualified (the bake file only emits unqualified local tags |
| 140 | + # when LOCAL_BUILD=="true"). |
| 141 | + - name: Short-circuit if images already exist |
| 142 | + id: short-circuit |
| 143 | + shell: bash |
| 144 | + env: |
| 145 | + LOCAL_BUILD: 'false' |
| 146 | + REGISTRY_AWS: ${{ steps.get-registries.outputs.REGISTRY_AWS }} |
| 147 | + REGISTRY_GCP: ${{ steps.get-registries.outputs.REGISTRY_GCP }} |
| 148 | + REGISTRY_DOCKERHUB: ${{ steps.get-registries.outputs.REGISTRY_DOCKERHUB }} |
| 149 | + COMMIT_SHORT_SHA: ${{ steps.short-shas.outputs.COMMIT_SHORT_SHA }} |
| 150 | + ADDRESS_SANITIZER: ${{ inputs.ADDRESS_SANITIZER }} |
| 151 | + ENABLE_AVX512: ${{ inputs.ENABLE_AVX512 }} |
| 152 | + FORCE: ${{ inputs.FORCE }} |
| 153 | + PUSH: ${{ inputs.PUSH }} |
| 154 | + run: | |
| 155 | + set -euo pipefail |
| 156 | +
|
| 157 | + if [[ "$PUSH" != "true" ]]; then |
| 158 | + echo "PUSH != true; short-circuit check disabled." |
| 159 | + echo "skipped=false" >> "$GITHUB_OUTPUT" |
| 160 | + exit 0 |
| 161 | + fi |
| 162 | +
|
| 163 | + if [[ "$FORCE" == "true" ]]; then |
| 164 | + echo "FORCE=true; skipping short-circuit check." |
| 165 | + echo "skipped=false" >> "$GITHUB_OUTPUT" |
| 166 | + exit 0 |
| 167 | + fi |
| 168 | +
|
| 169 | + mapfile -t refs < <( |
| 170 | + docker buildx bake --print -f docker-bake.hcl \ |
| 171 | + | jq -r '.target | to_entries[].value.tags[]?' |
| 172 | + ) |
| 173 | +
|
| 174 | + if [[ ${#refs[@]} -eq 0 ]]; then |
| 175 | + echo "::error::No image tags emitted by docker-bake.hcl; refusing to short-circuit." |
| 176 | + exit 1 |
| 177 | + fi |
| 178 | +
|
| 179 | + all_present=true |
| 180 | + for ref in "${refs[@]}"; do |
| 181 | + if docker buildx imagetools inspect "$ref" >/dev/null 2>&1; then |
| 182 | + echo "found: $ref" |
| 183 | + else |
| 184 | + echo "missing: $ref" |
| 185 | + all_present=false |
| 186 | + fi |
| 187 | + done |
| 188 | +
|
| 189 | + if [[ "$all_present" == "true" ]]; then |
| 190 | + echo "All images already present at tag ${COMMIT_SHORT_SHA}; skipping build+push." |
| 191 | + echo "skipped=true" >> "$GITHUB_OUTPUT" |
| 192 | + else |
| 193 | + echo "skipped=false" >> "$GITHUB_OUTPUT" |
| 194 | + fi |
| 195 | +
|
| 196 | + # Build pass: fails fast on real build errors. The push is split out |
| 197 | + # below so registry flakes (DockerHub especially) can retry without |
| 198 | + # rebuilding. |
| 199 | + - name: Build service images |
| 200 | + if: steps.short-circuit.outputs.skipped != 'true' |
| 201 | + uses: docker/bake-action@v5 |
| 202 | + with: |
| 203 | + push: false |
| 204 | + files: docker-bake.hcl |
| 205 | + env: |
| 206 | + LOCAL_BUILD: ${{ inputs.PUSH == 'true' && 'false' || 'true' }} |
| 207 | + REGISTRY_AWS: ${{ steps.get-registries.outputs.REGISTRY_AWS }} |
| 208 | + REGISTRY_GCP: ${{ steps.get-registries.outputs.REGISTRY_GCP }} |
| 209 | + REGISTRY_DOCKERHUB: ${{ steps.get-registries.outputs.REGISTRY_DOCKERHUB }} |
| 210 | + COMMIT_SHORT_SHA: ${{ steps.short-shas.outputs.COMMIT_SHORT_SHA }} |
| 211 | + ADDRESS_SANITIZER: ${{ inputs.ADDRESS_SANITIZER }} |
| 212 | + ENABLE_AVX512: ${{ inputs.ENABLE_AVX512 }} |
| 213 | + |
| 214 | + # Retry to absorb transient registry failures. Buildx cache + registry |
| 215 | + # layer dedup make retries cheap — only whatever failed gets re-pushed. |
| 216 | + - name: Push service images |
| 217 | + if: inputs.PUSH == 'true' && steps.short-circuit.outputs.skipped != 'true' |
| 218 | + shell: bash |
| 219 | + env: |
| 220 | + LOCAL_BUILD: ${{ inputs.PUSH == 'true' && 'false' || 'true' }} |
| 221 | + REGISTRY_AWS: ${{ steps.get-registries.outputs.REGISTRY_AWS }} |
| 222 | + REGISTRY_GCP: ${{ steps.get-registries.outputs.REGISTRY_GCP }} |
| 223 | + REGISTRY_DOCKERHUB: ${{ steps.get-registries.outputs.REGISTRY_DOCKERHUB }} |
| 224 | + COMMIT_SHORT_SHA: ${{ steps.short-shas.outputs.COMMIT_SHORT_SHA }} |
| 225 | + ADDRESS_SANITIZER: ${{ inputs.ADDRESS_SANITIZER }} |
| 226 | + ENABLE_AVX512: ${{ inputs.ENABLE_AVX512 }} |
| 227 | + run: | |
| 228 | + set -euo pipefail |
| 229 | +
|
| 230 | + attempts=4 |
| 231 | + delay=30 |
| 232 | + for i in $(seq 1 "$attempts"); do |
| 233 | + if docker buildx bake --push -f docker-bake.hcl; then |
| 234 | + exit 0 |
| 235 | + fi |
| 236 | + if [[ "$i" -lt "$attempts" ]]; then |
| 237 | + echo "::warning::docker buildx bake --push failed on attempt $i/$attempts; retrying in ${delay}s..." |
| 238 | + sleep "$delay" |
| 239 | + delay=$((delay * 2)) |
| 240 | + fi |
| 241 | + done |
| 242 | + echo "::error::docker buildx bake --push failed after $attempts attempts" |
| 243 | + exit 1 |
0 commit comments