Home > Software engineering >  How to setup PATH to sh script in gitlab pipeline?
How to setup PATH to sh script in gitlab pipeline?

Time:02-01

I have a bash script plan.sh that executes multiple times during the pipeline execution.

plan:
  before_script:
   - chmod  x ${CI_PROJECT_DIR}/gitlab/deploy/plan/plan.sh     
  script:    
    - cd ${FOLDER1}
    - ${CI_PROJECT_DIR}/gitlab/deploy/plan/plan.sh
    
    - cd ${FOLDER2}    
    - ${CI_PROJECT_DIR}/gitlab/deploy/plan/plan.sh

    - cd ${FOLDER3}
    - ${CI_PROJECT_DIR}/gitlab/deploy/plan/plan.sh

I would like to execute script using just a file name instead of full path. So I am setting the PATH environment variable.

plan:
  before_script:
   - export PATH=${CI_PROJECT_DIR}/gitlab/deploy/plan:$PATH
   - chmod  x plan.sh     
  script:    
    - cd ${FOLDER1}
    - plan.sh
    
    - cd ${FOLDER2}    
    - plan.sh

    - cd ${FOLDER3}
    - plan.sh

However this throws error

$ chmod x plan.sh
chmod: cannot access 'plan.sh': No such file or directory

CodePudding user response:

PATH is used to look up the location of binaries when executing them by their name only.

PATH is not used to resolve arbitrary file arguments passed to another binary. For chmod this means that you still have to provide the full path (relative or absolute) or cd to the correct directory first.

Here's another example that does/not work:

PATH=/bin
cd /

ls bin/ls       # works
ls /bin/ls      # works
bin/ls bin/ls   # works
bin/ls /bin/ls  # works
/bin/ls /bin/ls # works

/bin/ls ls      # error
bin/ls ls       # error
ls ls           # error
  • Related