While @Jakub Kania was not that wrong with it's summary
https://mcmap.net/q/553102/-dynamically-setting-artifact-path-folder-structure-on-gitlab-ci-yml
That is no variables set in your script part of the job can be used.
and I have to redict my own comment beneath @Matt Bracewell's post
https://mcmap.net/q/553102/-dynamically-setting-artifact-path-folder-structure-on-gitlab-ci-yml
I don't see this will help anything because it addresses artifacts:name but not artifacts:paths . paths is still not dynamically usable as you can see in https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html
~~the undocumented part says:__~~
you can use any variable in artifacts:path that is available at job level
i.e.
prod_build:
+ variables:
+ NAME_OF_ARTIFACT: "myApp_*.zip"
stage: build
script:
- echo $NAME_OF_ARTIFACT
- yarn run build
artifacts:
paths:
- dist/$NAME_OF_ARTIFACT
expire_in: 2 hrs
That's indeed consequent to what is asked in Artifacts Filename Cannot Be Set with Dynamic Variables even they don't ask for paths
but names
generate for myself:
script:
- mkdir build
- |
cat > build/artifact1$$.txt <<EOF
Hello World! ${CI_PROJECT_ID} ${CI_PIPELINE_IID}
EOF
- export MY_VAR_FILE_NAME=artifact1$$
- echo "MY_VAR_FILE_NAME=${MY_VAR_FILE_NAME}" >> generate.env
- |
cat > build/artifact2$$.txt <<EOF
The end is near! ${CI_PROJECT_ID} ${CI_PIPELINE_IID}
EOF
- export MY_VAR_FILE2_NAME=artifact2$$
- echo "MY_VAR_FILE2_NAME=${MY_VAR_FILE2_NAME}" >> generate.env
- |
cat > build/ci${CI_PIPELINE_IID}.txt <<EOF
Happy to hear you! ${CI_PROJECT_ID} ${CI_PIPELINE_IID}
EOF
artifacts:
reports:
# using the https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsdotenv
# does NOT do the trick to fill a job variable before artifact:paths is evaluated!
dotenv: generate.env
paths:
- build/ci${CI_PIPELINE_IID}.txt # WORKS!
- build/$MY_VAR_FILE_NAME.txt # doen't work
- build/$MY_VAR_FILE2_NAME.txt # doen't work
⚠️ unknown variables are expanded to empty and build/$MY_VAR_FILE_NAME.txt
becomes build/
i.e. all files in build folder are artifacts - mentioned in pipeline out WARNING: build/.txt: no matching files. Ensure that the artifact path is relative to the working directory
resp. unexpected build/: found 4 matching files and directories
As a workaround - but yes, it's ugly -, you can do the trick either by using wildcards (but I assume you have good reasons to do not)
prod_build
artifacts:
paths:
- dist/*.zip
OR splitting into two jobs
generate before save:
script:
- mkdir build
- |
cat > build/artifact1$$.txt <<EOF
Hello World! ${CI_PROJECT_ID} ${CI_PIPELINE_IID}
EOF
- export MY_VAR_FILE_NAME=artifact1$$
- echo "MY_VAR_FILE_NAME=${MY_VAR_FILE_NAME}" >> generate.env
- |
cat > build/artifact2$$.txt <<EOF
The end is near! ${CI_PROJECT_ID} ${CI_PIPELINE_IID}
EOF
- export MY_VAR_FILE2_NAME=artifact2$$
- echo "MY_VAR_FILE2_NAME=${MY_VAR_FILE2_NAME}" >> generate.env
artifacts:
reports:
# using the https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsdotenv trick to fill a job variable for downstream of successor job
dotenv: generate.env
paths:
- build/*.txt
save artifacts:
## this is your expected input:
# variables:
# MY_VAR_FILE_NAME: #filled by dotenv
# MY_VAR_FILE2_NAME: #filled by dotenv
needs:
- generate before save
script:
- find build/
- echo "expecting … "
- echo "- $MY_VAR_FILE_NAME"
- echo "- $MY_VAR_FILE2_NAME"
artifacts:
paths:
- build/$MY_VAR_FILE_NAME.txt # WORKS
- build/$MY_VAR_FILE2_NAME.txt # WORKS
i.e. in your case:
image: node:latest
stages:
- build
pre_prod_build:
stage: build
script:
## steps ignored for purpose of question
- export NAME_OF_ARTIFACT_FOLDER=$(cat package.json | jq -r .name)"_"$(cat package.json | jq -r .version)".zip"
- echo $NAME_OF_ARTIFACT_FOLDER ##prints the expected name here eg. myApp_1.0.0.zip
- echo "NAME_OF_ARTIFACT_FOLDER=${NAME_OF_ARTIFACT_FOLDER}" >> artifact_name.dotenv
artifacts:
reports:
dotenv: artifact_name.dotenv
prod_build:
stage: build
## this is your expected input:
# variables:
# NAME_OF_ARTIFACT_FOLDER: # set by pre_prod_build dotenv
needs:
- job: pre_prod_build
script:
- echo $NAME_OF_ARTIFACT_FOLDER ##prints the expected name here eg. myApp_1.0.0.zip
- yarn run build
artifacts:
paths:
- dist/$NAME_OF_ARTIFACT_FOLDER ## NOW IT WORKS
expire_in: 2 hrs
yarn build
and would want gitlab to identify the said artifact for deployment – Kampmeier