Skip to content

Commit b04ceae

Browse files
committed
Extract version from version file.
1 parent 1e3e301 commit b04ceae

File tree

2 files changed

+103
-21
lines changed

2 files changed

+103
-21
lines changed

.github/workflows/CompletePipeline.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,58 @@ jobs:
177177
exclude_list: ${{ inputs.unittest_exclude_list }}
178178
disable_list: ${{ inputs.unittest_disable_list }}
179179

180+
VersionCheck:
181+
name: ''
182+
runs-on: 'ubuntu-24.04'
183+
needs:
184+
- Prepare
185+
- UnitTestingParams
186+
if: needs.Prepare.outputs.version != '' && needs.UnitTestingParams.outputs.package_version_file != ''
187+
outputs:
188+
code_version: ${{ steps.extract.outputs.code_version }}
189+
steps:
190+
- name: ⏬ Checkout repository
191+
uses: actions/checkout@v5
192+
with:
193+
# The command 'git describe' (used for version) needs the history.
194+
fetch-depth: 0
195+
196+
- name: 🔧 Install pyTooling dependencies (native)
197+
run: |
198+
python -m pip install --disable-pip-version-check -U pyTooling
199+
200+
- name: Extract version from Python source file
201+
id: extract
202+
if: endsWith(needs.UnitTestingParams.outputs.package_version_file, '.py')
203+
shell: python
204+
run: |
205+
from pathlib import Path
206+
from sys import exit
207+
from pyTooling.Packaging import extractVersionInformation
208+
209+
expectedVersion = "${{ needs.Prepare.outputs.version }}".strip()
210+
211+
versionFile = Path("${{ needs.UnitTestingParams.outputs.package_version_file }}")
212+
if not versionFile.exists():
213+
print(f"::error title=CompletePipeline::Version file '{versionFile}' not found.")
214+
exit(1)
215+
216+
versionInformation = extractVersionInformation(versionFile)
217+
print(f"expected: {expectedVersion}")
218+
print(f"from code: {versionInformation.Version}")
219+
220+
if expectedVersion != versionInformation.Version:
221+
print(f"::error title=CompletePipeline::Expected version does not version in Python code.")
222+
exit(2)
223+
224+
# Write jobs to special file
225+
github_output = Path(getenv("GITHUB_OUTPUT"))
226+
print(f"GITHUB_OUTPUT: {github_output}")
227+
with github_output.open("a+", encoding="utf-8") as f:
228+
f.write(dedent(f"""\
229+
code_version={versionInformation.Version}
230+
"""))
231+
180232
UnitTesting:
181233
uses: pyTooling/Actions/.github/workflows/UnitTesting.yml@dev
182234
needs:

.github/workflows/Parameters.yml

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ on:
4545
required: false
4646
default: ''
4747
type: string
48+
version_file:
49+
description: "Path to module containing the version ('__version__' variable)."
50+
required: false
51+
default: '__init__.py'
52+
type: string
4853
python_version:
4954
description: 'Python version.'
5055
required: false
@@ -121,6 +126,9 @@ on:
121126
package_directory:
122127
description: "The package's directory."
123128
value: ${{ jobs.Parameters.outputs.package_directory }}
129+
package_version_file:
130+
description: "Path to the package's module containing the version ('__version__' variable)."
131+
value: ${{ jobs.Parameters.outputs.package_version_file }}
124132
artifact_basename:
125133
description: "Artifact base name."
126134
value: ${{ jobs.Parameters.outputs.artifact_basename }}
@@ -136,14 +144,21 @@ jobs:
136144
name: ✎ Generate pipeline parameters
137145
runs-on: "ubuntu-${{ inputs.ubuntu_image_version }}"
138146
outputs:
139-
python_version: ${{ steps.variables.outputs.python_version }}
140-
package_fullname: ${{ steps.variables.outputs.package_fullname }}
141-
package_directory: ${{ steps.variables.outputs.package_directory }}
142-
artifact_basename: ${{ steps.variables.outputs.artifact_basename }}
143-
artifact_names: ${{ steps.artifacts.outputs.artifact_names }}
144-
python_jobs: ${{ steps.jobs.outputs.python_jobs }}
147+
python_version: ${{ steps.variables.outputs.python_version }}
148+
package_fullname: ${{ steps.variables.outputs.package_fullname }}
149+
package_directory: ${{ steps.variables.outputs.package_directory }}
150+
package_version_file: ${{ steps.variables.outputs.package_version_file }}
151+
artifact_basename: ${{ steps.variables.outputs.artifact_basename }}
152+
artifact_names: ${{ steps.artifacts.outputs.artifact_names }}
153+
python_jobs: ${{ steps.jobs.outputs.python_jobs }}
145154

