How to set global variable from pipeline job?

I have set few variables in the pipeline.

If the variable PACKAGEPATH set, then only the build stage will run.

I want to add one more stage before build as prebuild and add a job there.
So, if the PACKAGEPATH is not set then the job in prebuild stage would run code and need to set the variable there.

But, how to set the variable from one job and make that variable available to other jobs?

Hi @uday.reddy3

passing variables between jobs is done using artifacts. You save your variable to a file and use it in another job.

I kept conditions to further stages to run only when PACKAGEPATH variable is passed.

So, as a first stage, I want to add one more job and set the value for that variable.

But with artifacts it is not possible right.

Modifying global variables from the job scope is not possible. Another way of achieving the jobs being built, and additional global variables being conditionally set, could be using workflow:rules:variables.

1 Like

But I don’t see any way to run a script in workflow you referred. In my case a global variable needs to be set depending on check done by a script, to allow later a bunch of other jobs to get skipped. How would you suggest implementing that with the workflow stuff?

For passing on dynamically created env variables, you can use artifacts:reports:dotenv.

Unfortunately GitLab documentation talks only about environment URL after a job finishes but basically you can set any variable.

Use the needs keyword in your “downstream” jobs and you will have the environment variables available in that job.

PS: I just stumbled upon a nice example in GitLab documentation how to use dotenv.