GCP cloudbuild.yaml条件步骤错误 [英] GCP cloudbuild.yaml conditional step error

查看:133
本文介绍了GCP cloudbuild.yaml条件步骤错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的云构建文件

substitutions:
        _CLOUDSDK_COMPUTE_ZONE: us-central1-a 
        _CLOUDSDK_CONTAINER_CLUSTER: $_CLOUDSDK_CONTAINER_CLUSTER
    steps:
    - name: gcr.io/$PROJECT_ID/sonar-scanner:latest
      entrypoint: 'bash'
      args:
      - '-c'
      - 'if [ $BRANCH_NAME != 'production' ]; then sonar-scanner -Dsonar.host.url=https://sonar.test.io -Dsonar.login=${_SONAR_LOGIN} -Dsonar.projectKey=service -Dsonar.sources=. ; fi'
    - id: 'build test-service image'
      name: 'gcr.io/cloud-builders/docker'
      args: ['build', '-t', 'gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA', '.']
    - id: 'push test-service image'
      name: 'gcr.io/cloud-builders/docker'
      args: ['push', 'gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA']
    - id: 'set test-service image in yamls'
      name: 'ubuntu'
      args: ['bash','-c','sed -i "s,TEST_SERVICE,gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA," k8s/*.yaml']
    - id: kubectl-apply
      name: 'gcr.io/cloud-builders/kubectl'
      args: ['apply', '-f', 'k8s/']
      env:
      - 'CLOUDSDK_COMPUTE_ZONE=${_CLOUDSDK_COMPUTE_ZONE}'
      - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLOUDSDK_CONTAINER_CLUSTER}'
    images: ['gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA']

我遇到错误

无法解组的构建配置cloudbuild.yaml:yaml:第17行:确实 找不到预期的密钥

failed unmarshalling build config cloudbuild.yaml: yaml: line 17: did not find expected key

更新1

根据@cloudomation建议(如果条件已更新)

As per @cloudomation suggestion updated if condition

- 'if [ $BRANCH_NAME != "production" ]; then sonar-scanner -Dsonar.host.url=https://sonar.test.io -Dsonar.login=${_SONAR_LOGIN} -Dsonar.projectKey=service -Dsonar.sources=. ; fi'

条件正常,但确实出现此错误

Condition is working but when it's true getting this error

Step #1: Digest: sha256:ef0de1c8e48544b9693b9aab2222bf849028bb66881762bf77e055b0abbf7f2b Step #1: Status: Downloaded newer image for gcr.io/wotnot-235414/sonar-scanner:latest Step #1: gcr.io/project-235414/sonar-scanner:latest Step #1: /opt/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner: exec: line 59: /opt/sonar-scanner-3.2.0.1227-linux/jre/bin/java: not found Finished Step #1 ERROR ERROR: build step 1 "gcr.io/project-235414/sonar-scanner:latest" failed: exit status 127

但是当我的脚步像这样时,它就可以很好地工作了

but when my step is like this way it's working perfectly

- name: gcr.io/$PROJECT_ID/sonar-scanner:latest
  args:
    - '-Dsonar.host.url=https://sonar.test.io'
    - '-Dsonar.login=XXXXXXXXXXXXXX'
    - '-Dsonar.projectKey=service-name'
    - '-Dsonar.sources=.'

这也正在运行,这意味着生成器映像中没有问题,只是传递参数的问题

also this is running that mean there no issue in builder image it's just issue of passing parameters

docker run gcr.io/$PROJECT_ID/sonar-scanner:latest bash -Dsonar.host.url=https://sonar.test.io -Dsonar.login=${_SONAR_LOGIN} -Dsonar.projectKey=service -Dsonar.sources=.

如果条件正常,但之后又出现问题

推荐答案

您应避免使用引号:

  - 'if [ $BRANCH_NAME != \'production\' ]; then sonar-scanner -Dsonar.host.url=https://sonar.test.io -Dsonar.login=${_SONAR_LOGIN} -Dsonar.projectKey=service -Dsonar.sources=. ; fi'

或使用双引号:

  - 'if [ $BRANCH_NAME != "production" ]; then sonar-scanner -Dsonar.host.url=https://sonar.test.io -Dsonar.login=${_SONAR_LOGIN} -Dsonar.projectKey=service -Dsonar.sources=. ; fi'

这篇关于GCP cloudbuild.yaml条件步骤错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