0

I'm trying to print my variable MY_VAR value between two stages on my GitLab CI process. For that, I have the following stage:

build-1:
  stage: build
  image: ...
  script: |
    (
      MYVAR="error">> build.env
    )
  artifacts:
    reports:
      dotenv: build.env
    expire_in: 1 day

build-2:
  stage: build
  image: ...
  script: |
    (
      echo "My variable: ${MYVAR}"
    )
  artifacts:
    when: always 

However, when I see my print on build-2 I got "My variable: ", so my variable returns empty value and it should returns "error". Am I missing something?

Thanks

Pedro Alves
  • 1,004
  • 1
  • 21
  • 47
  • Does this answer your question? [Can't share global variable value between jobs in gitlab ci yaml file](https://stackoverflow.com/questions/52928915/cant-share-global-variable-value-between-jobs-in-gitlab-ci-yaml-file) – D Malan Feb 24 '23 at 16:32
  • Was based on that link that I built my code :D – Pedro Alves Feb 24 '23 at 16:42
  • 1
    You didn't add a dependency between the two jobs with `dependencies` or `needs`. See [here](https://docs.gitlab.com/ee/ci/variables/index.html#control-which-jobs-receive-dotenv-variables). – D Malan Feb 24 '23 at 16:44

2 Answers2

1

As shown on this post Can't share global variable value between jobs in gitlab ci yaml file

I had to add the dependency between the 2 two jobs.

build-1:
  stage: build
  image: ...
  script: |
    (
      MYVAR="error">> build.env
    )
  artifacts:
    reports:
      dotenv: build.env
    expire_in: 1 day

build-2:
  stage: build
  image: ...
  script: |
    (
      echo "My variable: ${MYVAR}"
    )
  artifacts:
    when: always 
  needs:
    - job: build-1
      artifacts: true
Pedro Alves
  • 1,004
  • 1
  • 21
  • 47
0

You could simply use variables as below:

variables:
    MYVAR: "error"

build-1:
  script:
    - echo "MYVAR is '$MYVAR'"

build-2:
  script:
    - echo "My variable: '$MYVAR'"
Kamilo
  • 21
  • 6