8000 Group variable `create` method does not set environment scope · Issue #3138 · python-gitlab/python-gitlab · GitHub
[go: up one dir, main page]

Skip to content
Group variable create method does not set environment scope #3138
Open
@amimas

Description

@amimas

Description of the problem, including code/CLI snippet

The create method for group variables is not setting the environment_scope attribute. I ran into the issue when attempting to create multiple CI CD variables at the group level where variable keys are same but has different scope.

Below is a script that can reproduce the issue. It attempts to do the following:

  • Create 3 variables in a given group where key is same but has different environment_scope defined
  • List all variables in the group
  • Delete the 3 variables that were added in step 1

To run the script, first set the environment variables mentioned in the comments section and then execute python <path-to-script.py>.

import os
import time
import gitlab

# This script is to try reproduce a bug in GitLab
# Expect following environment variables are set when this script is run:
# - GITLAB_URL: GitLab URL
# - GITLAB_API_TOKEN: GitLab API token
# - GITLAB_GROUP_ID: GitLab group ID

GITLAB_URL = os.getenv('GITLAB_URL')
GITLAB_API_TOKEN = os.getenv('GITLAB_API_TOKEN')
GITLAB_GROUP_ID = os.getenv('GITLAB_GROUP_ID')

# Print environment variables for debugging
print(f"GITLAB_URL: {GITLAB_URL}")
print(f"GITLAB_API_TOKEN: {GITLAB_API_TOKEN}")
print(f"GITLAB_GROUP_ID: {GITLAB_GROUP_ID}")

# Initialize GitLab connection
gl = gitlab.Gitlab(url=GITLAB_URL, private_token=GITLAB_API_TOKEN)

def create_variable(key, value, environment_scope):
    group = gl.groups.get(GITLAB_GROUP_ID)
    group.variables.create({
        'key': key,
        'value': value,
        'variable_type': 'env_var',
        'protected': False,
        'masked': False,
        'environment_scope': environment_scope
    })
    print(f"Created variable: {key} with scope: {environment_scope}")

def delete_variable(key, environment_scope):
    group = gl.groups.get(GITLAB_GROUP_ID)
    group.variables.delete(key, filter={"environment_scope": environment_scope})
    print(f"Deleted variable: {key} with scope: {environment_scope}")

def list_variables():
    group = gl.groups.get(GITLAB_GROUP_ID)
    variables = group.variables.list(get_all=True)
    for var in variables:
        print(f"Variable: {var.key}, Value: {var.value}, Scope: {var.environment_scope}")

# Create variables with the same key but different environment scopes
print("Creating variables...")
create_variable("TEST_VARIABLE", "test_value_prod", "production")
print()
create_variable("TEST_VARIABLE", "test_value_stage", "staging")
print()
create_variable("TEST_VARIABLE", "test_value_dev", "development")
print()

# List variables to ensure the variables are created
print("Listing variables to ensure the variables are created...")
list_variables()
print()

# Delete the variables
print("Deleting variables...")
delete_variable("TEST_VARIABLE", "production")
print()
delete_variable("TEST_VARIABLE", "staging")
print()
delete_variable("TEST_VARIABLE", "development")
print()

# List variables to ensure the variables are deleted
print("Listing variables to ensure the variables are deleted...")
list_variables()
print()

Expected Behavior

When group.variables.create() method is called, it should set/pass all the attributes to the GitLab API so that the variable is configured/created correctly.

Actual Behavior

The group.variables.create() method is not configuring the environment_scope. When running the sample script, it fails when creating the 2nd variable in the group. The error message will indicate that the failure is due to another variable with same key/name already exists:

Traceback (most recent call last):
  File "group_variable_bug.py", line 50, in <module>
    create_variable("TEST_VARIABLE", "test_value_stage", "staging")
  File "group_variable_bug.py", line 25, in create_variable
    group.variables.create({
  File "venv/lib/python3.12/site-packages/gitlab/exceptions.py", line 346, in wrapped_f
    raise error(e.error_message, e.response_code, e.response_body) from e
gitlab.exceptions.GitlabCreateError: 400: {'key': ['(TEST_VARIABLE) has already been taken']}

If we manually check the variable in the GitLab group (Group Settings -> CI CD -> Variables), we'll see the first variable being created but it does not have the environment scope - it's set to All environments as that's the default.

Image

Specifications

  • python-gitlab version: 5.6.0
  • Gitlab server version (or gitlab.com): 17.9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0