conan-recipe-version.yml 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. name: Get Conan Recipe Version
  2. on:
  3. workflow_call:
  4. inputs:
  5. project_name:
  6. required: true
  7. type: string
  8. additional_buildmetadata:
  9. required: false
  10. default: ""
  11. type: string
  12. is_external_pr:
  13. required: false
  14. default: false
  15. type: boolean
  16. outputs:
  17. recipe_id_full:
  18. description: "The full Conan recipe id: <name>/<version>@<user>/<channel>"
  19. value: ${{ jobs.get-semver.outputs.recipe_id_full }}
  20. recipe_id_latest:
  21. description: "The full Conan recipe aliased (latest) id: <name>/(latest)@<user>/<channel>"
  22. value: ${{ jobs.get-semver.outputs.recipe_id_latest }}
  23. recipe_semver_full:
  24. description: "The full semver <Major>.<Minor>.<Patch>-<PreReleaseTag>+<BuildMetaData>"
  25. value: ${{ jobs.get-semver.outputs.semver_full }}
  26. is_release_branch:
  27. description: "is current branch a release branch?"
  28. value: ${{ jobs.get-semver.outputs.release_branch }}
  29. recipe_user:
  30. description: "The conan user"
  31. value: ${{ jobs.get-semver.outputs.user }}
  32. recipe_channel:
  33. description: "The conan channel"
  34. value: ${{ jobs.get-semver.outputs.channel }}
  35. jobs:
  36. get-semver:
  37. runs-on: ubuntu-latest
  38. outputs:
  39. recipe_id_full: ${{ steps.get-conan-broadcast-data.outputs.recipe_id_full }}
  40. recipe_id_latest: ${{ steps.get-conan-broadcast-data.outputs.recipe_id_latest }}
  41. semver_full: ${{ steps.get-conan-broadcast-data.outputs.semver_full }}
  42. is_release_branch: ${{ steps.get-conan-broadcast-data.outputs.is_release_branch }}
  43. user: ${{ steps.get-conan-broadcast-data.outputs.user }}
  44. channel: ${{ steps.get-conan-broadcast-data.outputs.channel }}
  45. steps:
  46. - name: Checkout repo
  47. uses: actions/checkout@v3
  48. if: ${{ ! is_external_pr }}
  49. with:
  50. fetch-depth: 0
  51. ref: ${{ github.head_ref }}
  52. - name: Checkout repo PR
  53. uses: actions/checkout@v3
  54. if: ${{ is_external_pr }}
  55. with:
  56. fetch-depth: 0
  57. ref: ${{ github.event.pull_request.head.ref }}
  58. repository: ${{ github.event.pull_request.head.repo.full_name }}
  59. - name: Setup Python and pip
  60. uses: actions/setup-python@v4
  61. with:
  62. python-version: "3.10.x"
  63. cache: 'pip'
  64. cache-dependency-path: .github/workflows/requirements-conan-package.txt
  65. - name: Install Python requirements and Create default Conan profile
  66. run: |
  67. pip install -r .github/workflows/requirements-conan-package.txt
  68. pip install gitpython
  69. - id: get-conan-broadcast-data
  70. name: Get Conan broadcast data
  71. run: |
  72. import subprocess
  73. from conans import tools
  74. from conans.errors import ConanException
  75. from git import Repo
  76. repo = Repo('.')
  77. user = "${{ github.repository_owner }}".lower()
  78. project_name = "${{ inputs.project_name }}"
  79. event_name = "${{ github.event_name }}"
  80. issue_number = "${{ github.ref }}".split('/')[2]
  81. is_tag = "${{ github.ref_type }}" == "tag"
  82. is_release_branch = False
  83. buildmetadata = "" if "${{ inputs.additional_buildmetadata }}" == "" else "${{ inputs.additional_buildmetadata }}_"
  84. # FIXME: for when we push a tag (such as an release)
  85. channel = "testing"
  86. if is_tag:
  87. branch_version = tools.Version("${{ github.ref_name }}")
  88. is_release_branch = True
  89. channel = "_"
  90. user = "_"
  91. else:
  92. try:
  93. branch_version = tools.Version(repo.active_branch.name)
  94. except ConanException:
  95. branch_version = tools.Version('0.0.0')
  96. if "${{ github.ref_name }}" == f"{branch_version.major}.{branch_version.minor}":
  97. channel = 'stable'
  98. is_release_branch = True
  99. elif "${{ github.ref_name }}" in ("main", "master"):
  100. channel = 'testing'
  101. else:
  102. channel = repo.active_branch.name.split("_")[0].replace("-", "_").lower()
  103. if "pull_request" in event_name:
  104. channel = f"pr_{issue_number}"
  105. # %% Get the actual version
  106. latest_branch_version = tools.Version("0.0.0")
  107. latest_branch_tag = None
  108. for tag in repo.git.tag(merged = True).splitlines():
  109. try:
  110. version = tools.Version(tag)
  111. except ConanException:
  112. continue
  113. if version > latest_branch_version:
  114. latest_branch_version = version
  115. latest_branch_tag = repo.tag(tag)
  116. # %% Get the actual version
  117. no_commits = 0
  118. for commit in repo.iter_commits("HEAD"):
  119. if commit == latest_branch_tag.commit:
  120. break
  121. no_commits += 1
  122. if no_commits == 0:
  123. # This is a release
  124. actual_version = f"{latest_branch_version.major}.{latest_branch_version.minor}.{latest_branch_version.patch}"
  125. if channel == "stable":
  126. user = "_"
  127. channel = "_"
  128. else:
  129. if latest_branch_version.prerelease and not "." in latest_branch_version.prerelease:
  130. # The prerealese did not contain a version number, default it to 1
  131. latest_branch_version.prerelease += ".1"
  132. if event_name == "pull_request":
  133. actual_version = f"{latest_branch_version.major}.{latest_branch_version.minor}.{latest_branch_version.patch}-{latest_branch_version.prerelease.lower()}+{buildmetadata}pr_{issue_number}_{no_commits}"
  134. else:
  135. if channel in ("stable", "_", ""):
  136. channel_metadata = f"{no_commits}"
  137. else:
  138. channel_metadata = f"{channel}_{no_commits}"
  139. # FIXME: for when we create a new release branch
  140. if latest_branch_version.prerelease == "":
  141. bump_up_minor = int(latest_branch_version.minor) + 1
  142. actual_version = f"{latest_branch_version.major}.{bump_up_minor}.{latest_branch_version.patch}-alpha+{buildmetadata}{channel_metadata}"
  143. else:
  144. actual_version = f"{latest_branch_version.major}.{latest_branch_version.minor}.{latest_branch_version.patch}-{latest_branch_version.prerelease.lower()}+{buildmetadata}{channel_metadata}"
  145. # %% print to output
  146. cmd_name = ["echo", f"::set-output name=name::{project_name}"]
  147. subprocess.call(cmd_name)
  148. cmd_version = ["echo", f"::set-output name=version::{actual_version}"]
  149. subprocess.call(cmd_version)
  150. cmd_channel = ["echo", f"::set-output name=channel::{channel}"]
  151. subprocess.call(cmd_channel)
  152. cmd_id_full= ["echo", f"::set-output name=recipe_id_full::{project_name}/{actual_version}@{user}/{channel}"]
  153. subprocess.call(cmd_id_full)
  154. cmd_id_latest = ["echo", f"::set-output name=recipe_id_latest::{project_name}/latest@{user}/{channel}"]
  155. subprocess.call(cmd_id_latest)
  156. cmd_semver_full = ["echo", f"::set-output name=semver_full::{actual_version}"]
  157. subprocess.call(cmd_semver_full)
  158. cmd_is_release_branch = ["echo", f"::set-output name=is_release_branch::{str(is_release_branch).lower()}"]
  159. subprocess.call(cmd_is_release_branch)
  160. print("::group::Conan Recipe Information")
  161. print(f"name = {project_name}")
  162. print(f"version = {actual_version}")
  163. print(f"user = {user}")
  164. print(f"channel = {channel}")
  165. print(f"recipe_id_full = {project_name}/{actual_version}@{user}/{channel}")
  166. print(f"recipe_id_latest = {project_name}/latest@{user}/{channel}")
  167. print(f"semver_full = {actual_version}")
  168. print(f"is_release_branch = {str(is_release_branch).lower()}")
  169. print("::endgroup::")
  170. shell: python