'Using find command to set env vars in gitlab-ci
I'm trying to use the find command in linux to detect if some file types are present in the current directory in my .gitlab-ci.yml by doing the following:
---
stages:
- "discover"
file types:
image: "alpine:latest"
stage: "discover"
script:
- "set +e" # so the job won't fail when the exit code is not 0
- "find -name '*.py' -exec ls '{}' + | grep ."
- "echo \"PY=$?\" >> build.env"
- "find -name '*.yml' -exec ls '{}' + | grep ."
- "echo \"YML=$?\" >> build.env"
- "find -name '*.yaml' -exec ls '{}' + | grep ."
- "echo \"YAML=$?\" >> build.env"
- "cat build.env" # returns: PY=0, YML=0, YAML=0
artifacts:
reports:
dotenv: "build.env"
However, in the return of doing cat build.env inside the pipeline, I actually get:
cat build.env
PY=0
YML=0
YAML=0
Whereas, I don't actually have any .yaml files, I always use .yml, so this should return YAML=1.
If I execute the commands in linux myself, and not in the CI, it works as expected:
cat build.env
PY=0
YML=0
YAML=1
I am guessing there is something happening inside the gitlab pipeline somewhere, but to me, it looks like this should work.
Any help is appreciated.
linuxenvironment-variablesgitlab-ciexit-codegitlab-ci.ymloptimizationdeep-learninggradient-descentnon-convex
Solution 1:[1]
Just do it on the same line.
- find -name '*.py' | grep . ; echo PY=$? >> build.env
- find -name '*.yml' | grep . ; echo YML=$? >> build.env
- find -name '*.yaml' | grep . ; echo YAML=$? >> build.env
I think I would do:
- find -name '*.py' | wc -l | sed 's/^/PY=/' >> build.env
- find -name '*.yml' | wc -l | sed 's/^/YML=/' >> build.env
- find -name '*.yaml' | wc -l | sed 's/^/YAML=/' >> build.env
or maybe
- ( printf PY=; find -name '*.py' | wc -l ) >> build.env
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KamilCuk |
