How I can get the gitlab-ci environment variable VERSION value from the python script - get_version.py for a gitlab-runners which will work on both OS windows and linux?
I need some universal solution so that it works on both OS.
Here is my .gitlab-ci.yml :
stages:
- versioning
variables:
VERSION: ""
versioning:
stage: versioning
script:
- echo "[versioning] ..."
- python ./ci-cd_scripts/get_version.py
- echo $VERSION
Here is my ./ci-cd_scripts/get_version.py :
import os
refName = os.environ.get("CI_COMMIT_REF_NAME")
piplineID = os.environ.get("CI_PIPELINE_ID")
relVersion = refName + ".0." + piplineID
version = relVersion.replace("rel.", "")
print("current version is", version)
python output in pipeline log
what I found that works is to save it to a temp file.
import os
refName = os.environ.get("CI_COMMIT_REF_NAME")
piplineID = os.environ.get("CI_PIPELINE_ID")
relVersion = refName + ".0." + piplineID
version = relVersion.replace("rel.", "")
print("current version is", version)
with open('.env', 'w') as writer:
writer.write(f'export VERSION="{version}"')
and then in the pipeline you just export the variable using the .env file
script:
- ./ci-cd_scripts/get_version.py
- source .env
- echo $VERSION
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With