Search code examples
gitlabyamlgitlab-cikubectljupyterhub

Yaml invalid when I try to do a kubectl patch deploy to Gitlab


I need to patch my Jupyterhub installation during installation via .gitlab-ci.yml. When I push my branch to giblab I get "yaml invalid". The script works when I run it from the command line, but not when I run it from .gitlab-ci.yml.

- kubectl patch deploy -n $NAMESPACE hub --type json --patch '[{"op": "replace", "path": "/spec/template/spec/containers/0/command", "value": ["bash", "-c", "\nmkdir -p ~/hotfix\ncp -r /usr/local/lib/python3.6/dist-packages/kubespawner ~/hotfix\nls -R ~/hotfix\npatch ~/hotfix/kubespawner/spawner.py << EOT\n72c72\n<             key=lambda x: x.last_timestamp,\n---\n>             key=lambda x: x.last_timestamp and x.last_timestamp.timestamp() or 0.,\nEOT\n\nPYTHONPATH=$HOME/hotfix jupyterhub --config /srv/jupyterhub_config.py --upgrade-db\n"]}]'

See https://rancher.com/blog/2020/ai-meets-kubernetes


Solution

  • It's because the command contains an unquoted : which YAML treats as mapping value indicator. You can use a folded block scalar which does interpret all contained characters literally but folds linebreaks into spaces (also helps readability):

    - >-
      kubectl patch deploy -n $NAMESPACE hub --type json
      --patch '[{"op": "replace",
      "path": "/spec/template/spec/containers/0/command",
      "value": ["bash", "-c", "\nmkdir -p ~/hotfix\ncp -r
      /usr/local/lib/python3.6/dist-packages/kubespawner ~/hotfix\nls
      -R ~/hotfix\npatch ~/hotfix/kubespawner/spawner.py <<
      EOT\n72c72\n<             key=lambda
      x: x.last_timestamp,\n---\n>             key=lambda x: x.last_timestamp
      and x.last_timestamp.timestamp() or 0.,\nEOT\n\nPYTHONPATH=$HOME/hotfix
      jupyterhub --config /srv/jupyterhub_config.py --upgrade-db\n"]}]'
    

    You can only split lines on single spaces and may not indent following lines more.