Skip to content
Snippets Groups Projects

Feat gl cicd var scripts

Merged Eesaan Atluri requested to merge atlurie/hpc-factory:feat-gl-cicd-var-scripts into main
@@ -15,32 +15,51 @@ def load_file(file_path):
# Function to create or update a GitLab CI/CD variable
def create_or_update_variable(project, var_dict):
# Check if the variable exists in the project
key = var_dict.get("key")
scope = var_dict.get("environment_scope", "*")
p_variable = None
DEFAULTS = {
"variable_type": "env_var",
"hidden": False,
"protected": False,
"masked": False,
"environment_scope": "*",
"raw": False,
"description": None,
}
# Merge defaults with var_dict
var_dict = {**DEFAULTS, **var_dict}
# Fetch a variable with matching key and scope
try:
p_variable = project.variables.get(var_dict["key"]).asdict()
all_vars = project.variables.list(get_all=True)
for var in all_vars:
if var.key == key and var.environment_scope == scope:
p_variable = var
break
except gitlab.exceptions.GitlabGetError:
print("Variable not found")
p_variable = False
if p_variable:
if p_variable != var_dict:
# Check if the attributes are the same
for k, v in var_dict.items():
if p_variable[k] != v:
# If not update the value in the project
print(f"Updating key {k} value")
project.variables.update(k, {"value": v})
exit(1)
# Check if the variable exists and same as input
if p_variable is not None:
if p_variable.asdict() != var_dict:
# if not same update the project variable
print(f"Updating {p_variable.attributes['key']}")
p_variable.delete()
return project.variables.create(var_dict)
else:
print(f"variable {var_dict["key"]} already exists")
# Create variable if it doesn't exist in the project
else:
for k, v in var_dict.items():
print(f"Creating variable {var_dict["key"]}")
return project.variables.create(var_dict)
print(f"Creating variable {var_dict["key"]}")
return project.variables.create(var_dict)
def get_pipeline_vars_by_key(sched_pipeline, key_name):
p_vars = sched_pipeline.attributes["variables"]
# p_vars.sort(key=lambda x: x["key"])
for p_variable in p_vars:
if p_variable.get("key") == key_name:
return p_variable
@@ -104,7 +123,6 @@ def main():
# Load the CI vars file
var_list = load_file(args.var_file)
# var_list.sort(key=lambda x: x["key"])
# Create or update all variables
for var_dict in var_list:
Loading