首页 > 解决方案 > curl 命令导致 bitbucket CI/CD 管道 YAML 中的缩进错误

问题描述

当我部署到生产环境时,我试图在 bitbucket 上向我的 CI/CD YAML 添加一个curl命令。该curl命令向 CloudFlare API 发送 POST 请求以清除缓存。该命令包括-H必要的 HTTP 标头变量。这会导致 bitbucket 出现缩进错误,因此我无法提交更改,我不知道为什么。我不熟悉 YAML 语法以及我应该如何解决这个问题。

位桶管道.YAML

image: python:3.7.4

clone:
 depth: full

pipelines:
   default:
     - step:
        caches:
          - pip
        script:
          - echo "nothing"
   branches:
      prod:
      - step:
          name: Deploy to Staging
          deployment: staging
          script: #staging script
           - git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_STAGING_APP_NAME.git HEAD:master --force
      - step:
          name: Deploy to Production
          deployment: production
          trigger: manual
          script: #production script
           - git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git HEAD:master --force
           - "curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE\purge_cache" -H "X-Auth-Email: $CLOUDFLARE_AUTH_EMAIL" \
          -H "X-Auth-Key: $CLOUDFLARE_AUTH_KEY" \
          -H "Content-Type: application/json" --data '{"purge_everything":true}'"

标签: yamlsyntax-errorbitbucketcloudflare

解决方案


您需要转义命令中的双引号。此外,-H续行的缩进不够远——它们必须比标量开始行上的列表项指示符缩进更多。

更好的方法是使用折叠块标量:

        - >-
          curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE\purge_cache"
          -H "X-Auth-Email: $CLOUDFLARE_AUTH_EMAIL"
          -H "X-Auth-Key: $CLOUDFLARE_AUTH_KEY"
          -H "Content-Type: application/json" --data '{"purge_everything":true}'

折叠块标量将换行符折叠成空格并且不处理任何特殊字符,因此您不需要转义任何内容(实际上,块标量中没有转义序列)。


推荐阅读