A GitHub Action that manages build numbers across repositories with automatic incrementing and persistence.
- 🔢 Safe Build Number Management: Automatically increments build numbers only on successful builds
- 💾 Persistent Storage: Stores build numbers in your repository using a JSON file
- 🔄 Atomic Operations: Ensures build numbers are always incremented safely
- 🏷️ Multiple Identifiers: Support for multiple build number sequences (e.g., staging, production, feature branches)
- 📝 Post-Success Commits: Only commits incremented numbers after successful workflow completion
- 🛡️ Failure Protection: Failed builds don't consume build numbers (similar to actions/cache pattern)
name: Build and Deploy
on:
push:
branches: [staging]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get Build Number
id: build_number
uses: moonshot-partners/build-number-manager@v1.0.2
with:
id: staging-branch
initial_number: 20
gh_repo: moonshot-partners/build-manager # repo where the action will store the state
# Optional: Use Personal Access Token instead of default github.token
# github_token: ${{ secrets.MY_GITHUB_PAT }}
# Optional: Only increment after successful completion (default: true)
# only_increment_after_finish: true
- name: Use Build Number
run: |
echo "Build Number: ${{ steps.build_number.outputs.build_number }}"
echo "Previous Number: ${{ steps.build_number.outputs.previous_number }}"name: Build and Deploy
on:
push:
branches: [production]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get Build Number
id: build_number
uses: moonshot-partners/build-number-manager@v1.0.2
with:
id: production-branch
initial_number: 100
gh_repo: moonshot-partners/build-manager
only_increment_after_finish: false # Increment immediately, don't wait for success
- name: Use Build Number
run: |
echo "Build Number: ${{ steps.build_number.outputs.build_number }}"
echo "Previous Number: ${{ steps.build_number.outputs.previous_number }}"| Input | Description | Required | Default |
|---|---|---|---|
id |
Unique identifier for the build number (e.g., staging-branch, production) |
✅ | - |
initial_number |
Initial build number if the ID doesn't exist | ✅ | 1 |
gh_repo |
GitHub repository in format owner/repo |
✅ | - |
github_token |
GitHub token for repository access. Use Personal Access Token if you need custom permissions | ❌ | ${{ github.token }} |
only_increment_after_finish |
If true, only increment build number after successful workflow completion. If false, increment immediately |
❌ | true |
| Output | Description |
|---|---|
build_number |
The incremented build number |
previous_number |
The previous build number before increment |
When using the action for the first time with a new id:
previous_number: Will be set to theinitial_numbervaluebuild_number: Will be set toinitial_number + 1
Example: If you set initial_number: 50 for a new ID:
- First run:
previous_number = 50,build_number = 51 - Second run:
previous_number = 51,build_number = 52 - And so on...
This ensures that the first actual build uses initial_number + 1, while still providing a meaningful previous number for reference.
- Initialization: When first run with a new
id, creates a build numbers file at.github/build-numbers.json - Retrieval: Reads the current build number for the specified
id - Increment: Calculates next number (current + 1) and outputs it
- Build Process: Your workflow uses the build number for building, testing, deploying
- Post Action: ONLY if all workflow steps succeed, commits the incremented number to the repository
- Failure Protection: If any step fails, the build number remains unchanged in the repository
- Initialization: When first run with a new
id, creates a build numbers file at.github/build-numbers.json - Retrieval & Increment: Reads the current build number, increments it, and immediately commits to repository
- Build Process: Your workflow uses the incremented build number for building, testing, deploying
- No Post Action: Build number is already saved, so no post-action is needed
The action stores build numbers in .github/build-numbers.json:
{
"staging-branch": 25,
"production": 150,
"feature-auth": 5
}- Node.js 20+
- npm or yarn
# Install dependencies
npm install
# Build the action
npm run build
# Run linting
npm run lint
# Format code
npm run format# Build and package the action
npm run packageThis creates a dist/ directory with the compiled action ready for use.
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Run
npm run buildto compile - Submit a pull request
For issues and questions, please use the GitHub Issues page.