4 # Copyright (c) 2020 Project CHIP Authors
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
19 # build.sh - utility for building (and optionally) tagging and pushing
22 # This script expects to find a Dockerfile next to $0, so symlink
23 # in an image name directory is the expected use case.
28 ORG=${DOCKER_BUILD_ORG:-connectedhomeip}
31 IMAGE=${DOCKER_BUILD_IMAGE:-$(basename "$(pwd)")}
34 VERSION=${DOCKER_BUILD_VERSION:-$(cat version)}
36 [[ ${*/--help//} != "${*}" ]] && {
38 echo "Usage: $me <OPTIONS>
40 Build and (optionally tag as latest, push) a docker image from Dockerfile in CWD
43 --try-download try to download latest image from dockerhub and skip whole
44 build procedure if the expected image version is downloaded.
45 --no-cache passed as a docker build argument
46 --latest update latest to the current built (or downloaded) version (\"$VERSION\")
47 --push push image(s) to docker.io (requires docker login for \"$ORG\")
48 --help get this message
55 echo "$me: *** ERROR: $*"
61 [[ -n $VERSION ]] || die "version cannot be empty"
63 if [[ ${*/--try-download//} != "${*}" ]]; then
64 docker pull "$ORG"/"$IMAGE":"$VERSION"
65 if [[ $? -eq 0 ]]; then
66 # tag it as latest for this version, note: this should only be used on CI
67 [[ ${*/--latest//} != "${*}" ]] && {
68 docker tag "$ORG"/"$IMAGE":"$VERSION" "$ORG"/"$IMAGE":latest
74 # go find and build any CHIP images this image is "FROM"
75 awk -F/ '/^FROM connectedhomeip/ {print $2}' Dockerfile | while read -r dep; do
77 (cd "../$dep" && ./build.sh "$@")
81 if [[ ${*/--no-cache//} != "${*}" ]]; then
82 BUILD_ARGS+=(--no-cache)
85 SOURCE=${BASH_SOURCE[0]}
86 SOURCE_DIR=$(cd "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
87 REPO_DIR="$SOURCE_DIR/../../../../"
89 # The image build will clone its own ot-br-posix checkout due to limitations of git submodule.
90 # Using the same ot-br-posix version as chip
91 OT_BR_POSIX=$REPO_DIR/third_party/ot-br-posix/repo
92 OT_BR_POSIX_CHECKOUT=$(cd "$REPO_DIR" && git rev-parse :third_party/ot-br-posix/repo)
94 docker build -t "$ORG/$IMAGE:$VERSION" -f "$SOURCE_DIR/Dockerfile" "${BUILD_ARGS[@]}" --build-arg OT_BR_POSIX_CHECKOUT="$OT_BR_POSIX_CHECKOUT" "$SOURCE_DIR"
96 [[ ${*/--latest//} != "${*}" ]] && {
97 docker tag "$ORG"/"$IMAGE":"$VERSION" "$ORG"/"$IMAGE":latest
100 [[ ${*/--push//} != "${*}" ]] && {
101 docker push "$ORG"/"$IMAGE":"$VERSION"
102 [[ ${*/--latest//} != "${*}" ]] && {
103 docker push "$ORG"/"$IMAGE":latest