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

19,999

Solution 1

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"

Solution 2

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)

Share:
19,999
static
Author by

static

Updated on June 08, 2022

Comments

  • static
    static almost 2 years

    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