GitLab CI/CD unset variables set in CI / CD Settings
Asked Answered
M

4

7

I'm running a gitlab job and in the job I'm trying to unset certain variables set in CI / CD Settings.

For example, I have SOME_VARIABLE set to <some_value>

Then, in the job definition, I'm trying


variables:
   SOME_VARIABLE: ""
script:
    - echo SOME_VARIABLE - [%SOME_VARIABLE%]

But in the job itself I'm still getting

SOME_VARIABLE - [<some_value>]

instead of

SOME_VARIABLE - []

Has anyone came across this?

Malkamalkah answered 16/3, 2019 at 8:27 Comment(2)
Can you show your .gitlabci.yml file? It may be caused by Gitlab CI interpreting your double quote before script as empty and still reusing the old one. Maybe if you put a space instead or another neutral valuePantia
Yup come across this multiple times and it's very annoying.Lashanda
M
6

I'll have to answer this since it might be rather obscure.

So turns out when you set a variable on Windows, you have to say

set v=some_value

To unset it, it needs to be

set v=

not

set v=''

When you set it to empty string it will be that, just quotes:

$ set v=""
$ echo %v%
""

If you unset it to empty correctly, you get:

$ set v=
$ echo %v%
%v%

However in gitlab, you can't leave values empty, such as

variables:
   v:

because the it's invalid syntax.

So what I had to do was unset the variable in the script area:

script:
  - set v=
  - run_my_script_that_needs_v_unset

Then the script worked as needed.

(I imagine it can be done similarly on the other platforms.)

Malkamalkah answered 17/3, 2019 at 5:9 Comment(2)
On Linux, you can just use unset VARIABLE. This answer confused me because it's specific to Windows, but the examples start with $ which is typically shown in the Linux command prompt (the Windows command prompt is something else, like >, right?)Pommard
You can unset the variable in the .gitlab-ci.yml as well by using VARIABLE: null, which is YAML format for an empty value.Mavilia
Y
0

You can use the GitLab API to play around with the CI/CD variable. You can either delete it or update it (depending on what suits you)

Delete Variable Update Variable

Below is the gitlab job to do the delete of variable named SOME_VARIABLE

cleanup-Variable:
  stage: cleanup
  script:
    - apk add --no-cache curl
    - 'STATUS_CODE="$(curl -o /dev/null -s -w "%{http_code}\n" --request DELETE --header "PRIVATE-TOKEN: ${YOUR_PROJECT_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CR_NUMBER")"'
- echo "Status code for delete variable is  $STATUS_CODE"
- if [[ $STATUS_CODE == "204" ]]; then exit 0; else exit 1; fi
  when: always

Edite answer. The Delete API returns 204 instead of 200. And no response body hence we have to use -s -w "%{http_code}\n" flags in curl command

Yaya answered 27/6, 2022 at 10:38 Comment(0)
M
-1

Project CI/CD variables take precedence over YAML-defined variables. Here's the order of precedence.

  1. Trigger variables or scheduled pipeline variables.
  2. Project-level variables or protected variables.
  3. Group-level variables or protected variables.
  4. YAML-defined job-level variables.
  5. YAML-defined global variables.
  6. Deployment variables.
  7. Predefined environment variables.

from GitLab CI/CD Variables: Priority of variables

Monohydroxy answered 16/3, 2019 at 21:44 Comment(1)
This is technically correct but does not answer my question.Malkamalkah
H
-1

You don't need = just do set <varible-name>

Example:

 before_script:
    - set S3_OBJECTS
    - source ./s3-objects.sh
Humoresque answered 31/10, 2020 at 7:46 Comment(1)
This is helpful, but it probably should've been a comment on Gyuri's answerPommard

© 2022 - 2024 — McMap. All rights reserved.