Script to trigger re-build of slop - https://github.com/naelstrof/slop

trigger_copr 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/bash
  2. warn() {
  3. echo "$1" >&2
  4. }
  5. die() {
  6. warn "fatal: $1"
  7. exit 3
  8. }
  9. usage() {
  10. warn "usage: $0 [options] MODE"
  11. warn ""
  12. warn "Options:"
  13. warn ""
  14. warn " -b BRN build from branch BRN (default: last tag)"
  15. warn " -c COPR_PROJECT COPR project name"
  16. warn " -n dry mode, don't do anything (just show"
  17. warn " what would be done)"
  18. warn " -r REL use REL as Release number in SPEC file"
  19. warn " -v VER use VER as Version number in SPEC file"
  20. warn " -u URL use URL as base (to get last tag and"
  21. warn " compose Source0 in SPEC file)"
  22. warn ""
  23. warn "If -b is not used, build is launched from last tag available"
  24. warn "in the GitHub repo. In that case, Release is '1' and Version"
  25. warn "is deduced from the tag by removing the initial 'v'."
  26. warn ""
  27. warn "If -b is used, the project repo is temporarily cloned, and"
  28. warn "both Version and Release are found by consulting git-describe"
  29. warn "on the specified branch."
  30. warn ""
  31. warn "If MODE is 'scratch' (default), Release is pre-fixed by string"
  32. warn "'0.scratch.' and build is triggered in scratch COPR project."
  33. exit 2
  34. }
  35. last_version() {
  36. git ls-remote --tag "$UrlBase" \
  37. | grep '/tags/' \
  38. | cut -d/ -f3 \
  39. | sort -V \
  40. | tail -1 \
  41. | sed "s/^v//"
  42. }
  43. mkspec() {
  44. local self_version
  45. local cl_date
  46. self_version="slop-build-copr $(git describe --tags)"
  47. cl_date=$(LC_ALL=C date +"%a %b %d %Y")
  48. sed -e "
  49. s|__SLOP_VERSION__|$Version|
  50. s|__SLOP_RELEASE__|$Release|
  51. s|__SLOP_URLBASE__|$UrlBase|
  52. s|__SLOP_BUILDSCRIPT_VERSION__|$self_version|
  53. s|__SLOP_DATE__|$cl_date|
  54. " <slop.spec.in
  55. }
  56. git_guess() {
  57. #
  58. # Print git-guessed $1
  59. #
  60. local what=$1 # what we want (ver|rel)
  61. local describe # full git-describe output
  62. local xtra= # extra part (-N-gHASH)
  63. local num=0 # num. of commits since tag (N)
  64. local sha= # '.g'+sha1 of this commit (gHASH)
  65. describe=$(git describe --tags --always HEAD)
  66. case $describe in
  67. *-*) # v1.2.3-21-g654cba
  68. tag=${describe%%-*} # tag=v1.2.3
  69. xtra=${describe#$tag-} # xtra=-21-g654cba
  70. num=${xtra%%-*} # num=21
  71. sha=.${xtra#$num-} # sha=.g654cba
  72. ;;
  73. *)
  74. tag=$describe
  75. ;;
  76. esac
  77. case $what in
  78. ver) echo "${tag#v}" ;;
  79. rel) echo "$((num + 1))$sha" ;;
  80. esac
  81. }
  82. choose_copr() {
  83. #
  84. # Choose COPR project based on $Mode
  85. #
  86. case $Mode in
  87. scratch) echo amahdal/slop-scratch ;;
  88. main) echo amahdal/slop ;;
  89. esac
  90. }
  91. choose_urlbase() {
  92. #
  93. # Choose COPR project based on $Mode
  94. #
  95. case $Mode in
  96. scratch) echo https://github.com/AloisMahdal/slop ;;
  97. main) echo https://github.com/naelstrof/slop ;;
  98. esac
  99. }
  100. choose_relpfx() {
  101. #
  102. # Choose COPR project based on $Mode
  103. #
  104. test "$Mode" == scratch && echo 0.scratch.
  105. return 0
  106. }
  107. main() {
  108. local Version # Version in SPEC file
  109. local Release # Release in SPEC file
  110. local CoprProject # COPR project
  111. local UrlBase # GitHub URL base
  112. local Tmp # our temp
  113. local Branch # branch to use, if empty, tags are considered
  114. local DryRun=false # do not do anything
  115. local Mode=scratch # implies COPR project and release prefix
  116. which copr >/dev/null \
  117. || die "copr not found, try 'sudo install copr-cli'"
  118. Tmp=$(mktemp -d)
  119. while true; do case $1 in
  120. -b) Branch=$2; shift 2 || usage ;;
  121. -c) CoprProject=$2; shift 2 || usage ;;
  122. -u) UrlBase=$2; shift 2 || usage ;;
  123. -r) Release=$2; shift 2 || usage ;;
  124. -v) Version=${2#v}; shift 2 || usage ;;
  125. -n) DryRun=true; shift ;;
  126. -*) usage ;;
  127. *) break ;;
  128. esac done
  129. Mode=${1:-$Mode}
  130. case $Mode in
  131. main|scratch) : ;;
  132. *) usage ;;
  133. esac
  134. test -n "$CoprProject" || CoprProject=$(choose_copr)
  135. test -n "$UrlBase" || UrlBase=$(choose_urlbase)
  136. if test -n "$Branch"; then
  137. die "not implemented"
  138. test -n "$Version" || Version=$(git_guess ver)
  139. test -n "$Release" || Release=$(git_guess rel)
  140. else
  141. test -n "$Version" || Version=$(last_version)
  142. test -n "$Release" || Release=1
  143. fi
  144. Release=$(choose_relpfx)$Release
  145. mkspec >"$Tmp/slop.spec"
  146. $DryRun && {
  147. warn "dry mode active, we would build:"
  148. warn " at $CoprProject"
  149. test -n "$Branch" && warn " using branch $Branch"
  150. test -n "$Branch" || warn " using last tag"
  151. warn " from $UrlBase"
  152. warn " yielding slop-$Version-$Release.*.rpm"
  153. warn ""
  154. warn "===== BEGIN slop.spec ====="
  155. cat "$Tmp/slop.spec"
  156. warn "===== END slop.spec ====="
  157. exit 1
  158. }
  159. copr build "$CoprProject" "$Tmp/slop.spec"
  160. rm -rf "$Tmp"
  161. }
  162. main "$@"