Skip to content

Commit 4b47e52

Browse files
authored
chore - Use blacksmith docker build with caching (#7771)
1 parent 0087082 commit 4b47e52

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

Diff for: .github/workflows/ghcr-build.yml

+23-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ env:
2929

3030
jobs:
3131
define-matrix:
32-
runs-on: blacksmith-4vcpu-ubuntu-2204
32+
runs-on: blacksmith
3333
outputs:
3434
base_image: ${{ steps.define-base-images.outputs.base_image }}
3535
steps:
@@ -151,14 +151,33 @@ jobs:
151151
- name: Lowercase Repository Owner
152152
run: |
153153
echo REPO_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
154-
- name: Build and push runtime image ${{ matrix.base_image.image }}
154+
- name: Short SHA
155+
run: |
156+
echo SHORT_SHA=$(git rev-parse --short "$RELEVANT_SHA") >> $GITHUB_ENV
157+
- name: Determine docker build params
155158
if: github.event.pull_request.head.repo.fork != true
159+
shell: bash
156160
run: |
157-
./containers/build.sh -i runtime -o ${{ env.REPO_OWNER }} --push -t ${{ matrix.base_image.tag }}
161+
./containers/build.sh -i runtime -o ${{ env.REPO_OWNER }} -t ${{ matrix.base_image.tag }} --dry
162+
163+
DOCKER_BUILD_JSON=$(jq -c . < docker-build-dry.json)
164+
echo "DOCKER_TAGS=$(echo "$DOCKER_BUILD_JSON" | jq -r '.tags | join(",")')" >> $GITHUB_ENV
165+
echo "DOCKER_PLATFORM=$(echo "$DOCKER_BUILD_JSON" | jq -r '.platform')" >> $GITHUB_ENV
166+
echo "DOCKER_BUILD_ARGS=$(echo "$DOCKER_BUILD_JSON" | jq -r '.build_args | join(",")')" >> $GITHUB_ENV
167+
- name: Build and push runtime image ${{ matrix.base_image.image }}
168+
if: github.event.pull_request.head.repo.fork != true
169+
uses: useblacksmith/build-push-action@v1
170+
with:
171+
push: true
172+
tags: ${{ env.DOCKER_TAGS }}
173+
platforms: ${{ env.DOCKER_PLATFORM }}
174+
build-args: ${{ env.DOCKER_BUILD_ARGS }}
175+
context: containers/runtime
176+
provenance: false
158177
# Forked repos can't push to GHCR, so we need to upload the image as an artifact
159178
- name: Build runtime image ${{ matrix.base_image.image }} for fork
160179
if: github.event.pull_request.head.repo.fork
161-
uses: docker/build-push-action@v6
180+
uses: useblacksmith/build-push-action@v1
162181
with:
163182
tags: ghcr.io/${{ env.REPO_OWNER }}/runtime:${{ env.RELEVANT_SHA }}-${{ matrix.base_image.tag }}
164183
outputs: type=docker,dest=/tmp/runtime-${{ matrix.base_image.tag }}.tar

Diff for: containers/build.sh

+27-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ org_name=""
77
push=0
88
load=0
99
tag_suffix=""
10+
dry_run=0
1011

1112
# Function to display usage information
1213
usage() {
13-
echo "Usage: $0 -i <image_name> [-o <org_name>] [--push] [--load] [-t <tag_suffix>]"
14+
echo "Usage: $0 -i <image_name> [-o <org_name>] [--push] [--load] [-t <tag_suffix>] [--dry]"
1415
echo " -i: Image name (required)"
1516
echo " -o: Organization name"
1617
echo " --push: Push the image"
1718
echo " --load: Load the image"
1819
echo " -t: Tag suffix"
20+
echo " --dry: Don't build, only create build-args.json"
1921
exit 1
2022
}
2123

@@ -27,6 +29,7 @@ while [[ $# -gt 0 ]]; do
2729
--push) push=1; shift ;;
2830
--load) load=1; shift ;;
2931
-t) tag_suffix="$2"; shift 2 ;;
32+
--dry) dry_run=1; shift ;;
3033
*) usage ;;
3134
esac
3235
done
@@ -113,10 +116,13 @@ echo "Repo: $DOCKER_REPOSITORY"
113116
echo "Base dir: $DOCKER_BASE_DIR"
114117

115118
args=""
119+
full_tags=()
116120
for tag in "${tags[@]}"; do
117121
args+=" -t $DOCKER_REPOSITORY:$tag"
122+
full_tags+=("$DOCKER_REPOSITORY:$tag")
118123
done
119124

125+
120126
if [[ $push -eq 1 ]]; then
121127
args+=" --push"
122128
args+=" --cache-to=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag,mode=max"
@@ -136,6 +142,26 @@ else
136142
# For push or without load, build for multiple platforms
137143
platform="linux/amd64,linux/arm64"
138144
fi
145+
if [[ $dry_run -eq 1 ]]; then
146+
echo "Dry Run is enabled. Writing build config to docker-build-dry.json"
147+
jq -n \
148+
--argjson tags "$(printf '%s\n' "${full_tags[@]}" | jq -R . | jq -s .)" \
149+
--arg platform "$platform" \
150+
--arg openhands_build_version "$OPENHANDS_BUILD_VERSION" \
151+
--arg dockerfile "$dir/Dockerfile" \
152+
'{
153+
tags: $tags,
154+
platform: $platform,
155+
build_args: [
156+
"OPENHANDS_BUILD_VERSION=" + $openhands_build_version
157+
],
158+
dockerfile: $dockerfile
159+
}' > docker-build-dry.json
160+
161+
exit 0
162+
fi
163+
164+
139165

140166
echo "Building for platform(s): $platform"
141167

0 commit comments

Comments
 (0)