- Notifications
You must be signed in to change notification settings - Fork673
Description
Description of the problem, including code/CLI snippet
Thecreate
method for group variables is not setting theenvironment_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 executepython <path-to-script.py>
.
importosimporttimeimportgitlab# 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 IDGITLAB_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 debuggingprint(f"GITLAB_URL:{GITLAB_URL}")print(f"GITLAB_API_TOKEN:{GITLAB_API_TOKEN}")print(f"GITLAB_GROUP_ID:{GITLAB_GROUP_ID}")# Initialize GitLab connectiongl=gitlab.Gitlab(url=GITLAB_URL,private_token=GITLAB_API_TOKEN)defcreate_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}")defdelete_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}")deflist_variables():group=gl.groups.get(GITLAB_GROUP_ID)variables=group.variables.list(get_all=True)forvarinvariables:print(f"Variable:{var.key}, Value:{var.value}, Scope:{var.environment_scope}")# Create variables with the same key but different environment scopesprint("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 createdprint("Listing variables to ensure the variables are created...")list_variables()print()# Delete the variablesprint("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 deletedprint("Listing variables to ensure the variables are deleted...")list_variables()print()
Expected Behavior
Whengroup.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
Thegroup.variables.create()
method is not configuring theenvironment_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 egitlab.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.

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