|
| 1 | +# Copyright 2025 Google LLC |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This workflow will build and push MaxDiffusion Docker image to GCR. |
| 16 | + |
| 17 | +name: Build and Push MaxDiffusion Docker Images |
| 18 | + |
| 19 | +on: |
| 20 | + workflow_call: |
| 21 | + inputs: |
| 22 | + image_name: |
| 23 | + required: true |
| 24 | + type: string |
| 25 | + device: |
| 26 | + required: true |
| 27 | + type: string |
| 28 | + build_mode: |
| 29 | + required: true |
| 30 | + type: string |
| 31 | + dockerfile: |
| 32 | + required: true |
| 33 | + type: string |
| 34 | + maxdiffusion_sha: |
| 35 | + required: true |
| 36 | + type: string |
| 37 | + image_date: |
| 38 | + required: true |
| 39 | + type: string |
| 40 | + base_image: |
| 41 | + required: false |
| 42 | + type: string |
| 43 | + default: '' |
| 44 | + |
| 45 | +permissions: |
| 46 | + contents: read |
| 47 | + |
| 48 | +jobs: |
| 49 | + build_and_push: |
| 50 | + runs-on: linux-x86-n2-16-buildkit |
| 51 | + container: google/cloud-sdk:524.0.0 |
| 52 | + if: > |
| 53 | + github.event_name == 'schedule' || |
| 54 | + github.event_name == 'pull_request' || |
| 55 | + github.event_name == 'workflow_dispatch' && ( |
| 56 | + github.event.inputs.target_device == 'all' || |
| 57 | + github.event.inputs.target_device == 'tpu' || |
| 58 | + github.event.inputs.target_device == 'gpu' |
| 59 | + ) |
| 60 | + steps: |
| 61 | + - name: Check if build should run |
| 62 | + id: check |
| 63 | + shell: bash |
| 64 | + run: | |
| 65 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.target_device }}" != "all" && "${{ github.event.inputs.target_device }}" != "${{ inputs.device }}" ]]; then |
| 66 | + echo "should_run=false" >> $GITHUB_OUTPUT |
| 67 | + echo "Skipping ${{ inputs.image_name }} build for device: ${{ inputs.device }} in ${{ inputs.build_mode }} mode." |
| 68 | + else |
| 69 | + echo "should_run=true" >> $GITHUB_OUTPUT |
| 70 | + echo "Building ${{ inputs.image_name }} for device: ${{ inputs.device }} in ${{ inputs.build_mode }} mode." |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Checkout MaxDiffusion |
| 74 | + uses: actions/checkout@v5 |
| 75 | + if: steps.check.outputs.should_run == 'true' |
| 76 | + with: |
| 77 | + # This ensures that every job clones the exact same commit as "setup" job |
| 78 | + ref: ${{ inputs.maxdiffusion_sha }} |
| 79 | + |
| 80 | + - name: Mark git repositories as safe |
| 81 | + run: git config --global --add safe.directory '*' |
| 82 | + if: steps.check.outputs.should_run == 'true' |
| 83 | + |
| 84 | + - name: Configure Docker |
| 85 | + run: gcloud auth configure-docker us-docker.pkg.dev,gcr.io -q |
| 86 | + if: steps.check.outputs.should_run == 'true' |
| 87 | + |
| 88 | + - name: Set up Docker BuildX |
| 89 | + uses: docker/setup-buildx-action@v3.11.1 |
| 90 | + if: steps.check.outputs.should_run == 'true' |
| 91 | + with: |
| 92 | + driver: remote |
| 93 | + endpoint: tcp://localhost:1234 |
| 94 | + |
| 95 | + - name: Build and push Docker image |
| 96 | + uses: docker/build-push-action@v6 |
| 97 | + if: steps.check.outputs.should_run == 'true' |
| 98 | + with: |
| 99 | + push: true |
| 100 | + context: . |
| 101 | + file: ${{ inputs.dockerfile }} |
| 102 | + tags: gcr.io/tpu-prod-env-multipod/${{ inputs.image_name }}:latest |
| 103 | + cache-from: type=gha |
| 104 | + outputs: type=image,compression=zstd,force-compression=true |
| 105 | + build-args: | |
| 106 | + DEVICE=${{ inputs.device }} |
| 107 | + MODE=${{ inputs.build_mode }} |
| 108 | + JAX_VERSION=NONE |
| 109 | + ${{ inputs.base_image != '' && format('BASEIMAGE={0}', inputs.base_image) || '' }} |
| 110 | +
|
| 111 | + - name: Add tags to Docker image |
| 112 | + if: steps.check.outputs.should_run == 'true' |
| 113 | + shell: bash |
| 114 | + run: | |
| 115 | + SOURCE_IMAGE="gcr.io/tpu-prod-env-multipod/${{ inputs.image_name }}" |
| 116 | +
|
| 117 | + # Add date tag |
| 118 | + gcloud container images add-tag "$SOURCE_IMAGE:latest" "$SOURCE_IMAGE:${{ inputs.image_date }}" --quiet |
| 119 | +
|
| 120 | + # Convert date to YYYYMMDD format |
| 121 | + clean_date=$(echo "${{ inputs.image_date }}" | sed 's/[-:]//g' | cut -c1-8) |
| 122 | +
|
| 123 | + # Add MaxDiffusion tag |
| 124 | + maxdiffusion_hash=$(git rev-parse --short HEAD) |
| 125 | + gcloud container images add-tag "$SOURCE_IMAGE:latest" "$SOURCE_IMAGE:maxdiffusion_${maxdiffusion_hash}_${clean_date}" --quiet |
| 126 | +
|
0 commit comments