GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

image-tag 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env bash
  2. set -o errexit
  3. set -o nounset
  4. set -o pipefail
  5. OUTPUT=--quiet
  6. if [ "${1:-}" = '--show-diff' ]; then
  7. OUTPUT=
  8. fi
  9. # If a tagged version, just print that tag
  10. HEAD_TAGS=( $(git tag --points-at HEAD) )
  11. case ${#HEAD_TAGS[@]} in
  12. 0) ;;
  13. 1) echo ${HEAD_TAGS[0]}; exit 0
  14. ;;
  15. 2) # For releases we may have two tags, one with an v prefix (e.g. v1.17.0 and 1.17.0),
  16. # discard the v prefix
  17. TAG1=${HEAD_TAGS[0]}
  18. TAG2=${HEAD_TAGS[1]}
  19. if [[ "${TAG1}" == v* && ${TAG1%$TAG2} == "v" ]]; then
  20. echo ${TAG2}; exit 0
  21. fi
  22. if [[ "${TAG2}" == v* && ${TAG2%$TAG1} == "v" ]]; then
  23. echo ${TAG1}; exit 0
  24. fi
  25. echo "error: more than one tag pointing to HEAD" >&2; exit 1; ;;
  26. *) echo "error: more than one tag pointing to HEAD" >&2; exit 1; ;;
  27. esac
  28. WORKING_SUFFIX=$(if ! git diff --exit-code ${OUTPUT} HEAD >&2; \
  29. then echo "-wip"; \
  30. else echo ""; \
  31. fi)
  32. BRANCH_PREFIX=$(git rev-parse --abbrev-ref HEAD)
  33. # replace spaces with dash
  34. BRANCH_PREFIX=${BRANCH_PREFIX// /-}
  35. # next, replace slashes with dash
  36. BRANCH_PREFIX=${BRANCH_PREFIX//[\/\\]/-}
  37. # now, clean out anything that's not alphanumeric or an dash
  38. BRANCH_PREFIX=${BRANCH_PREFIX//[^a-zA-Z0-9-]/}
  39. # finally, lowercase with TR
  40. BRANCH_PREFIX=`echo -n $BRANCH_PREFIX | tr A-Z a-z`
  41. echo "$BRANCH_PREFIX-$(git rev-parse --short HEAD)$WORKING_SUFFIX"