Skip to content
Snippets Groups Projects

Draft: feat: Add scripts to read and write the cicd vars

Closed Eesaan Atluri requested to merge atlurie/hpc-factory:feat-gl-cicd-vars-updater into main
Files
4
+ 71
0
import requests
import json
import argparse
# Load configuration from JSON file
def load_config(file_path):
try:
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 fetch all CI/CD variables from a GitLab project
def fetch_variables(config):
gitlab_url = config["gitlab_url"]
project_id = config["src_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?per_page=100"
response = requests.get(url, headers=headers)
if response.status_code == 200:
variables = response.json()
return variables
else:
print(f"Failed to fetch variables. Status code: {response.status_code}")
return []
# Function to format the fetched variables for input into the config file
def format_variables_for_input(variables):
ci_variables = {var['key']: var['value'] for var in variables}
return ci_variables
# Main function to load the config and fetch variables
def main():
# Setup argument parser
parser = argparse.ArgumentParser(description="GitLab CI/CD Variables Reader")
parser.add_argument(
"--auth_config",
type=str,
default="auth-config.json",
help="Path to the configuration file (default: config.json)"
)
# Parse the arguments
args = parser.parse_args()
# Load the configuration file
config = load_config(args.auth_config)
# Fetch and print the variables
variables = fetch_variables(config)
if variables:
print("ci_variables:")
formatted_variables = format_variables_for_input(variables)
# Print the formatted variables dictionary in JSON format
print(json.dumps(formatted_variables, indent=4))
else:
print("No variables found.")
# Run the main function
if __name__ == "__main__":
main()
Loading