Skip to content

branch-gg/github-action-workflow-lock

Repository files navigation

Usage

Prerequisites

  1. GitHub Repository: The action is intended to be used within a GitHub repository.
  2. Lock Branch: A branch in your repository (e.g., locks) where the lock file will be stored.
  3. Personal Access Token (PAT): A GitHub PAT with repo scope stored as a secret (e.g., PAT_TOKEN).

Basic Example

  1. Ensure the Lock Branch Exists

    Create a branch named locks in your repository. This branch will store the lock file and should not trigger workflows.

  2. Configure Workflow Triggers

    Make sure your workflow triggers ignore the lock branch:

on:
  push:
    ...
    branches-ignore:
      - 'locks'
  pull_request:
    ...
    branches-ignore:
      - 'locks'
  1. Use the Action in Your Workflow Create or update your workflow file (e.g., .github/workflows/build.yml):
name: Build with Distributed Lock

on:
  workflow_dispatch:
  push:
    branches:
      - main
    branches-ignore:
      - 'locks'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Acquire Lock
        id: acquire-lock
        uses: your-username/distributed-lock-action@v1
        with:
          github-token: ${{ secrets.PAT_TOKEN }}
          lock-file-path: 'locks/lock.json'
          lock-branch: 'locks'
          lock-key: 'build-lock'
          max-concurrent: '1'
          polling-interval: '10'
          mode: 'acquire'

      - name: Critical Section
        run: |
          echo "Executing critical section..."

      - name: Release Lock
        if: always()
        uses: your-username/distributed-lock-action@v1
        with:
          github-token: ${{ secrets.PAT_TOKEN }}
          lock-file-path: 'locks/lock.json'
          lock-branch: 'locks'
          lock-key: 'build-lock'
          mode: 'release'

      - name: Downstream Job
        run: |
          echo "Continuing with downstream tasks..."

Parameters:

  • github-token: (Required) GitHub token with repo scope. Use a PAT stored in secrets.
  • lock-file-path: (Required) Path to the lock file in the repository (e.g., locks/lock.json).
  • lock-branch: (Optional) Branch where the lock file is stored. Default is locks.
  • lock-key: (Required) Unique key for the lock (e.g., build-lock).
  • max-concurrent: (Optional) Maximum number of concurrent workflows allowed.
  • polling-interval: (Optional) Time in seconds between retries. Default is 10.
  • mode: (Optional) Action mode: acquire or release. Default is acquire.

Contributing

Initial Setup

After you've cloned the repository to your local machine or codespace, you'll need to perform some initial setup steps before you can develop your action.

Note

You'll need to have a reasonably modern version of Node.js handy (20.x or later should work!). If you are using a version manager like nodenv or nvm, this template has a .node-version file at the root of the repository that will be used to automatically switch to the correct version when you cd into the repository. Additionally, this .node-version file is used by GitHub Actions in any actions/setup-node actions.

  1. 🛠️ Install the dependencies

    npm install
  2. 🏗️ Package the TypeScript for distribution

    npm run bundle
  3. ✅ Run the tests

    $ npm test
    
    PASS  ./index.test.js
      ✓ ... (nms)
    
    ...

Releasing the Action Code

The src/ directory is the heart of your action! This contains the source code that will be run when your action is invoked. You can replace the contents of this directory with your own code.

There are a few things to keep in mind when writing your action code:

  • Most GitHub Actions toolkit and CI/CD operations are processed asynchronously. In main.ts, you will see that the action is run in an async function.

    import * as core from '@actions/core';
    //...
    
    async function run() {
      try {
        //...
      } catch (error) {
        core.setFailed(error.message);
      }
    }

    For more information about the GitHub Actions toolkit, see the documentation.

  1. Create a new branch

    git checkout -b releases/v1
  2. Replace the contents of src/ with your action code

  3. Add tests to __tests__/ for your source code

  4. Format, test, and build the action

    npm run all

    This step is important! It will run ncc to build the final JavaScript action code with all dependencies included. If you do not run this step, your action will not work correctly when it is used in a workflow. This step also includes the --license option for ncc, which will create a license file for all of the production node modules used in your project.

  5. Commit your changes

    git add .
    git commit -m "My first action is ready!"
  6. Push them to your repository

    git push -u origin releases/v1
  7. Create a pull request and get feedback on your action

  8. Merge the pull request into the main branch

Your action is now published! 🚀

For information about versioning your action, see Versioning in the GitHub Actions toolkit.

Validate the Action

You can now validate the action by referencing it in a workflow file. For example, ci.yml demonstrates how to reference an action in the same repository.

steps:
  - name: Checkout
    id: checkout
    uses: actions/checkout@v4

  - name: Test Local Action
    id: test-action
    uses: ./
    with:
      milliseconds: 1000

  - name: Print Output
    id: output
    run: echo "${{ steps.test-action.outputs.time }}"

For example workflow runs, check out the Actions tab! 🚀

Usage

After testing, you can create version tag(s) that developers can use to reference different stable versions of your action. For more information, see Versioning in the GitHub Actions toolkit.

To include the action in a workflow in another repository, you can use the uses syntax with the @ symbol to reference a specific branch, tag, or commit hash.

steps:
  - name: Checkout
    id: checkout
    uses: actions/checkout@v4

  - name: Test Local Action
    id: test-action
    uses: actions/typescript-action@v1 # Commit with the `v1` tag
    with:
      milliseconds: 1000

  - name: Print Output
    id: output
    run: echo "${{ steps.test-action.outputs.time }}"

Publishing a New Release

This project includes a helper script, script/release designed to streamline the process of tagging and pushing new releases for GitHub Actions.

GitHub Actions allows users to select a specific version of the action to use, based on release tags. This script simplifies this process by performing the following steps:

  1. Retrieving the latest release tag: The script starts by fetching the most recent SemVer release tag of the current branch, by looking at the local data available in your repository.
  2. Prompting for a new release tag: The user is then prompted to enter a new release tag. To assist with this, the script displays the tag retrieved in the previous step, and validates the format of the inputted tag (vX.X.X). The user is also reminded to update the version field in package.json.
  3. Tagging the new release: The script then tags a new release and syncs the separate major tag (e.g. v1, v2) with the new release tag (e.g. v1.0.0, v2.1.2). When the user is creating a new major release, the script auto-detects this and creates a releases/v# branch for the previous major version.
  4. Pushing changes to remote: Finally, the script pushes the necessary commits, tags and branches to the remote repository. From here, you will need to create a new release in GitHub so users can easily reference the new tags in their workflows.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published