ibex/ci/vars_to_logging_cmd.py
Philipp Wagner c4a0c9d3bf CI: Script to convert CI variables
Add a script to convert the contents of ci/vars.yml to Azure Pipelines
logging commands, which effectively set runtime variables in a pipeline.

We need this script as a workaround for a missing Azure Pipelines
feature: variables are not inherited in extended templates, and reading
the vars.yml file in a "extends" template isn't possible either (at
least not if we want to use the exact revision of the ibex repository
which triggered CI).
2020-08-21 09:59:32 +01:00

43 lines
1.6 KiB
Python
Executable file

#!/usr/bin/env python3
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
# Read an Azure Pipelines-compatible variables file, and convert it into
# logging commands that Azure Pipelines understands, effectively setting the
# variables at runtime.
#
# This script can be used as a workaround if variables cannot be included in the
# Pipeline definition directly.
#
# See https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands
# for more information on logging commands.
import sys
import yaml
def vars_to_logging_cmd(vars_file):
data = {}
print(vars_file)
with open(vars_file, 'r', encoding="utf-8") as fp:
data = yaml.load(fp, Loader=yaml.SafeLoader)
if not (isinstance(data, dict) and 'variables' in data):
print("YAML file wasn't a dictionary with a 'variables' key. Got: {}"
.format(data))
print("Setting variables from {}".format(vars_file))
for key, value in data['variables'].items():
# Note: These lines won't show up in the Azure Pipelines output unless
# "System Diagnostics" are enabled (go to the Azure Pipelines web UI,
# click on "Run pipeline" to manually run a pipeline, and check "Enable
# system diagnostics".)
print("##vso[task.setvariable variable={}]{}".format(key, value))
return 0
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {} VARS_FILE".format(sys.argv[0]))
sys.exit(1)
sys.exit(vars_to_logging_cmd(sys.argv[1]))