pinas
pinas

Reputation: 3074

Use script variable in artifact path

is there any chanche to use the TEST_VAR variable in the artifacts path?

inject:
  stage: inject
  script:
    - echo "testDownload.zip" > varName.txt
    - export TEST_VAR=$(cat varName.txt)
    - echo $TEST_VAR #This is working
    - wget http://some.url.com/download/testDownload.zip
  artifacts:
    name: 
    paths:
    - $TEST_VAR
    expire_in: 1h

In production the file testDownload.zip will have a variable name (-.zip) and I'd like to make it available to all later stages with its original name.

Thank you

Andrew is correct (unfortunately) - somewhere I found the following "workaround" (not great, not terrible):

variables:
  INJECTION_PATH: "./Test/"


inject:
  stage: inject
  before_script:
    - sudo apt-get -qq install unzip
  script:
    - wget hhttp://some.url.com/download/7d8e6751-2ca3-477c-9185-7097932c3043.zip
    - unzip ./7d8e6751-2ca3-477c-9185-7097932c3043.zip -d $INJECTION_PATH
  artifacts:
    paths:
      - $INJECTION_PATH
    expire_in: "600"

This will make the $INJECTION_PATH folder and its contant available in every stage independent of the filenames in the folder.

Upvotes: 4

Views: 4939

Answers (1)

Andrew
Andrew

Reputation: 4622

Unfortunately, it's not possible, see https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html#gitlab-runner-internal-variable-expansion-mechanism :

GitLab Runner internal variable expansion mechanism

  • Supported: project/group variables, .gitlab-ci.yml variables, config.toml variables, and variables from triggers, pipeline schedules, and manual pipelines.
  • Not supported: variables defined inside of scripts (e.g., export MY_VARIABLE="test").

Exported variables are available only in "*script" section, with some limitations regarding after_script, see https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html#execution-shell-environment

My understanding is that everything outside *script sections is initialized when job is started, and can't be redefined at runtime. In your case you may set a predefined path (i.e. downloaded_artifact.zip), and then wget should save downloaded content into that file. To preserve filename you can echo filename in downloaded_artifact.txt, and pass it to the next stage. It's hacky, but unfortunately you can't pass context between stages in anything other that files (or some shared db, which can be a more hacky solution).

Related ticket: https://gitlab.com/gitlab-org/gitlab/-/issues/16765

Upvotes: 5

Related Questions