|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 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 | +# http://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 | +name: Build container |
| 15 | + |
| 16 | +on: |
| 17 | + workflow_call: |
| 18 | + inputs: |
| 19 | + build-ref: |
| 20 | + required: false |
| 21 | + default: ${{ github.sha }} |
| 22 | + description: Ref, branch, or SHA to build. |
| 23 | + type: string |
| 24 | + image-name: |
| 25 | + required: true |
| 26 | + description: Name of the image to build and push. |
| 27 | + type: string |
| 28 | + build-args: |
| 29 | + required: false |
| 30 | + default: "" |
| 31 | + description: Additional Docker build args. |
| 32 | + type: string |
| 33 | + build-contexts: |
| 34 | + required: false |
| 35 | + default: "" |
| 36 | + description: Additional Docker build contexts. |
| 37 | + type: string |
| 38 | + dockerfile: |
| 39 | + required: true |
| 40 | + description: Path to the Dockerfile. |
| 41 | + type: string |
| 42 | + platform: |
| 43 | + required: true |
| 44 | + description: Docker build platform. |
| 45 | + type: string |
| 46 | + runner: |
| 47 | + required: true |
| 48 | + description: Runner to use for the build. |
| 49 | + type: string |
| 50 | + registry: |
| 51 | + required: true |
| 52 | + description: Container registry to push to. |
| 53 | + type: string |
| 54 | + target: |
| 55 | + required: false |
| 56 | + default: "" |
| 57 | + description: Dockerfile stage to build. |
| 58 | + type: string |
| 59 | + |
| 60 | +permissions: |
| 61 | + contents: read |
| 62 | + pull-requests: read |
| 63 | + |
| 64 | +defaults: |
| 65 | + run: |
| 66 | + shell: bash -x -e -u -o pipefail {0} |
| 67 | + |
| 68 | +jobs: |
| 69 | + build: |
| 70 | + runs-on: ${{ inputs.runner }} |
| 71 | + env: |
| 72 | + REGISTRY: ${{ inputs.registry }} |
| 73 | + IMAGE_NAME: ${{ inputs.image-name }} |
| 74 | + GH_REF: ${{ github.ref }} |
| 75 | + RUN_ID: ${{ github.run_id }} |
| 76 | + steps: |
| 77 | + - name: Checkout repository |
| 78 | + uses: actions/checkout@v6 |
| 79 | + with: |
| 80 | + ref: ${{ inputs.build-ref }} |
| 81 | + submodules: recursive |
| 82 | + |
| 83 | + - name: Set up Docker Buildx |
| 84 | + uses: docker/setup-buildx-action@v3 |
| 85 | + |
| 86 | + - name: Get recently merged PR cache refs |
| 87 | + id: recent_pr_cache_refs |
| 88 | + uses: actions/github-script@v8 |
| 89 | + env: |
| 90 | + REGISTRY: ${{ inputs.registry }} |
| 91 | + IMAGE_NAME: ${{ inputs.image-name }} |
| 92 | + with: |
| 93 | + script: | |
| 94 | + const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); |
| 95 | + const result = await github.graphql(` |
| 96 | + query($owner: String!, $repo: String!) { |
| 97 | + repository(owner: $owner, name: $repo) { |
| 98 | + pullRequests(states: MERGED, first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) { |
| 99 | + nodes { |
| 100 | + number |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + `, { owner, repo }); |
| 106 | +
|
| 107 | + const refs = result.repository.pullRequests.nodes |
| 108 | + .map(({ number }) => `type=registry,ref=${process.env.REGISTRY}/${process.env.IMAGE_NAME}:${number}-buildcache,mode=max`) |
| 109 | + .join("\n"); |
| 110 | +
|
| 111 | + core.setOutput("cache-from", refs); |
| 112 | + core.info(`Found ${result.repository.pullRequests.nodes.length} recently merged PR cache refs.`); |
| 113 | +
|
| 114 | + - name: Compute build metadata |
| 115 | + id: build_meta |
| 116 | + shell: bash |
| 117 | + run: | |
| 118 | + set -euo pipefail |
| 119 | +
|
| 120 | + PR_NUMBER="" |
| 121 | + if [[ "$GH_REF" =~ refs/heads/pull-request/([0-9]+) ]]; then |
| 122 | + PR_NUMBER="${BASH_REMATCH[1]}" |
| 123 | + fi |
| 124 | +
|
| 125 | + TAGS=("$REGISTRY/$IMAGE_NAME:$RUN_ID") |
| 126 | + if [[ "$GH_REF" == "refs/heads/main" ]]; then |
| 127 | + CACHE_KEY="main" |
| 128 | + TAGS+=("$REGISTRY/$IMAGE_NAME:main") |
| 129 | + elif [[ -n "$PR_NUMBER" ]]; then |
| 130 | + CACHE_KEY="$PR_NUMBER" |
| 131 | + TAGS+=("$REGISTRY/$IMAGE_NAME:$PR_NUMBER") |
| 132 | + else |
| 133 | + CACHE_KEY=$(printf '%s' "${GITHUB_REF_NAME:-$RUN_ID}" | tr '/' '-' | tr -cd '[:alnum:]._-') |
| 134 | + if [[ -z "$CACHE_KEY" ]]; then |
| 135 | + CACHE_KEY="$RUN_ID" |
| 136 | + fi |
| 137 | + fi |
| 138 | +
|
| 139 | + CACHE_FROM=( |
| 140 | + "type=registry,ref=$REGISTRY/$IMAGE_NAME:main-buildcache,mode=max" |
| 141 | + ) |
| 142 | + if [[ "$CACHE_KEY" != "main" ]]; then |
| 143 | + CACHE_FROM+=("type=registry,ref=$REGISTRY/$IMAGE_NAME:$CACHE_KEY-buildcache,mode=max") |
| 144 | + fi |
| 145 | +
|
| 146 | + { |
| 147 | + echo "tags<<EOF" |
| 148 | + printf '%s\n' "${TAGS[@]}" |
| 149 | + echo "EOF" |
| 150 | + echo "cache-from<<EOF" |
| 151 | + printf '%s\n' "${CACHE_FROM[@]}" |
| 152 | + echo "EOF" |
| 153 | + echo "cache-to=type=registry,ref=$REGISTRY/$IMAGE_NAME:$CACHE_KEY-buildcache,mode=max" |
| 154 | + } >> "$GITHUB_OUTPUT" |
| 155 | +
|
| 156 | + - name: Build and push |
| 157 | + uses: docker/build-push-action@v5 |
| 158 | + with: |
| 159 | + file: ${{ inputs.dockerfile }} |
| 160 | + push: true |
| 161 | + context: . |
| 162 | + platforms: ${{ inputs.platform }} |
| 163 | + build-contexts: ${{ inputs.build-contexts }} |
| 164 | + build-args: ${{ inputs.build-args }} |
| 165 | + cache-from: | |
| 166 | + ${{ steps.build_meta.outputs.cache-from }} |
| 167 | + ${{ steps.recent_pr_cache_refs.outputs.cache-from }} |
| 168 | + cache-to: ${{ steps.build_meta.outputs.cache-to }} |
| 169 | + no-cache: false |
| 170 | + tags: | |
| 171 | + ${{ steps.build_meta.outputs.tags }} |
| 172 | + target: ${{ inputs.target }} |
0 commit comments