Filter build steps by branch google cloud plateform

673 views Asked by At

I have the below steps

steps:
  # This step show the version of Gradle
  - id: Gradle Install
    name: gradle:7.4.2-jdk17-alpine
    entrypoint: gradle
    args: ["--version"]

  # This step build the gradle application
  - id: Build
    name: gradle:7.4.2-jdk17-alpine
    entrypoint: gradle
    args: ["build"]

  # This step run test
  - id: Publish
    name: gradle:7.4.2-jdk17-alpine
    entrypoint: gradle
    args: ["publish"]

The last step I want to do only on MASTER branch

Found one link related to this https://github.com/GoogleCloudPlatform/cloud-builders/issues/138

Its using a bash command, how can I put the gradle command inside the bash.

Update

After the suggestion answer I have updated the steps as

  - id: Publish
    name: gradle:7.4.2-jdk17-alpine
    entrypoint: "bash"
    args:
    - "-c"
    - |
      [[ "$BRANCH_NAME" == "develop" ]] && gradle publish

The build pipeline failed with below exception

Starting Step #2 - "Publish"
Step #2 - "Publish": Already have image: gradle:7.4.2-jdk17-alpine
Finished Step #2 - "Publish"
ERROR
ERROR: build step 2 "gradle:7.4.2-jdk17-alpine" failed: starting step container failed: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown
2

There are 2 answers

0
San Jaisy On

The suggested solution didn't work for me, I have to write the below code

  # This step run test
  - id: Publish
    name: gradle:7.4.2-jdk17-alpine
    entrypoint: "sh"
    args:
      - -c
      - |
        if [ $BRANCH_NAME == 'master' ]
        then
          echo "Branch is = $BRANCH_NAME"
          gradle publish
        fi
1
Divyani Yadav On

Current workarounds are mentioned as following :

  1. Using different cloudbuild.yaml files for each branch
  2. Overriding entrypoint and injecting bash as mentioned in the link:

steps:

- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    echo "Here's a convenient pattern to use for embedding shell scripts in cloudbuild.yaml."
    echo "This step only pushes an image if this build was triggered by a push to master."
    [[ "$BRANCH_NAME" == "master" ]] && docker push gcr.io/$PROJECT_ID/image

This tutorial outlines an alternative where you check in different cloudbuild.yaml files on different development branches.

You can try the following for Gradle command as mentioned by bhito:

- id: Publish 
name: gradle:7.4.2-jdk17-alpine 
entrypoint: sh 
args: - c 
- | 
[[ "$BRANCH_NAME" == "master" ]] && gradle publish

Cloud build provides configuring triggers by branch, tag, and pr . This lets you define different build configs to use for different repo events, e.g. one for prs, another for deploying to prod, etc.you can refer to the documentation on how to create and manage triggers.

you can check this blog for more updates on additional features and can go through the release notes for more Cloud build updates. To gain some more insights on Gradle, you can refer to the link