Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

build.sh 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. blindsidenetworks/scalelite)"
  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. # Set the version tag when it is a release or the commit sha was included.
  52. if [[ "$CD_REF_NAME" == *"v"* ]]; then
  53. export CD_BUILD_NUMBER=${CD_REF_NAME:1}
  54. else
  55. export CD_BUILD_NUMBER="$CD_REF_NAME ($(eval git rev-parse --short=7 HEAD))"
  56. fi
  57. # Build the image
  58. if [ -z $CD_DOCKER_REPO ]; then
  59. export CD_DOCKER_REPO=$CD_REF_SLUG
  60. fi
  61. echo "#### Docker image $CD_DOCKER_REPO:$CD_REF_NAME is being built"
  62. docker build --build-arg BUILD_NUMBER="${CD_BUILD_NUMBER}" -t $CD_DOCKER_REPO:$CD_REF_NAME .
  63. if [ -z "$CD_DOCKER_USERNAME" ] || [ -z "$CD_DOCKER_PASSWORD" ]; then
  64. 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)"
  65. exit 0
  66. fi
  67. # Publish the image
  68. docker login -u="$CD_DOCKER_USERNAME" -p="$CD_DOCKER_PASSWORD"
  69. echo "#### Docker image $CD_DOCKER_REPO:$CD_REF_NAME is being published"
  70. docker push $CD_DOCKER_REPO
  71. # Publish image as latest and v2 if it is a release (excluding alpha and beta)
  72. if [[ "$CD_REF_NAME" == *"v"* ]] && [[ "$CD_REF_NAME" != *"alpha"* ]] && [[ "$CD_REF_NAME" != *"beta"* ]]; then
  73. docker_image_id=$(docker images | grep -E "^$CD_DOCKER_REPO.*$CD_REF_NAME" | awk -e '{print $3}')
  74. docker tag $docker_image_id $CD_DOCKER_REPO:latest
  75. docker push $CD_DOCKER_REPO:latest
  76. docker tag $docker_image_id $CD_DOCKER_REPO:v2
  77. docker push $CD_DOCKER_REPO:v2
  78. fi
  79. exit 0