conan-recipe-version.yml 9.1 KB

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