conan-recipe-version.yml 8.6 KB

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