diff --git a/scripts/gitlab-ci-vars-updater.py b/scripts/gitlab-ci-vars-updater.py index 5e51a25adfde9987e00cb72fbca093b2e7fa8eb4..232720c177612e687da728667224e87326177696 100644 --- a/scripts/gitlab-ci-vars-updater.py +++ b/scripts/gitlab-ci-vars-updater.py @@ -1,91 +1,66 @@ -import requests -import json import argparse +import json + +import gitlab + -# Load configuration from JSON file -def load_config(file_path): +def load_file(file_path): try: - with open(file_path, 'r') as file: + with open(file_path, "r") as file: return json.load(file) except FileNotFoundError: print(f"Error: Configuration file '{file_path}' not found.") exit(1) -# Function to create or update a GitLab CI/CD variable -def create_or_update_variable(config, var_name, var_value): - gitlab_url = config["gitlab_url"] - project_id = config["dest_project_id"] - private_token = config["private_token"] - - headers = { - "PRIVATE-TOKEN": private_token, - "Content-Type": "application/json" - } - - url = f"{gitlab_url}/api/v4/projects/{project_id}/variables/{var_name}" - # Check if the variable already exists - response = requests.get(url, headers=headers) - - if response.status_code == 404: - print(f"Variable '{var_name}' does not exist. Creating new variable...") - create_url = f"{gitlab_url}/api/v4/projects/{project_id}/variables" - data = { - "key": var_name, - "value": var_value, - "variable_type": "env_var", # Can be 'file' for file variables - "protected": False, # Set to True if you want the variable protected - "masked": False # Set to True if you want the variable masked - } - create_response = requests.post(create_url, headers=headers, json=data) - - if create_response.status_code == 201: - print(f"Variable '{var_name}' created successfully.") - else: - print(f"Failed to create variable '{var_name}'. Status code: {create_response.status_code}") - elif response.status_code == 200: - print(f"Variable '{var_name}' already exists. Updating variable...") - data = {"value": var_value} - update_response = requests.put(url, headers=headers, json=data) - - if update_response.status_code == 200: - print(f"Variable '{var_name}' updated successfully.") - else: - print(f"Failed to update variable '{var_name}'. Status code: {update_response.status_code}") +# 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 + p_variable = project.variables.get(var_dict["key"]).asdict() + if p_variable: + # 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 from: {p_variable[k]} -> {v}") + project.variables.update(k, {"value": v}) + # Create variable if it doesn't exist in the project else: - print(f"Error checking variable '{var_name}'. Status code: {response.status_code}") + for k, v in var_dict.items(): + print(f"Creating key {k}: with value -> {v}") + project.variables.create({"key": k, "value": v}) + -# Main function to load the auth config and apply variables def main(): # Setup argument parser parser = argparse.ArgumentParser(description="GitLab CI/CD Variables Updater") parser.add_argument( - "--auth_config", + "--config_file", type=str, - default="auth-config.json", - help="Path to the configuration file (default: config.json)" + default="gitlab.ini", + help="Path to the configuration file (default: gitlab.ini)", ) parser.add_argument( - "--vars", + "--var_file", type=str, default="ci-variables.json", - help="Path to the configuration file (default: config.json)" + help="Path to the CI vars file (default: ci-variables.json)", ) + parser.add_argument("--project_id", type=int, help="Gitlab project ID") # Parse the arguments args = parser.parse_args() - # Load the configuration file - config = load_config(args.auth_config) - vars_dict = load_config(args.vars) + gl = gitlab.Gitlab.from_config("uabrc", [args.config_file]) + project = gl.projects.get(args.project_id) + + # Load the CI vars file + var_list = load_file(args.var_file) - ci_variables = vars_dict["ci_variables"] + # Create or update all variables + for var_dict in var_list: + create_or_update_variable(project, var_dict) - # Loop through and create/update all variables - for var_name, var_value in ci_variables.items(): - create_or_update_variable(config, var_name, var_value) -# Run the main function if __name__ == "__main__": main() -