当我尝试将 kubectl 补丁部署到 Gitlab 时 Yaml 无效

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

我需要在安装过程中通过 .gitlab-ci.yml 修补我的 Jupyterhub 安装。当我将我的分支推送到 giblab 时,我得到 "yaml invalid"。当我从命令行 运行 它时,该脚本有效,但当我从 .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"]}]'

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

这是因为该命令包含一个未加引号的 :,YAML 将其视为映射值指示符。您可以使用折叠块标量,它确实按字面解释所有包含的字符,但将换行符折叠成空格(也有助于提高可读性):

- >-
  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"]}]'

您只能在单个空格上拆分行,并且不能进一步缩进后续行。