Search code examples
github-actionsgoogle-cloud-build

Github action wait for cloud build to complete


I trigger cloud build from github action (GHA) using gcloud command

  - name: Start CB trigger
    run: |
      export CLOUDSDK_CORE_DISABLE_PROMPTS=1
      gcloud beta builds triggers run iaisudh983u4923 --branch=main

Cloud build gets triggered asynchronously and GHA moves on. I would like GHA to wait for cloud build to finish processing and then based on whether cloud build succeeded or failed - GHA should proceed accordingly.

When I run the same gcloud beta builds triggers command locally - I get the same behavior. Command executes successfully without waiting for cloud build to finish executing.


Solution

  • In following code we trigger cloud build from GHA and then wait for status to be anything other then "QUEUED" or "WORKING"

    To allow GHA to view logs from cloud build - grant role roles/logging.viewer to gha service account. This works only if logs are stored in logging and not in cloud storage bucket in the CB trigger yaml file

    options:
     logging: CLOUD_LOGGING_ONLY
    

    GHA code:

      - name: Start CB trigger to deploy main branch to dev GAE
        run: |
          export CLOUDSDK_CORE_DISABLE_PROMPTS=1
          BUILD_ID=$(gcloud beta builds triggers run ${{ vars.DEPLOY_DJANGO_TRIGGER_ID_DEV_2 }} \
            --branch=$branch --quiet \
            --format="value(metadata.build.id)")
          
          echo "BUILD_ID = $BUILD_ID"
          gcloud beta builds log --stream "$BUILD_ID"
          BUILD_STATUS=$(gcloud builds describe $BUILD_ID --format="value(status)")
          echo "Build status = $BUILD_STATUS"
          
          if [ "$BUILD_STATUS" == "SUCCESS" ]; then
              exit 0
          else
              exit 1
          fi