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

trigger_copr 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. | grep -v '[^0-9a-z.]' \
  40. | sort -V \
  41. | tail -1 \
  42. | sed "s/^v//"
  43. }
  44. mkspec() {
  45. local self_version
  46. local cl_date
  47. self_version="slop-build-copr $(git describe --tags)"
  48. cl_date=$(LC_ALL=C date +"%a %b %d %Y")
  49. sed -e "
  50. s|__APP_VERSION__|$Version|
  51. s|__APP_RELEASE__|$Release|
  52. s|__APP_URLBASE__|$UrlBase|
  53. s|__APP_BUILDSCRIPT_VERSION__|$self_version|
  54. s|__APP_DATE__|$cl_date|
  55. " <slop.spec.in
  56. }
  57. git_guess() {
  58. #
  59. # Print git-guessed $1
  60. #
  61. local what=$1 # what we want (ver|rel)
  62. local describe # full git-describe output
  63. local xtra= # extra part (-N-gHASH)
  64. local num=0 # num. of commits since tag (N)
  65. local sha= # '.g'+sha1 of this commit (gHASH)
  66. describe=$(git describe --tags --always HEAD)
  67. case $describe in
  68. *-*) # v1.2.3-21-g654cba
  69. tag=${describe%%-*} # tag=v1.2.3
  70. xtra=${describe#$tag-} # xtra=-21-g654cba
  71. num=${xtra%%-*} # num=21
  72. sha=.${xtra#$num-} # sha=.g654cba
  73. ;;
  74. *)
  75. tag=$describe
  76. ;;
  77. esac
  78. case $what in
  79. ver) echo "${tag#v}" ;;
  80. rel) echo "$((num + 1))$sha" ;;
  81. esac
  82. }
  83. choose_copr() {
  84. #
  85. # Choose COPR project based on $Mode
  86. #
  87. case $Mode in
  88. scratch) echo amahdal/slop-scratch ;;
  89. main) echo amahdal/slop ;;
  90. esac
  91. }
  92. choose_urlbase() {
  93. #
  94. # Choose COPR project based on $Mode
  95. #
  96. case $Mode in
  97. scratch) echo https://github.com/AloisMahdal/slop ;;
  98. main) echo https://github.com/naelstrof/slop ;;
  99. esac
  100. }
  101. choose_relpfx() {
  102. #
  103. # Choose COPR project based on $Mode
  104. #
  105. test "$Mode" == scratch && echo 0.scratch.
  106. return 0
  107. }
  108. do_action() {
  109. local url
  110. case $Action in
  111. build)
  112. copr build "$CoprProject" "$Tmp/slop.spec"
  113. ;;
  114. demo)
  115. warn "demo mode active, we would build:"
  116. warn " at $CoprProject"
  117. test -n "$Branch" && warn " using branch $Branch"
  118. test -n "$Branch" || warn " using last tag"
  119. warn " from $UrlBase"
  120. warn " yielding slop-$Version-$Release.*.rpm"
  121. warn ""
  122. warn "===== BEGIN slop.spec ====="
  123. cat "$Tmp/slop.spec"
  124. warn "===== END slop.spec ====="
  125. return 1
  126. ;;
  127. mkspec)
  128. cat "$Tmp/slop.spec"
  129. ;;
  130. rpmstuff)
  131. url=$(
  132. grep -o 'Source.*:.*http.*' "$Tmp/slop.spec" \
  133. | sed "
  134. s/.*http/http/
  135. s/ *$//
  136. s/%{version}/$Version/
  137. "
  138. )
  139. wget --quiet "$url"
  140. cat "$Tmp/slop.spec" > slop.spec
  141. ;;
  142. esac
  143. }
  144. main() {
  145. local Version # Version in SPEC file
  146. local Release # Release in SPEC file
  147. local CoprProject # COPR project
  148. local UrlBase # GitHub URL base
  149. local Tmp # our temp
  150. local Branch # branch to use, if empty, tags are considered
  151. local Mode=scratch # implies COPR project and release prefix
  152. local Action=build # what to do
  153. local es=0 # exit status
  154. which copr >/dev/null \
  155. || die "copr not found, try 'sudo install copr-cli'"
  156. Tmp=$(mktemp -d)
  157. while true; do case $1 in
  158. -a) Action=$2; shift 2 || usage ;;
  159. -b) Branch=$2; shift 2 || usage ;;
  160. -c) CoprProject=$2; shift 2 || usage ;;
  161. -u) UrlBase=$2; shift 2 || usage ;;
  162. -r) Release=$2; shift 2 || usage ;;
  163. -v) Version=${2#v}; shift 2 || usage ;;
  164. -n) Action=demo; shift ;;
  165. -*) usage ;;
  166. *) break ;;
  167. esac done
  168. Mode=${1:-$Mode}
  169. case $Action in
  170. build|demo|mkspec|rpmstuff) : ;;
  171. *) usage ;;
  172. esac
  173. case $Mode in
  174. main|scratch) : ;;
  175. *) usage ;;
  176. esac
  177. test -n "$CoprProject" || CoprProject=$(choose_copr)
  178. test -n "$UrlBase" || UrlBase=$(choose_urlbase)
  179. if test -n "$Branch"; then
  180. die "not implemented"
  181. test -n "$Version" || Version=$(git_guess ver)
  182. test -n "$Release" || Release=$(git_guess rel)
  183. else
  184. test -n "$Version" || Version=$(last_version)
  185. test -n "$Release" || Release=1
  186. fi
  187. Release=$(choose_relpfx)$Release
  188. mkspec >"$Tmp/slop.spec"
  189. do_action
  190. rm -rf "$Tmp"
  191. return $es
  192. }
  193. main "$@"