conan-recipe-version.yml 7.5 KB

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