GoogleCloudPlatform / cloud-builders-community

Community-contributed images for Google Cloud Build
https://cloud.google.com/cloud-build/
Apache License 2.0
1.26k stars 858 forks source link

Cancelot not cancelling builds in progress #386

Open langri-sha opened 4 years ago

langri-sha commented 4 years ago

Affected builder image

gcr.io/cloud-builders-community/cancelot

Expected Behavior

Cancelot should be cancelling running builds.

Actual Behavior

The filters are not matching any running builds, even though some are currently in progress.

Steps to Reproduce the Problem

  1. Create a trigger for a GH repository matching any branch
  2. Add the following build step:
  - name: 'gcr.io/$PROJECT_ID/cloud-builders/cancelot'
    id: 'Cancelot'
    args: [
      '--current_build_id', '$BUILD_ID',
      '--branch_name', '$BRANCH_NAME',
      '--same_trigger_only'
    ]
  1. Push a change to a branch
  2. Wait for the build to change status to WORKING
  3. Push a change to the same branch
  4. Once Cancelot runs subsequently, the filters do not return any matching builds that are in progress

Additional Info

Step output for initial build:

2020/03/29 10:33:38 Starting cancelot
2020/03/29 10:33:38 Going to fetch current build details for: 338be4dd-2763-425e-9463-73d04fc923e7
2020/03/29 10:33:38 Going to check ongoing jobs for branch: fix/use-day-arithmetic-for-dst-robustness
2020/03/29 10:33:38 Builds filter created: 

        build_id != "338be4dd-2763-425e-9463-73d04fc923e7" AND 
        source.repo_source.branch_name = "fix/use-day-arithmetic-for-dst-robustness" AND 
        status = "WORKING" AND 
        start_time<"2020-03-29T10:33:15.545550039Z" AND
        trigger_id = "9e080861-0f43-4fbe-88f3-3123a45394dc"
2020/03/29 10:33:39 Ongoing builds triggered by 9e080861-0f43-4fbe-88f3-3123a45394dc for fix/use-day-arithmetic-for-dst-robustness has size of: 0

Step output for subsequent build:

2020/03/29 10:31:51 Starting cancelot
2020/03/29 10:31:51 Going to fetch current build details for: c8f63287-7f94-4527-9ee4-d5181675a8b8
2020/03/29 10:31:51 Going to check ongoing jobs for branch: fix/use-day-arithmetic-for-dst-robustness
2020/03/29 10:31:51 Builds filter created: 

        build_id != "c8f63287-7f94-4527-9ee4-d5181675a8b8" AND 
        source.repo_source.branch_name = "fix/use-day-arithmetic-for-dst-robustness" AND 
        status = "WORKING" AND 
        start_time<"2020-03-29T10:31:29.481194096Z" AND
        trigger_id = "9e080861-0f43-4fbe-88f3-3123a45394dc"
2020/03/29 10:31:52 Ongoing builds triggered by 9e080861-0f43-4fbe-88f3-3123a45394dc for fix/use-day-arithmetic-for-dst-robustness has size of: 0
siberex commented 4 years ago

Looks like something is wrong with filters here, so cloudbuild SDK returns empty list for ongoing builds.

Probably API was changed since then, and branch name is now available in Source.RepoSource.Revision.BranchName property: https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values#using_default_substitutions

Also this will not work with GitHub App builds (because they in fact are sourced from storage bucket).

I am using simple bash script instead of cancelot to cancel previous cloud builds, it bypasses those issues. Also it fetches target branch from current build id and works for Github-sourced builds (filtering by substitutions.BRANCH_NAME).

Adapted version to mimic cancelot args is here: https://gist.github.com/siberex/bb0540b208019382d08732cc6dd59007

Usage:

steps:
  - id: 'cancel previous builds'
    name: 'gcr.io/cloud-builders/gcloud-slim:latest'
    entrypoint: 'bash'
    args: ['path/to/cancelot.sh', '--current_build_id', '$BUILD_ID', '--same_trigger_only']
derekperkins commented 3 years ago

Is this still a problem?

kelly-tock commented 2 years ago

yes, it still does not work.

DmitryBara commented 1 year ago

Does your cloud build trigger configured in global region? Otherwise such builders as slackbot, cancelot could not see information about your build