您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

build.sh 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. ################################################################################
  3. # For this script to work properly it is required to define some environment variables
  4. # in the CI/CD Env variable declaration, while others should be passed as parameters.
  5. #
  6. #------------------------------------------------------------------------------
  7. # Defined as part of the CD/CI Env Variables:
  8. #
  9. # CD_DOCKER_USERNAME
  10. # A DockerHub username to be used for uploading the build.
  11. #
  12. # CD_DOCKER_PASSWORD
  13. # A DockerHub password to be used for uploading the build.
  14. #
  15. # CD_DOCKER_REPO
  16. # A DockerHub repository. By default the CD_REF_SLUG is also used as the docker repo.
  17. #
  18. # CD_BUILD_ALL
  19. # As the build is supposed to be done only for master (for a nightly deployments) and for releases
  20. # (like 'release-2.0.5' for production deployments), it is additionally required to include this
  21. # variable in order to build any other brnach, as it may be required for testing or reviewing work
  22. # as part of the development process.
  23. #
  24. display_usage() {
  25. echo "This script should be used as part of a CI strategy."
  26. echo -e "Usage:\n build_image.sh [ARGUMENTS]"
  27. echo -e "\nMandatory arguments \n"
  28. echo -e " repo_slug The git repository (e.g. bigbluebutton/greenlight)"
  29. echo -e "\nOptional arguments \n"
  30. echo -e " branch | tag The branch (e.g. master | release-2.0.5)"
  31. }
  32. # if less than two arguments supplied, display usage
  33. if [ $# -le 0 ]; then
  34. display_usage
  35. exit 1
  36. fi
  37. # check whether user had supplied -h or --help . If yes display usage
  38. if [[ ($# == "--help") || $# == "-h" ]]; then
  39. display_usage
  40. exit 0
  41. fi
  42. export CD_REF_SLUG=$1
  43. export CD_REF_NAME=$2
  44. if [ -z $CD_REF_NAME ]; then
  45. export CD_REF_NAME=$(git branch | grep \* | cut -d ' ' -f2)
  46. fi
  47. if [ "$CD_REF_NAME" != "master" ] && [[ "$CD_REF_NAME" != *"release"* ]] && ( [ -z "$CD_BUILD_ALL" ] || [ "$CD_BUILD_ALL" != "true" ] ); then
  48. echo "#### Docker image for $CD_REF_SLUG:$CD_REF_NAME won't be built"
  49. exit 0
  50. fi
  51. # Include sqlite for production
  52. sqliteCount="$(grep "gem 'sqlite3'" Gemfile | wc -l)"
  53. if [ $sqliteCount -lt 2 ]; then
  54. sed -i "/^group :production do/a\ \ gem 'sqlite3', '~> 1.3.6'" Gemfile
  55. fi
  56. # Set the version tag when it is a release or the commit sha was included.
  57. if [[ "$CD_REF_NAME" == *"release"* ]]; then
  58. export CD_BUILD_NUMBER=${CD_REF_NAME:8}
  59. else
  60. export CD_BUILD_NUMBER="$CD_REF_NAME ($(expr substr $(git rev-parse HEAD) 1 7))"
  61. fi
  62. # Build the image
  63. if [ -z $CD_DOCKER_REPO ]; then
  64. export CD_DOCKER_REPO=$CD_REF_SLUG
  65. fi
  66. echo "#### Docker image $CD_DOCKER_REPO:$CD_REF_NAME is being built"
  67. docker build --build-arg build_number="${CD_BUILD_NUMBER}" -t $CD_DOCKER_REPO:$CD_REF_NAME .
  68. if [ -z "$CD_DOCKER_USERNAME" ] || [ -z "$CD_DOCKER_PASSWORD" ]; then
  69. echo "#### Docker image for $CD_DOCKER_REPO can't be published because CD_DOCKER_USERNAME or CD_DOCKER_PASSWORD are missing (Ignore this warning if running outside a CD/CI environment)"
  70. exit 0
  71. fi
  72. # Publish the image
  73. docker login -u="$CD_DOCKER_USERNAME" -p="$CD_DOCKER_PASSWORD"
  74. echo "#### Docker image $CD_DOCKER_REPO:$CD_REF_NAME is being published"
  75. docker push $CD_DOCKER_REPO
  76. # Publish image as latest and v2 if it is a release (excluding alpha and beta)
  77. if [[ "$CD_REF_NAME" == *"release"* ]] && [[ "$CD_REF_NAME" != *"alpha"* ]] && [[ "$CD_REF_NAME" != *"beta"* ]]; then
  78. docker_image_id=$(docker images | grep -E "^$CD_DOCKER_REPO.*$CD_REF_NAME" | awk -e '{print $3}')
  79. docker tag $docker_image_id $CD_DOCKER_REPO:latest
  80. docker push $CD_DOCKER_REPO:latest
  81. docker tag $docker_image_id $CD_DOCKER_REPO:v2
  82. docker push $CD_DOCKER_REPO:v2
  83. fi
  84. exit 0