[Python] How to properly set versioning pipeline in Gitlab

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have troubles with setting versioning pipeline in GitLab. What is my goal:

    • when it is pushed to main branch (either by commit or MR) the tag with version number is added to commit
    • also version number is added to file within repository (something like version.py and there is version variable which holds version number)

    What I achieved:

    • first part - I have working pipeline which runs when CI_COMMIT_BRANCH == main. It creates and pushes version tag to remote

    What I have troubles with:

    • I don't know when and how to properly update version number in the file (I know how to do it technically, I can update the version number in file, I don't know how to correctly push it to repository so it is there when next job runs).

    Pipeline:

    version-job:
    stage: version
    variables:
    VERSION_FILE_PATH: "<path-to-version-file>"
    image: <custom-image>
    script:
    - python3 /version_app/main.py
    rules:
    - if: $CI_COMMIT_BRANCH== "main"
    tags:
    - <runner-tag>


    Versioning functionality:

    def update_version_in_file(file_path, version):
    if not os.path.exists(file_path):
    logger.warning(f"Unable to write version to file. File path '{file_path}' does not exist")
    return

    logger.info(f"Updating version in file to: {version}")
    with open(file_path,"r+", encoding="utf-8") as v:
    tree = ast.parse(v.read())
    updater = VersionUpdater(version)
    tree = updater.visit(tree)
    v.seek(0)
    v.write(astor.to_source(tree))


    git("add",file_path)
    git("commit","-m",f"File version update {version}")

    def main():
    version = func_to_get_version()
    git("config","http.sslCAInfo",CERTIFICATE_PATH)
    git("config","user.email","<...>")
    git("config","user.name","<...>")

    version_file_path = os.environ.get("VERSION_FILE_PATH","./app/version.py")

    update_version_in_file(version_file_path,version)
    tag_repo(version)
    git("push", "origin", version)

    return 0

    Continue reading...

Compartilhe esta Página