Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an (environment) variable in the build script in Gitlab CI?

I have a dummy build script in the Gitlab CI:

pwd
ci_app_path=$(pwd)
echo "INFO: current directory: $ci_app_path"

and I get this output when the system start the build process:

pwd
/home/kai/gitlab-runners/gitlab-ci-runner/tmp/builds/project-1

ci_app_path=$(pwd)

echo "INFO: current directory: $ci_app_path"
INFO: current directory:

so the variable was not set (or was set only for that line: as I know each line executed separately)

I heard about push/pop mechanism to reach the functionality I desired, but could not find any details, how to implement that.

Update:

as I thought, each line is going executed separately. So the variable scope is only one line, where it is defined:

script:

pwd
ci_app_path=$(pwd) && echo "INFO: current directory: $ci_app_path"

output:

pwd
/home/devuser/gitlab-runners/gitlab-ci-runner/tmp/builds/project-1

ci_app_path=$(pwd) && echo "INFO: current directory: $ci_app_path"
INFO: current directory: /home/kai/gitlab-runners/gitlab-ci-runner/tmp/builds/project-1

I don't think that writing the whole script as a one-liner is a good idea/practice.

How to get the variables set while executing the build script?

P.S.

actually I wonder why the whole build script must contain no empty lines?, otherwise it returns:

No such file or directory

and a build fails on this place

like image 491
static Avatar asked Oct 14 '13 12:10

static


2 Answers

Gitlab CI just plays shell commands so do what you do in *sh:

export ci_app_path=$(pwd)
echo "INFO: current directory: $ci_app_path"
like image 120
Jakub Kania Avatar answered Sep 27 '22 23:09

Jakub Kania


By the time Gitlab-CI folks fix it, you may create a Makefile and export variables inside it and in Gitlab-CI pass the variables, for example

PWD=$PWD make

Makefile starts with a: export GOPATH=$(PWD)

like image 24
rhtyd Avatar answered Sep 28 '22 00:09

rhtyd