build.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. set -eo pipefail
  3. image_name=$1
  4. org_name=$2
  5. push=0
  6. if [[ $3 == "--push" ]]; then
  7. push=1
  8. fi
  9. echo -e "Building: $image_name"
  10. tags=()
  11. OPEN_DEVIN_BUILD_VERSION="dev"
  12. cache_tag_base="buildcache"
  13. cache_tag="$cache_tag_base"
  14. if [[ -n $GITHUB_REF_NAME ]]; then
  15. # check if ref name is a version number
  16. if [[ $GITHUB_REF_NAME =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  17. major_version=$(echo $GITHUB_REF_NAME | cut -d. -f1)
  18. minor_version=$(echo $GITHUB_REF_NAME | cut -d. -f1,2)
  19. tags+=($major_version $minor_version)
  20. fi
  21. sanitized=$(echo $GITHUB_REF_NAME | sed 's/[^a-zA-Z0-9.-]\+/-/g')
  22. OPEN_DEVIN_BUILD_VERSION=$sanitized
  23. cache_tag+="-${sanitized}"
  24. tags+=($sanitized)
  25. fi
  26. echo "Tags: ${tags[@]}"
  27. dir=./containers/$image_name
  28. if [ ! -f $dir/Dockerfile ]; then
  29. echo "No Dockerfile found"
  30. exit 1
  31. fi
  32. if [ ! -f $dir/config.sh ]; then
  33. echo "No config.sh found for Dockerfile"
  34. exit 1
  35. fi
  36. source $dir/config.sh
  37. if [[ -n "$org_name" ]]; then
  38. DOCKER_ORG="$org_name"
  39. fi
  40. DOCKER_REPOSITORY=$DOCKER_REGISTRY/$DOCKER_ORG/$DOCKER_IMAGE
  41. DOCKER_REPOSITORY=${DOCKER_REPOSITORY,,} # lowercase
  42. echo "Repo: $DOCKER_REPOSITORY"
  43. echo "Base dir: $DOCKER_BASE_DIR"
  44. args=""
  45. for tag in ${tags[@]}; do
  46. args+=" -t $DOCKER_REPOSITORY:$tag"
  47. done
  48. if [[ $push -eq 1 ]]; then
  49. args+=" --push"
  50. args+=" --cache-to=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag,mode=max"
  51. fi
  52. docker buildx build \
  53. $args \
  54. --build-arg OPEN_DEVIN_BUILD_VERSION=$OPEN_DEVIN_BUILD_VERSION \
  55. --cache-from=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag \
  56. --cache-from=type=registry,ref=$DOCKER_REPOSITORY:$cache_tag_base-main \
  57. --platform linux/amd64,linux/arm64 \
  58. --provenance=false \
  59. -f $dir/Dockerfile $DOCKER_BASE_DIR