146155
steps:
156+
- name: ⏬ Checkout repository
157+
uses: actions/checkout@v5
158+
with:
159+
# The command 'git describe' (used for version) needs the history.
160+
fetch-depth: 0
161+
147162
- name: Generate a startup delay of ${{ inputs.pipeline-delay }} seconds
148163
id: delay
149164
if: inputs.pipeline-delay >= 0
@@ -162,6 +177,7 @@ jobs:
162177
python_version = "${{ inputs.python_version }}".strip()
163178
package_namespace = "${{ inputs.package_namespace }}".strip()
164179
package_name = "${{ inputs.package_name }}".strip()
180+
version_file = "${{ inputs.version_file }}".strip()
165181
name = "${{ inputs.name }}".strip()
166182
167183
if package_namespace == "":
@@ -174,16 +190,28 @@ jobs:
174190
package_fullname = f"{package_namespace}.{package_name}"
175191
package_directory = f"{package_namespace}/{package_name}"
176192
193+
packageDirectory = Path(package_directory)
194+
packageVersionFile = packageDirectory / version_file
195+
print(f"Check if package version file '{packageVersionFile}' exists ... ", end="")
196+
if packageVersionFile.exists():
197+
print("✅")
198+
package_version_file = packageVersionFile.as_posix()
199+
else:
200+
print("❌")
201+
package_version_file = ""
202+
print(f"::warning title=Parameters::Version file '{packageVersionFile}' not found.")
203+
177204
artifact_basename = package_fullname if name == "" else name
178205
if artifact_basename == "" or artifact_basename == ".":
179-
print("::error title=Parameter::artifact_basename is empty.")
206+
print("::error title=Parameters::artifact_basename is empty.")
180207
exit(1)
181208
182209
print("Variables:")
183-
print(f" python_version: {python_version}")
184-
print(f" package_fullname: {package_fullname}")
185-
print(f" package_directory: {package_directory}")
186-
print(f" artifact_basename: {artifact_basename}")
210+
print(f" python_version: {python_version}")
211+
print(f" package_fullname: {package_fullname}")
212+
print(f" package_directory: {package_directory}")
213+
print(f" package_version_file: {package_directory}")
214+
print(f" artifact_basename: {artifact_basename}")
187215
188216
# Write jobs to special file
189217
github_output = Path(getenv("GITHUB_OUTPUT"))
@@ -193,6 +221,7 @@ jobs:
193221
python_version={python_version}
194222
package_fullname={package_fullname}
195223
package_directory={package_directory}
224+
package_version_file={package_version_file}
196225
artifact_basename={artifact_basename}
197226
"""))
198227
@@ -263,7 +292,7 @@ jobs:
263292
currentAlphaRelease = "3.15.0-a.1"
264293
265294
if systems == "":
266-
print("::error title=Parameter::system_list is empty.")
295+
print("::error title=Parameters::system_list is empty.")
267296
else:
268297
systems = [sys.strip() for sys in systems.split(" ")]
269298
@@ -428,12 +457,13 @@ jobs:
428457
- name: Verify out parameters
429458
id: verify
430459
run: |
431-
printf "python_version: %s\n" '${{ steps.variables.outputs.python_version }}'
432-
printf "package_fullname: %s\n" '${{ steps.variables.outputs.package_fullname }}'
433-
printf "package_directory: %s\n" '${{ steps.variables.outputs.package_directory }}'
434-
printf "artifact_basename: %s\n" '${{ steps.variables.outputs.artifact_basename }}'
435-
printf "====================\n"
436-
printf "artifact_names: %s\n" '${{ steps.artifacts.outputs.artifact_names }}'
437-
printf "====================\n"
438-
printf "python_jobs: %s\n" '${{ steps.jobs.outputs.python_jobs }}'
439-
printf "====================\n"
460+
printf "python_version: %s\n" '${{ steps.variables.outputs.python_version }}'
461+
printf "package_fullname: %s\n" '${{ steps.variables.outputs.package_fullname }}'
462+
printf "package_directory: %s\n" '${{ steps.variables.outputs.package_directory }}'
463+
printf "package_version_file: %s\n" '${{ steps.variables.outputs.package_version_file }}'
464+
printf "artifact_basename: %s\n" '${{ steps.variables.outputs.artifact_basename }}'
465+
printf "================================================================================\n"
466+
printf "artifact_names: %s\n" '${{ steps.artifacts.outputs.artifact_names }}'
467+
printf "================================================================================\n"
468+
printf "python_jobs: %s\n" '${{ steps.jobs.outputs.python_jobs }}'
469+
printf "================================================================================\n"

0 commit comments

Comments
 (0)