-
Notifications
You must be signed in to change notification settings - Fork 670
Group variable create
method does not set environment scope
#3138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was checking out the code and noticed following differences between Group VS Project variables. The project variable manager is configured with the python-gitlab/gitlab/v4/objects/variables.py Lines 57 to 68 in cbc613d
But, the group variable manager doesn't set the python-gitlab/gitlab/v4/objects/variables.py Lines 41 to 50 in cbc613d
I looked through the history of the above code and noticed the group variable manger was always configured this way ( Also, GitLab supports more attributes for variables (i.e. |
@nejch - When you get a chance, could you please share your insight? Do you agree with my findings? I was thinking of opening a PR to fix it but wasn't sure about the tests. In this case, should it be in the unit test or functional test? My worry about unit test is that API responses are being mocked - so, the tests would easily pass even if I don't make any code change. Not sure what the general guideline is for testing. |
@amimas I'm not able to reproduce this myself, your snippet works without issue for me. However I'm also testing against GitLab 16.7.10. The following was run against a subgroup under a top level group: Stopping halfway through, the variables in the UI look correct I also tested first with python-gitlab 5.0.0, then also tested 5.6.0, no change in behavior. Is it possible this a GitLab licensing issue? Environment scope is only available (per the docs) for Premium and Ultimate customers |
Hi @holysoles - thanks for trying to reproduce it. I don't think it's a gitlab license issue. I had checked that too. I think I mentioned earlier that I was having difficulty reproducing the issue at first. After blowing away my venv and doing a fresh setup, I was able to reproduce it. Not sure why though. I linked another PR to this issue above. That's where I ran into it first and it's blocking that PR. That's why I tried to create a small reproducible script for this issue here. |
Thought I'd try again with a fresh environment. Unfortunately I'm still seeing the issue on my end. Here's what my python venv has:
I used GitLab version 17.9.0.
|
As a note the Feel free to do a PR to add them, as long as the upstream GitLab docs show they are valid. |
I just ran into an issue that might be related: However: I run into this issue not with It seems to me this issue does exist, but as it occurs with two entirely different gitlab tools, the problem might not lie in the two tools, but in gitlab itself: Possibly, gitlab does not properly handle Premium / Ultimate vs. Community editions ? 🤷🏻♂️ My conclusion: It might make sense to report this as a bug report to gitlab - currently ~61'000 open issues 🥴. |
@dd-ssc - Not sure what brought you to this thread but really glad you commented. Usually I test out features directly in GitLab but somehow I didn't think of it for this one. I can confirm now that the issue is in GitLab itself 😥 . Remove any tools and go back to basics. Simple Even in the UI, creating the group level variable, there's no way to set a value for environment scope, regardless of create/update action. # GitLab API for creating a group level CI variable with environment scope defined
$ curl --request POST --header 'PRIVATE-TOKEN: token-string-here123' --form "key=TEST_VAR" --form "value=TEST_VALUE" --form "environment=review/*" --url 'http://localhost/api/v4/groups/test-group/variables' | jq .
{
"variable_type": "env_var",
"key": "TEST_VAR",
"value": "TEST_VALUE",
"hidden": false,
"protected": false,
"masked": false,
"raw": false,
"environment_scope": "*",
"description": null
} # GitLab API for updating the previous group level CI variable's value and environment scope
$ curl --request PUT --header 'PRIVATE-TOKEN: token-string-here123' --form "value=TEST_UPDATED_VALUE" --form "environment=review/*" --url 'http://localhost/api/v4/groups/test-group/variables/TEST_VAR' | jq .
{
"variable_type": "env_var",
"key": "TEST_VAR",
"value": "TEST_UPDATED_VALUE",
"hidden": false,
"protected": false,
"masked": false,
"raw": false,
"environment_scope": "*",
"description": null
} Definitely this is a bug in GitLab 😞 . Since @holysoles couldn't reproduce the issue using the sample script I provided using GitLab v16.7.10, the issue must have been introduced in one of the later updates. Surprised this wasn't caught/fixed yet while GitLab went through 2 major release already (current version is 18.x). Maybe it's not a widely used feature 🤷 |
I realized that I was a bit too quick to the tests mentioned in previous comment. The environment scope for group variable requires premium or ultimate license. My test gitlab instance did not have the necessary license setup. So, I retried it again. # check gitlab license
$ curl --request GET --header "PRIVATE-TOKEN: token-string-here123" "http://localhost/api/v4/license" | jq .
{
"id": 1,
"plan": "ultimate",
...
} Re-ran the same tests and confirmed that it's still behaving same way. Cannot set the environment scope via the API. Tried to create the group variable with environment scope manually in the UI and that works fine. |
Couldn't find an existing issue in GitLab about it. Opened one now: https://gitlab.com/gitlab-org/gitlab/-/issues/548248 |
Uh oh!
There was an error while loading. Please reload this page.
Description of the problem, including code/CLI snippet
The
create
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:
environment_scope
definedTo run the script, first set the environment variables mentioned in the comments section and then execute
python <path-to-script.py>
.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 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: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
The text was updated successfully, but these errors were encountered: