Pass gitlab runner environment variables to host

This is the general ideia:
I have a project that locally runs over docker, then I have CI with gitlab that transfers via FTP, my FTP account info is in the gitlab-ci.yml file… My ideia is to add environment variables in to my docker-compose file and retrieve them on Gitlab CI Runner…
I have no problem creating the variables in docker-compose, but in runner if I type env or export -p they are not visible… Any suggestion how to make this possible…
Resuming

—> -> retrieve variables declared in docker-compose…

Advance thanks

Hello,

You can define your variables in the yaml file to compose:
ex:

environment:
- VARFROMYAML=Text1

and from the .gitlab-ci.yml file, you can use the clauses:
Ex:

before_script:
  - echo "${VARFROMYAML}"
  - export VAR1=Text2
  - echo "${VAR1}"

also clause

script:
  - echo "${VARFROMYAML}"
  - export VAR1=Text2
  - echo "${VAR1}"

other way than the compose file, it would be export from a script

from the .gitlab-ci.yml file use the “before_script” clause to export the variables
Ex:
before_script:
- source /home/gitlab-runner/env.sh

1 Like

Oh… Is not working:

This is what i did (for testing):

a) In docker-compose-yml file:

  environment:
      FTP_USERYML: PUSER
      FTP_PASSYML: PPASS

b) In gitlab-ci.yml

before_script:
    #Instalação do lftp na máquina virtual
    - echo "${FTP_USERYML}"
    - env FTP_TESTE="${FTP_USERYML}"

But in the runner i got:

Screenshot%20from%202018-07-06%2011-04-48

As you can see the echo is empty :disappointed:!!

Any suggestion :roll_eyes:?

I see that you are using docker executor docker executor, my example was based on a shell executor.

Reviewing the documentation of docker-compose, a note appears, I assume that the difference. #environment

I made another small example with docker executor:

Check it maybe it will help you as a guide, but from the .gitlab.ci.yml file and not from compose:

image: ruby:2.5

variables:
  VAR4: "Valor4"
  
before_script:
  - echo "From before_script -> ${VAR2}"
  - echo "From before_script -> ${VAR4}"
  - export VAR3="VALOR3"
  
after_script:
  - echo "From after_script VAR2-> ${VAR2}"
  - echo "From after_script VAR3-> ${VAR3}"
  - echo "From after_script VAR4-> ${VAR4}"
stages:
  - build
  - test
  - deploy

job1:
  stage: build
  script:
  - echo "From script -> ${VAR2}"
  - echo "From script-> ${VAR3}"
  - echo "From script-> ${VAR4}"
  only:
    - master
  tags:
    - docker
    
before_script:
  - echo "From before_script VAR2-> ${VAR2}"
  - echo "From before_script VAR3-> ${VAR3}"
  - echo "From before_script VAR4-> ${VAR4}"

observe the variable clause

    on Runner para prueba - borrar (ee5f25d0)
    Using Docker executor with image ruby:2.5 ...
    Using docker image sha256:158cf88dc768e1f423757e1d462cd0575126f23d1b6ea422679094b005582cdf for predefined container...
    Pulling docker image ruby:2.5 ...
    Using docker image ruby:2.5 ID=sha256:55fb4a37704e96ac8c6c930180e22cf986207d6d39ab03dedc41a608bbd30372 for build container...
    Running on runner-ee5f25d0-project-370-concurrent-0 via d7745b02ea41...
    Fetching changes...
    HEAD is now at 8852fd1 Update .gitlab-ci.yml
    From http://host/test/test-project
       8852fd1..c9ddf12  master     -> origin/master
    Checking out c9ddf127 as master...
    Skipping Git submodules setup
    $ echo "From before_script VAR2-> ${VAR2}"
    From before_script VAR2-> 
    $ echo "From before_script VAR3-> ${VAR3}"
    From before_script VAR3-> 
    $ echo "From before_script VAR4-> ${VAR4}"
    From before_script VAR4-> Valor4
    $ echo "From script -> ${VAR2}"
    From script -> 
    $ echo "From script-> ${VAR3}"
    From script-> 
    $ echo "From script-> ${VAR4}"
    From script-> Valor4
    Running after script...
    $ echo "From after_script VAR2-> ${VAR2}"
    From after_script VAR2-> 
    $ echo "From after_script VAR3-> ${VAR3}"
    From after_script VAR3-> 
    $ echo "From after_script VAR4-> ${VAR4}"
    From after_script VAR4-> Valor4
    Job succeeded

Regards,