conan-recipe-version.yml 11 KB

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