build.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2020 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # build.sh - utility for building (and optionally) tagging and pushing
  18. # the a Docker image
  19. #
  20. # This script expects to find a Dockerfile next to $0, so symlink
  21. # in an image name directory is the expected use case.
  22. me=$(basename "$0")
  23. cd "$(dirname "$0")"
  24. GHCR_ORG="ghcr.io"
  25. ORG=${DOCKER_BUILD_ORG:-project-chip}
  26. # directory name is
  27. IMAGE=${DOCKER_BUILD_IMAGE:-$(basename "$(pwd)")}
  28. # version
  29. VERSION=${DOCKER_BUILD_VERSION:-$(sed 's/ .*//' version)}
  30. if [[ $OSTYPE == 'darwin'* ]]; then
  31. DOCKER_VOLUME_PATH=~/Library/Containers/com.docker.docker/Data/vms/0/
  32. TARGET_PLATFORM_TYPE="linux/arm64"
  33. else
  34. DOCKER_VOLUME_PATH=/var/lib/docker/
  35. TARGET_PLATFORM_TYPE="linux/amd64"
  36. fi
  37. [[ ${*/--help//} != "${*}" ]] && {
  38. set +x
  39. echo "Usage: $me <OPTIONS>
  40. Build and (optionally tag as latest, push) a docker image from Dockerfile in CWD
  41. Options:
  42. --no-cache passed as a docker build argument
  43. --latest update latest to the current built version (\"$VERSION\")
  44. --push push image(s) to docker.io (requires docker login for \"$ORG\")
  45. --skip-build skip the build/prune step
  46. --help get this message
  47. --squash squash docker layers before push them to docker.io (requires docker-squash python module)
  48. "
  49. exit 0
  50. }
  51. die() {
  52. echo "$me: *** ERROR: $*"
  53. exit 1
  54. }
  55. set -ex
  56. [[ -n $VERSION ]] || die "version cannot be empty"
  57. if [ -f "$DOCKER_VOLUME_PATH" ]; then
  58. mb_space_before=$(df -m "$DOCKER_VOLUME_PATH" | awk 'FNR==2{print $3}')
  59. fi
  60. # go find and build any CHIP images this image is "FROM"
  61. awk -F/ '/^FROM project-chip/ {print $2}' Dockerfile | while read -r dep; do
  62. dep=${dep%:*}
  63. (cd "../$dep" && ./build.sh "$@")
  64. done
  65. BUILD_ARGS=()
  66. if [[ ${*/--no-cache//} != "${*}" ]]; then
  67. BUILD_ARGS+=(--no-cache)
  68. fi
  69. [[ ${*/--skip-build//} != "${*}" ]] || {
  70. docker build "${BUILD_ARGS[@]}" --build-arg TARGETPLATFORM="$TARGET_PLATFORM_TYPE" --build-arg VERSION="$VERSION" -t "$GHCR_ORG/$ORG/$IMAGE:$VERSION" .
  71. docker image prune --force
  72. }
  73. [[ ${*/--latest//} != "${*}" ]] && {
  74. docker tag "$GHCR_ORG"/"$ORG"/"$IMAGE":"$VERSION" "$GHCR_ORG"/"$ORG"/"$IMAGE":latest
  75. }
  76. [[ ${*/--squash//} != "${*}" ]] && {
  77. command -v docker-squash >/dev/null &&
  78. docker-squash "$GHCR_ORG"/"$ORG"/"$IMAGE":"$VERSION" -t "$GHCR_ORG"/"$ORG"/"$IMAGE":latest
  79. }
  80. [[ ${*/--push//} != "${*}" ]] && {
  81. docker push "$GHCR_ORG"/"$ORG"/"$IMAGE":"$VERSION"
  82. [[ ${*/--latest//} != "${*}" ]] && {
  83. docker push "$GHCR_ORG"/"$ORG"/"$IMAGE":latest
  84. }
  85. }
  86. [[ ${*/--clear//} != "${*}" ]] && {
  87. docker rmi -f "$GHCR_ORG"/"$ORG"/"$IMAGE":"$VERSION"
  88. [[ ${*/--latest//} != "${*}" ]] && {
  89. docker rmi -f "$GHCR_ORG"/"$ORG"/"$IMAGE":latest
  90. }
  91. }
  92. docker images --filter=reference="$GHCR_ORG/$ORG/*"
  93. if [ -f "$DOCKER_VOLUME_PATH" ]; then
  94. df -h "$DOCKER_VOLUME_PATH"
  95. mb_space_after=$(df -m "$DOCKER_VOLUME_PATH" | awk 'FNR==2{print $3}')
  96. printf "%'.f MB total used\n" "$((mb_space_before - mb_space_after))"
  97. fi
  98. exit 0