Coder Social home page Coder Social logo

siberex / cancelot Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 32 KB

Google Cloud Builds: Cancel queued builds on newly submitted build

License: Apache License 2.0

Shell 100.00%
automation ci cloud-build gcloud gcloud-cli gcloud-sdk gcp gcp-cloud-build google-cloud google-cloud-build

cancelot's Introduction

Cancelot

Provides automation for cancelling Google Cloud Builds.

Cancels previously queued builds in the same workflow when new build is submitted.

For example, allows you to cancel builds running for the branch on new commits to this branch.

Aimed to replace cancelot because of issues with it.

Use as a first step to cancel previous builds currently in progress.

Use --same_trigger_only to narrow down to builds triggered by the same trigger id.

Usage (Google Cloud Build)

ℹ️ Tip: Use suitable regional registry to reduce cross-region traffic costs.

For example, eu.gcr.io/google.com/cloudsdktool/google-cloud-cli:alpine for europe-* regions.

Sample trigger config .cloudbuild/pull-request-trigger.yml:

name: pull-request
description: Do some work on each PR
filename: .cloudbuild/pull-request-action.yaml
github:
  name: cancelot
  owner: siberex
  pullRequest:
    branch: ^main$

Sample action config, .cloudbuild/pull-request-action.yaml:

steps:
- id: cancelot
  name: 'gcr.io/google.com/cloudsdktool/google-cloud-cli:alpine'
  entrypoint: bash
  args:
    # To download the latest version (if you trust running random bash scripts from the internets!):
    #- curl -L https://gist.github.com/siberex/bb0540b208019382d08732cc6dd59007/raw -o cancelot.sh && chmod +x cancelot.sh
    - cancelot.sh 
    -  --current_build_id "${BUILD_ID}"
    -  --branch_name "${BRANCH_NAME}"
    -  --same_trigger_only
  env:
    - 'PROJECT_ID=$PROJECT_ID'
    - 'LOCATION=$LOCATION'
- id: do-some-work
  waitFor: ['cancelot']
  name: alpine
  entrypoint: /bin/sh
  args:
    - -c
    - |
      sleep 5s
      echo JOB DONE!

Both CLI ARGS and ENV variables are accepted and are interchangeable:

--current_build_id $BUILD_ID could be replaced with CURRENT_BUILD_ID=$BUILD_ID env.

PROJECT_ID=$PROJECT_ID could be replaced with --project $PROJECT_ID arg.

Arguments took precedence over ENV.

Usage (Other CI)

Script could be used locally or with any CI environment with gcloud installed and authenticated (gcloud auth login).

./cancelot.sh --current_build_id $BUILD_ID --branch_name $BRANCH_NAME [--same_trigger_only] [--project "gcloud-project-id"] [--region "europe-west2"]

You can download the latest version from gist, btw, without cloning the repo (if you trust running random bash scripts from the internets!):

curl -L https://gist.github.com/siberex/bb0540b208019382d08732cc6dd59007/raw -o cancelot.sh && chmod +x cancelot.sh

Run locally with docker and pass gcloud auth config from the host via volume mapping:

docker run -it --rm -e CLOUDSDK_CONFIG=/config/gcloud \
  -v "$HOME/.config/gcloud":/config/gcloud \
  -v "$PWD/cancelot.sh":/cancelot.sh \
  --entrypoint bash \
  gcr.io/google.com/cloudsdktool/google-cloud-cli:alpine \
  cancelot.sh --current_build_id abcdef12-3456-7890-a1b2-c3d4e5f6dead

gcloud SDK official imagesgcr.io/google.com/cloudsdktool/google-cloud-cli:alpine

Cloud Builder gcloud imagegcr.io/cloud-builders/gcloud-slim:latest

cancelot's People

Contributors

siberex avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.