Pass the Gitlab variable values using python

Hello , I am New in GitLab, I want that in GitLab, the value of user defined variables which we keep inside the gitlab-ci.yml file, should not keep that value inside gitlab-ci.yml, but the value of that variable should come from the Python code. for example -

This is my gitlab-ci.yml -


  • generate_artifact

stage: generate_artifact
- cat simple_script.txt > pipeline_statuses.json
- echo “My Username is $POSTGRES_USERNAME” # Access the environment variable
- echo “My Password is $POSTGRES_PASSWORD” # Access the environment variable
- echo “My Database Name is $POSTGRES_DATABASE_NAME” # Access the environment variable
- echo “This is my Database Version $POSTGRES_VERSION” # Access the environment variable
- shell
- pipeline_statuses.json
POSTGRES_DATABASE_NAME: “$database_name”
POSTGRES_VERSION: “$postgres_version”

Now I want the value of username , password , database_name and postgres_version to be passed, which value I can give through UI.

Now this is my file

import requests
import base64
import zipfile
import io
import os
from django.shortcuts import render
from django.http import JsonResponse
from django.http import HttpResponse # Import this if not already imported

class CustomBranchNotFoundError(Exception):
selected_tools =

def trigger_pipeline_view(request):
if request.method == ‘POST’:
form_data = request.POST
username = request.POST.get(‘username’)
password = request.POST.get(‘password’)
database_name = request.POST.get(‘database_name’)
postgres_version = request.POST.get(‘postgres_version’)
# Dictionary mapping tool names to their corresponding branch names
print(f’Username: {username}‘)
print(f’Password: {password}’)
print(f’Database Name: {database_name}‘)
print(f’Postgres Version: {postgres_version}’

        tool_branch_mapping = {
            "Fluetd" : "fluentd" ,  
            "Rsyslog" : "rsyslog"  ,
            "Filebeat" : "filebeat",
            "Graylog" : "graylog" ,


        # Identify which tools the user has selected
        for tool, branch_name in tool_branch_mapping.items():
            if form_data.get(tool):

        request.session['selected_tools_count'] = len(selected_tools)
        # Replace these variables with your actual GitLab project ID and private token
        project_id = "29"
        private_token = "glpat-8xMVT61BBXVx7Cz_Si8K"
        base_url = ""
        headers = {"PRIVATE-TOKEN": private_token}

        pipeline_names = []

        for branch_name in selected_tools:
            trigger_branch(base_url, project_id, headers, branch_name)

    except CustomBranchNotFoundError:
        return render(request, 'custom_error_page.html', {'error_message': "Specified branch does not exist."})

    return render(request, 'result.html', {'message': 'Installation in Progress', 'pipeline_names': pipeline_names})
    return render(request, 'trigger_pipeline.html')

def trigger_branch(base_url, project_id, headers, branch_name):
data = {
“ref”: branch_name,
response = + f"projects/{project_id}/pipeline", headers=headers, json=data, verify=False)

    response.raise_for_status()  # This will raise an exception if response status code is not 2xx
except requests.exceptions.HTTPError as e:
    if response.status_code == 400 and "Reference not found" in response.json().get('message', {}).get('base', []):
        raise CustomBranchNotFoundError("The specified branch does not exist.")
        raise ValueError(f"Error triggering pipeline for branch '{branch_name}': {e}")

def get_latest_pipeline_statuses(base_url, project_id, headers, count=2):
response = requests.get(base_url + f"projects/{project_id}/pipelines", headers=headers, verify=False)

if response.status_code != 200:
    raise ValueError(f"Error fetching pipelines: {response.status_code}, {response.json()}")

pipelines = response.json()
if not pipelines:
    return []

latest_pipelines = pipelines[:count]
latest_statuses = []

for pipeline in latest_pipelines:
    latest_status = pipeline['status']
    latest_statuses.append({"id": pipeline['id'], "status": latest_status})

return latest_statuses

def get_latest_pipeline_artifacts(base_url, project_id, headers, pipeline_id):
response = requests.get(base_url + f"projects/{project_id}/pipelines/{pipeline_id}/jobs", headers=headers, verify=False)

if response.status_code != 200:
    raise ValueError(f"Error fetching pipeline jobs: {response.status_code}, {response.json()}")

jobs = response.json()
artifacts = []
# customer_name = request.POST.get('customer_name', '')
for job in jobs:
    response = requests.get(base_url + f"projects/{project_id}/jobs/{job['id']}/artifacts", headers=headers, verify=False)
    if response.status_code == 200:
        with zipfile.ZipFile(io.BytesIO(response.content), 'r') as zip_file:
            # Modify the following to fetch the required artifacts
            required_artifacts = ['ip.txt', 'info.txt']
            for artifact_name in required_artifacts:
                if artifact_name in zip_file.namelist():
                    content ='utf-8')
                    artifacts.append({"filename": artifact_name, "content": content})
                    # Create a new PipelineArtifact instance and save it to the database
                    # Check if an artifact with the same filename and pipeline ID already exists
                    existing_artifact = PipelineArtifact.objects.filter(

                    if existing_artifact:
                        # Update the content of the existing artifact
                        existing_artifact.content = content
                        existing_artifact.customer = customer_instance
                        # Create a new PipelineArtifact instance and save it to the database
                        artifact = PipelineArtifact(

return artifacts

def display_artifacts(request):
# Retrieve all saved artifacts from the database
artifacts = PipelineArtifact.objects.all()

# Pass the artifacts to the template
context = {'artifacts': artifacts}

return render(request, 'display_artifacts.html', context)

def get_latest_pipeline_statuses_and_artifacts(request):
# Replace these variables with your actual GitLab project ID and private token
project_id = “29”
private_token = “glpat-8xMVT61BBXVx7Cz_Si8K”
base_url = “
headers = {“PRIVATE-TOKEN”: private_token}
pipeline_count = request.session.get(‘selected_tools_count’, 0)

# Get the statuses of the latest pipelines
latest_pipeline_statuses = get_latest_pipeline_statuses(base_url, project_id, headers, pipeline_count)

# Get the artifacts for each of the latest pipelines
all_artifacts = []
for pipeline_status in latest_pipeline_statuses:
    pipeline_id = pipeline_status["id"]
    artifacts = get_latest_pipeline_artifacts(base_url, project_id, headers, pipeline_id)
    all_artifacts.append({"status": pipeline_status["status"], "artifacts": artifacts})

return JsonResponse({"pipelines": all_artifacts})

Now, I want pass the value of these variables username , password , database_name and postgres_version in gitlab-ci.yml file using python code

Thanks for taking the time to be thorough in your request, it really helps! :blush: