A specialized GitHub Action that creates a new branch in a specified repository, based on an existing branch. This GitHub Action is useful for teams who want to automate the creation of feature, hotfix, or release branches as part of a Continuous Integration process, especially when the target repository is different from the one running the workflow.
- Branch Creation Automation: Programmatically creates a new Git branch based on a specified source branch.
- Cross-Repository Support: Can operate on any repository for which the provided GitHub Token has the necessary permissions.
- Secure API Interaction: Uses the official GitHub Octokit client (Node.js) for secure interaction with the GitHub Git Data API.
- Simple Integration: One-step usage in any workflow.
- Your workflow must pass all required inputs to this action (
owner,repo,base-branch,new-branch). - The action requires a GitHub Token with
writepermissions for the repository'scontents. For creating branches in the current repository,${{ github.token }}usually suffices if the job has explicitcontents: writepermissions. For external repositories, a PAT (Personal Access Token) is often required.
name: Spawn New Release Branch
on:
workflow_dispatch # Manually trigger the branch creation
jobs:
create_branch:
runs-on: ubuntu-latest
permissions:
# **ATTENTION**: The token needs 'contents: write' permissions.
contents: write # Required for creating branches/references
steps:
- name: Create 'release/v1.2.0' branch from 'main'
uses: ws2git/branch-spawn@v1
with:
# **ADJUST INPUTS HERE**
owner: ${{ github.repository_owner }} # Or a specific organization/user name
repo: ${{ github.event.repository.name }} # Or a specific repository name
base-branch: main
new-branch: release/v1.2.0
github-token: ${{ github.token }} # Use the default token (or a PAT secret for cross-repo access)| Name | Required | Description |
|---|---|---|
owner |
Yes | The owner (user or organization) of the repository where the branch will be created. |
repo |
Yes | The name of the target repository. |
base-branch |
Yes | The existing branch name to create the new branch from (e.g., main or develop). |
new-branch |
Yes | The name of the new branch to be created (e.g., feature/my-task). |
github-token |
Yes | The GitHub Token with contents: write permissions for the target repository. |
Internally, this action runs a Node.js script using the official GitHub Octokit client to interact with the Git Data API.
Octokit Logic (High Level):
- Get Base Branch SHA: Calls
octokit.rest.git.getRefforrefs/heads/${base-branch}to retrieve the SHA of the latest commit on the source branch. - Create New Reference: Calls
octokit.rest.git.createRefusing the retrieved SHA to create a new reference (branch) atrefs/heads/${new-branch}. - Output: Sets an output
success-messageupon successful creation.
If any required parameter is missing or the token lacks the necessary permissions, the action will fail and exit with an error.
This Action requires a GitHub Token to authenticate with the GitHub API via Octokit. This token must have the contents: write permission on the target repository to create a new reference (branch).
Recommended for the current repository: Use the default token ${{ github.token }} combined with explicit job permissions:
permissions:
contents: write
# ...
with:
github-token: ${{ github.token }}Advanced Operations/External Repositories: If you need to create a branch in a repository different from the one running the workflow, or if contents: write is not sufficient, pass a PAT (Personal Access Token) stored as a Secret:
with:
github-token: ${{ secrets.MY_PAT_SECRET }}Never expose the PAT in plain text.
- Always ensure the token passed via the
github-tokeninput has the appropriate write permissions for the target repository defined by theownerandrepoinputs. - For cross-repository operations, a Personal Access Token (PAT) is typically necessary, as
${{ github.token }}is limited to the repository where the workflow is running.
If you find a bug or have a question, open an issue.