bashgitlab-ciopen-policy-agentconftest

Calling a bash script from Gitlab CI


I have no ideas what I am doing wrong. Just running this simple bash script on my pipeline. It also does not work when I test it locally. Same error.

refresh.sh

#!/bin/sh
# refresh.sh
# Create a kustomization.yaml and then add all kubernetes YAMLs into resources...
echo -n "
# built by ./refresh.sh
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
" > kustomization.yaml

yamls=$(find . -type f -name "*.yaml" | grep -v kustomization.yaml)

for y in ${yamls[@]}; do
  kustomize edit add resource $y
done

Error:

./refresh.sh: line 16: syntax error: bad substitution

my .gitlab-ci.yml

image:
  name: openpolicyagent/conftest:latest

stages:
  - kustomize
  - policy

kustomize:
  stage: kustomize
  script:
    - echo "Running test 1..."
    - chmod +x refresh.sh
    - ./refresh.sh

policy:
  stage: policy
  script:
    - echo "Running test 2"
    - kubectl kustomize | conftest test -
    - rm kustomization.yaml

Also I can see my script listed on my current directory on the logs when I add ls -al in the pipeline kustomize stage so not sure what the issue is:

$ ls -al
total 24
drwxrwxrwx    8 root     root           167 May  2 18:11 .
drwxrwxrwx    4 root     root            44 May  2 18:11 ..
drwxrwxrwx    6 root     root           128 May  2 18:11 .git
-rw-rw-rw-    1 root     root          1020 May  2 18:11 .gitlab-ci.yml
-rw-rw-rw-    1 root     root          9140 May  2 18:11 README.md
drwxrwxrwx    2 root     root            76 May  2 18:11 policy
-rwxrwxrwx    1 root     root           375 May  2 18:11 refresh.sh
drwxrwxrwx    4 root     root            38 May  2 18:11 staging

Thanks


Solution

  • Your first error (file not found) was probably caused by bash not being available in the openpolicyagent/conftest:latest container. When you changed the interpreter to /bin/sh the error now happens because of this line:

    for y in ${yamls[@]}; do
    

    POSIX sh doesn't support array references. And $yamls is not an array anyway, so you probably should use just $yamls instead of ${yamls[@]}.