conan-recipe-version.yml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. else:
  73. try:
  74. branch_version = tools.Version(repo.active_branch.name)
  75. except ConanException:
  76. branch_version = tools.Version('0.0.0')
  77. if "${{ github.ref_name }}" == f"{branch_version.major}.{branch_version.minor}":
  78. channel = 'stable'
  79. is_release_branch = True
  80. elif "${{ github.ref_name }}" in ("main", "master"):
  81. channel = 'testing'
  82. else:
  83. channel = repo.active_branch.name.split("_")[0].replace("-", "_").lower()
  84. if event_name == "pull_request":
  85. channel = f"pr_{issue_number}"
  86. # %% Get the actual version
  87. latest_branch_version = tools.Version("0.0.0")
  88. latest_branch_tag = None
  89. for tag in repo.git.tag(merged = True).splitlines():
  90. try:
  91. version = tools.Version(tag)
  92. except ConanException:
  93. continue
  94. if version > latest_branch_version:
  95. latest_branch_version = version
  96. latest_branch_tag = repo.tag(tag)
  97. # %% Get the actual version
  98. no_commits = 0
  99. for commit in repo.iter_commits("HEAD"):
  100. if commit == latest_branch_tag.commit:
  101. break
  102. no_commits += 1
  103. if no_commits == 0:
  104. # This is a release
  105. actual_version = f"{latest_branch_version.major}.{latest_branch_version.minor}.{latest_branch_version.patch}"
  106. if channel == "stable":
  107. user = "_"
  108. channel = "_"
  109. else:
  110. if event_name == "pull_request":
  111. 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}"
  112. else:
  113. if channel in ("stable", "_", ""):
  114. channel_metadata = f"{no_commits}"
  115. else:
  116. channel_metadata = f"{channel}_{no_commits}"
  117. # FIXME: for when we create a new release branch
  118. if latest_branch_version.prerelease == "":
  119. actual_version = f"{latest_branch_version.major}.{latest_branch_version.minor}.{latest_branch_version.patch}-alpha+{channel_metadata}"
  120. else:
  121. actual_version = f"{latest_branch_version.major}.{latest_branch_version.minor}.{latest_branch_version.patch}-{latest_branch_version.prerelease.lower()}+{channel_metadata}"
  122. # %% print to output
  123. cmd_name = ["echo", f"::set-output name=name::{project_name}"]
  124. subprocess.call(cmd_name)
  125. cmd_version = ["echo", f"::set-output name=version::{actual_version}"]
  126. subprocess.call(cmd_version)
  127. cmd_channel = ["echo", f"::set-output name=channel::{channel}"]
  128. subprocess.call(cmd_channel)
  129. cmd_id_full= ["echo", f"::set-output name=recipe_id_full::{project_name}/{actual_version}@{user}/{channel}"]
  130. subprocess.call(cmd_id_full)
  131. cmd_id_latest = ["echo", f"::set-output name=recipe_id_latest::{project_name}/latest@{user}/{channel}"]
  132. subprocess.call(cmd_id_latest)
  133. cmd_semver_full = ["echo", f"::set-output name=semver_full::{actual_version}"]
  134. subprocess.call(cmd_semver_full)
  135. cmd_is_release_branch = ["echo", f"::set-output name=is_release_branch::{str(is_release_branch).lower()}"]
  136. subprocess.call(cmd_is_release_branch)
  137. print("::group::Conan Recipe Information")
  138. print(f"name = {project_name}")
  139. print(f"version = {actual_version}")
  140. print(f"user = {user}")
  141. print(f"channel = {channel}")
  142. print(f"recipe_id_full = {project_name}/{actual_version}@{user}/{channel}")
  143. print(f"recipe_id_latest = {project_name}/latest@{user}/{channel}")
  144. print(f"semver_full = {actual_version}")
  145. print(f"is_release_branch = {str(is_release_branch).lower()}")
  146. print("::endgroup::")
  147. shell: python