Gyuri
Gyuri

Reputation: 4892

GitLab CI/CD unset variables set in CI / CD Settings

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?

Upvotes: 7

Views: 12640

Answers (4)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4769

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

Upvotes: -1

Serhii Aksiutin
Serhii Aksiutin

Reputation: 637

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

Example:

 before_script:
    - set S3_OBJECTS
    - source ./s3-objects.sh

Upvotes: -2

Gyuri
Gyuri

Reputation: 4892

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.)

Upvotes: 6

King Chung Huang
King Chung Huang

Reputation: 5624

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

Upvotes: -2

Related Questions