File: ci_interactions.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (38 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os, inspect
from ci_tools.variables import in_ci


def output_ci_warning(message: str, location=None) -> None:
    ci_type = in_ci()

    if ci_type == 1:
        if not location:
            import inspect

            source = inspect.stack()[1].filename
        else:
            source = location

        print(f"##vso[task.logissue type=warning;sourcepath={source}]{message}")
    elif ci_type == 2:
        pass
    else:
        print("Unrecognized CI format, not outputting warning.")


def set_ci_variable(name: str, value: str) -> None:
    """
    Sets a runtime variable in azure devops or github actions. The variable will only be available
    AFTER the step which invokes this function is completed.
    """
    ci_type = in_ci()

    if ci_type == 1:
        print(f"##vso[task.setvariable variable={name}]{value}")
    elif ci_type == 2:
        env_file = os.getenv("GITHUB_ENV")

        with open(env_file, "a") as env_file:
            env_file.write(f"{name}={value}")
    else:
        print(f'Unrecognized CI format, not setting variable "{name}."')