Search code examples
linuxenvironment-variablesgitlab-ciexit-codegitlab-ci.yml

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.


Solution

  • I ended up doing this:

        - "set +e -vx"
        - "PY=$(find -name '*.py' | grep .)"
        - |
          if [[ "${PY}" ]]; then
              PY=0
          else
              PY=1
          fi
    

    It made it longer, but @KamilCuk's answer didn't quite satisfy. But thanks!