Giter Club home page Giter Club logo

gh-action-mutex's Introduction

gh-action-mutex

A simple locking/unlocking mechanism to provide mutual exclusion in Github Actions

Getting started

To prevent concurrent access to a job:

jobs:
  run_in_mutex:
    runs-on: ubuntu-latest
    name: Simple mutex test
    steps:
      - uses: actions/checkout@v3
      - name: Set up mutex
        uses: ben-z/[email protected]
      - run: |
          echo "I am protected!"
          sleep 5

By default, the gh-mutex branch in the current repo is used to store the state of locks.

To have multiple mutexes, simply specify the branch argument in the workflow config:

jobs:
  two_clients_test_client_1:
    runs-on: ubuntu-latest
    name: Two clients test (client 1)
    needs: [simple_test]
    steps:
      - uses: actions/checkout@v3
      - name: Set up mutex
        uses: ben-z/[email protected]
        with:
          branch: another-mutex
      - run: |
          echo "I am protected by the 'another-mutex' mutex!"
          sleep 5

More options such as using a different repo to store the mutex (which allows sharing a mutex between jobs from arbitrary repos) or using different access tokens can be found in action.yml.

Motivation

GitHub Action has the concurrency option for preventing running multiple jobs concurrently. However, it has a queue of length 1. When multiple jobs with the same concurrency group get queued, only the currently running job and the latest queued job are kept. Other jobs are simply cancelled. There's more discussion here and it appears that GitHub does not want to add the requested cancel-pending feature any time soon (as of 2022-03-26). This GitHub action solves that issue.

Implementation Details

Mutexes are implemented using simple spinlocks. The test-and-set functionality is provided by Git, where a git push can only succeed if the commit to be pushed is a fast-forward of what's on the remote.

Developing Locally

  1. Install act
  2. Populate .github-token with a personal access token with the repo permision.
  3. act --rebuild -v -s GITHUB_TOKEN=$(cat .github-token)

Inspirations

gh-action-mutex's People

Contributors

ben-z avatar psyhomb avatar bbednarek avatar chamini2 avatar moser-ss avatar

Stargazers

Sava Simic 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.