An advisory lock service for CI/CD pipelines, implemented as a GitHub Action. It prevents race conditions by ensuring mutual exclusion - only one job can access a shared resource concurrently.
This action uses a PostgreSQL database to manage locks. When a workflow job needs to acquire a lock, it communicates with the lock service. If the lock is available, it's granted, and the job proceeds. If not, the job can wait or fail, depending on your workflow configuration.
- Advisory Locking: Create and manage locks within your GitHub Actions workflows.
- Pull Request Integration: Lock and release events are posted as PR comments.
- Slack Notifications: Choose if you want to be notified in your Slack channels about locking events.
- Easy Disabling: Skip locking for specific pull requests by:
- adding a
SKIP_MUTEXlabel - including
SKIP_MUTEXin the PR's description or comment - or defining
SKIP_MUTEX=1as an environment variable.
- adding a
Here is an example of how to use the mutex action in a workflow:
- name: Acquire Lock
uses: releasetools/mutex@v1
permissions:
contents: read
pull-requests: write
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
with:
command: "lock"
id: "my-resource"
slack-channel: "#ci-cd"Any other workflows or actions using a mutex on the same lock id, will not run until the lock is released.
- A PostgreSQL database: This action requires access to a PostgreSQL database to store lock information. You can use any standard Postgres provider. If you need a free one for getting started, consider using Neon.
The action supports the following environment variables (env:).
Connection string for a PostgreSQL database. The action will create a table named releasetools_mutex if it doesn't exist. If the role specified in the connection string cannot create tables, ensure such a table exists. You can find the schema definition in database.ts.
The action needs access to the GitHub API. It can be passed via ${{ secrets.GITHUB_TOKEN }}. The workflow needs additional permissions:
permissions:
contents: read
pull-requests: writeThe Slack Bot Token for sending notifications. It requires the chat:write permission, and the associated bot must be invited to the specified slack-channel, otherwise it will fail to post.
The action can be configured using inputs (with:).
Required. The command to execute (e.g., lock or release).
Required. A unique identifier for the lock.
Optional reason for taking the lock. Useful to provide context regarding which service took the lock and why.
Lock expiration in seconds from current time. Defaults to 60 seconds in the future.
Maximum time in seconds to wait to acquire the lock, before failing.
If not specified, it defaults to -1 which results in using the specified expiration as a timeout for the current run.
Allows changing the polling interval. Useful for long-duration locks.
Used to signal if a lock should be automatically released when the workflow job ends. Defaults to true.
By default, a comment will be posted on the Pull Request running the action, when locks are acquired or released.
Set it to true to never post comments on PRs.
Required for Slack notifications. The Slack channel to post updates to (e.g., C12345678).
The bot that owns the SLACK_BOT_TOKEN should be a member of this channel.
See Slack API docs for channel ID formats.
All contributions are welcome!
-
Clone the repository:
git clone https://github.com/releasetools/mutex.git cd mutex -
Install dependencies and pre-commit hooks:
npm install npm run prepare
The main entry point is main.ts, which handles 'lock' or 'release' actions. A post-job script in post.ts handles automatic lock release if enabled.
You can learn about creating GitHub actions in this tutorial.
You can use releasetools-cli to create release tags.
Run this command to tag the HEAD commit and also update the v1 tag.
releasetools git::release --major --sign --force --push v1.0.2Since mutex is a Javascript-based action, no other step is needed to make a new release available.
Use the template below to draft new releases. Update the changelog section to include all relevant changes/features/bugfixes.
## Summary
- An advisory lock service for CI/CD pipelines, implemented as a GitHub Action.
- It prevents race conditions by ensuring mutual exclusion - only one job can access a shared resource concurrently.
## Features
- **Advisory Locking**: Create and manage locks within your GitHub Actions workflows.
- **Pull Request Integration**: Lock and release events are posted as PR comments.
- **Slack Notifications**: Choose if you want to be notified on Slack about locking events.
- **Easy Disabling**: Skip locking for specific pull requests by:
- adding a `SKIP_MUTEX` label
- including `SKIP_MUTEX` in the PR's description or comment
- or defining `SKIP_MUTEX=1` as an environment variable.
## Changelog
- TBD.Copyright © 2025 Mihai Bojin
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.