2

I am wanting to store the output from a script in a variable for use in subsequent commands from within Gitlab CI. Here is the script:

image: ...

build c-ares:
  variables:
    CARES_ARTIFACTS_DIR: "-"
  script:
    - CARES_ARTIFACTS_DIR=$(./build-c-ares.sh)
  after_script:
    - echo $CARES_ARTIFACTS_DIR
  artifacts:
    name: CARES_ARTIFACTS
    paths:
      - $CARES_ARTIFACTS_DIR

My intention is to:

  1. first declare the variable CARES_ARTIFACTS_DIR with global scope
  2. Set the variable value using the output from the build-c-ares.sh script
  3. Recover the output from the build-c-ares.sh script on a later command using the variable

My code does not behave as intended - on dereferencing the variable I find it contains the original value it was assigned at declaration:

$ CARES_ARTIFACTS_DIR=$(./build-c-ares.sh)
Cloning into 'c-ares'...
Running after_script
00:01
Running after script...
$ echo $CARES_ARTIFACTS_DIR
-
Uploading artifacts for successful job
00:00
Uploading artifacts...
WARNING: -: no matching files. Ensure that the artifact path is relative to the working directory 
ERROR: No files to upload                     

2 Answers 2

1

It is probably easier to just redirect the script output to a file and define that as an artifact.

Something similar to:

image: ...

build c-ares:
  script:
    - ./build-c-ares.sh > script_output
    - cat script_output
  artifacts:
    paths:
      - script_output
Sign up to request clarification or add additional context in comments.

Comments

0

In regards to the specific issue, the variables used in the "artefacts" step will again use the variable initialisation defined for the job. Both the "artefacts" and the "script" steps for the job will start with the custom CARES_ARTIFACTS_DIR variable set to the value "-":

build c-ares:
  variables:
    CARES_ARTIFACTS_DIR: "-"
  script:
    # $CARES_ARTIFACTS_DIR=="-"
    - CARES_ARTIFACTS_DIR=$(./build-c-ares.sh)
    # $CARES_ARTIFACTS_DIR=="hello from build-c-ares.sh"
    - echo $CARES_ARTIFACTS_DIR # prints "hello from build-c-ares.sh"
  after_script:
    # $CARES_ARTIFACTS_DIR=="-"
    - echo $CARES_ARTIFACTS_DIR # prints "-"

Fundamentally, Gitlab variables cannot feed information across job steps as intended in the original post. My subjective opinion is to keep steps independent where possible and restrict input to artefacts from upstream jobs or variables explicitly defined in the pipeline script or settings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.