Update Gitlab CI/CD variables in pipelines

Hi,

I would like to update ci/cd variables in gitlab pipeline, so that the pipeline is able to update values of existing variables.
How would I do that? Is there any built-in feature for that? Or do I have to build a script which use the REST-API of gitlab ci /cd variables?

Thanks
Jens

HI,

the predefined variables can’t be change.
But if you want to “play” with variables, you can declare, and change value.

For example if you define a default variable, in a job you can overwrite it.

1 Like

I’m assuming you’re referring to custom project and/or group level variables one defines via the UI ?
There is a REST API endpoint for updating (PUT) project & group level variables but as Jean pointed out, not the Gitlab predefined variables (CI_PROJECT_*). I’m also assuming you want to change these so later pipeline instances can make use of the revised values? Otherwise, these are injected as env variables & you can alter them as one might do with any variables. To be fair, I’ve not experimented to see if any of the predefined vars are injected as read-only variables (bash’s declare -r name=val).

1 Like

Can you be a little bit more explicit about what you need to do? Do you just need to update the variables within a pipeline, or do you really need to write the new variables back to the project for future pipelines to pick up?

I just stumbled on to this topic while looking for an answer to the same question (I think). I don’t know if I should start a new topic or continue here, so I apologize if it doesn’t need to be here. I need to do what the OP had asked and have the value of the variable be updated for the next time the pipeline runs. We generate an output file that has an iteration number as part of the filename. We need to increment that iteration number each time the pipeline runs.

@AUGrad98 I think the answer to your question is to use the project variable API to do the update from within the pipeline.

1 Like