0

I am having trouble with dynamically passing one of two file based variables to a job.

I have defined two file variables in my CI/CD settings that contain my helm values for deployments to developement and production clusters. They are typical yaml syntax, their content does not really matter.

baz:
  foo: bar

I have also defined two jobs for the deployment that depend on a general deployment template .deploy.

.deploy:
  variables:
    DEPLOYMENT_NAME: ""
    HELM_CHART_NAME: ""
    HELM_VALUES: ""

  before_script:
    - kubectl ...
  script: 
    - helm upgrade $DEPLOYMENT_NAME charts/$HELM_CHART_NAME
      --install
      --atomic
      --debug
      -f $HELM_VALUES

The specialization happens in two jobs, one for dev and one for prod.

deploy:dev:
  extends: .deploy
  variables:
    DEPLOYMENT_NAME: my-deployment
    HELM_CHART_NAME: my-dev-chart
    HELM_VALUES: $DEV_HELM_VALUES # from CI/CD variables

deploy:prod:
  extends: .deploy
  variables:
    DEPLOYMENT_NAME: my-deployment
    HELM_CHART_NAME: my-prod-chart
    HELM_VALUES: $PROD_HELM_VALUES # from CI/CD variables

The command that fails is the one in the script tag of .deploy. If I pass in the $DEV_HELM_VALUES or $PROD_HELM_VALUES, the deployment is triggered. However if I put in the $HELM_VALUES as described above, the command fails (Error: "helm upgrade" requires 2 arguments, which is very misleading).

The problem is that the $HELM_VALUES that are accessed in the command are already the resolved content of the file, whereas passing the $DEV_HELM_VALUES or the $PROD_HELM_VALUES directly works with the -f syntax.

This can be seen using echo in the job's output:

echo "$DEV_HELM_VALUES"
/builds/my-company/my-deployment.tmp/DEV_HELM_VALUES
echo "$HELM_VALUES"
baz:
  foo: bar

How can I make sure the $HELM_VALUES only point to one of the files, and do not contain the files' content?

Flo Win
  • 154
  • 10
  • How about using [dynamic bash variables](https://stackoverflow.com/questions/16553089/dynamic-variable-names-in-bash)? – federicober May 24 '22 at 14:31
  • 1
    Your test `echo "$DEV_HELM_VALUES"` indicates that `DEV_HELM_VALUES` is a regular gitlab-ci variable, not a gitlab-ci file variable, while the beginning of your question seems to indicate that `DEV_HELM_VALUES` should be a file variable. Can you clarify? – ErikMD May 24 '22 at 23:17
  • @ErikMD sorry, I messed up when copying together the values. Indeed `$DEV_HELM_VALUES` is the file variable, and `$HELM_VALUES` contains the resolved file content. – Flo Win May 25 '22 at 14:44
  • That still does not make sense from my viewpoint because if you have `HELM_VALUES: $DEV_HELM_VALUES` in your YAML, `echo "$HELM_VALUES"` should return `/builds/my-company/my-deployment.tmp/DEV_HELM_VALUES` I believe (unlike `cat "$HELM_VALUES"` of course). But admittedly, I didn't have the time yet to reproduce a minimal-working-example in a fresh repo… – ErikMD May 25 '22 at 19:43
  • 1
    BTW even if this is GitLab CI code, that's still mostly shell code, so don't forget to double-quote all your shell variables → `-f "$HELM_VALUES"` – ErikMD May 25 '22 at 19:45

0 Answers0