GCP cloudbuild.yaml conditional step error

冷暖自知 提交于 2019-12-13 02:49:21

问题


This is my cloud build file

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

I am getting error

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

Update 1

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 condition working but after that then condition having some issue


回答1:


You should escape the quotes:

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

or use double quotes:

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


来源:https://stackoverflow.com/questions/58502210/gcp-cloudbuild-yaml-conditional-step-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!