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

trigger_copr 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_relpfx() {
  92. #
  93. # Choose COPR project based on $Mode
  94. #
  95. test "$Mode" == scratch && echo 0.scratch.
  96. return 0
  97. }
  98. main() {
  99. local Version # Version in SPEC file
  100. local Release # Release in SPEC file
  101. local CoprProject # COPR project
  102. local UrlBase # GitHub URL base
  103. local Tmp # our temp
  104. local Branch # branch to use, if empty, tags are considered
  105. local DryRun=false # do not do anything
  106. local Mode=scratch # implies COPR project and release prefix
  107. which copr >/dev/null \
  108. || die "copr not found, try 'sudo install copr-cli'"
  109. UrlBase=https://github.com/AloisMahdal/slop
  110. Tmp=$(mktemp -d)
  111. while true; do case $1 in
  112. -b) Branch=$2; shift 2 || usage ;;
  113. -c) CoprProject=$2; shift 2 || usage ;;
  114. -u) UrlBase=$2; shift 2 || usage ;;
  115. -r) Release=$2; shift 2 || usage ;;
  116. -v) Version=${2#v}; shift 2 || usage ;;
  117. -n) DryRun=true; shift ;;
  118. -*) usage ;;
  119. *) break ;;
  120. esac done
  121. Mode=${1:-$Mode}
  122. case $Mode in
  123. main|scratch) : ;;
  124. *) usage ;;
  125. esac
  126. test -n "$CoprProject" || CoprProject=$(choose_copr)
  127. if test -n "$Branch"; then
  128. die "not implemented"
  129. test -n "$Version" || Version=$(git_guess ver)
  130. test -n "$Release" || Release=$(git_guess rel)
  131. else
  132. test -n "$Version" || Version=$(last_version)
  133. test -n "$Release" || Release=1
  134. fi
  135. Release=$(choose_relpfx)$Release
  136. mkspec >"$Tmp/slop.spec"
  137. $DryRun && {
  138. warn "dry mode active, we would build:"
  139. warn " at $CoprProject"
  140. test -n "$Branch" && warn " using branch $Branch"
  141. test -n "$Branch" || warn " using last tag"
  142. warn " from $UrlBase"
  143. warn " yielding slop-$Version-$Release.*.rpm"
  144. warn ""
  145. warn "===== BEGIN slop.spec ====="
  146. cat "$Tmp/slop.spec"
  147. warn "===== END slop.spec ====="
  148. exit 1
  149. }
  150. copr build "$CoprProject" "$Tmp/slop.spec"
  151. rm -rf "$Tmp"
  152. }
  153. main "$@"