Skip to content

Instantly share code, notes, and snippets.

@MinhOmega
Created August 20, 2024 03:53
Show Gist options
  • Select an option

  • Save MinhOmega/1691a20900a75ecdcd97584cc223751c to your computer and use it in GitHub Desktop.

Select an option

Save MinhOmega/1691a20900a75ecdcd97584cc223751c to your computer and use it in GitHub Desktop.
name: Bulk Commit With GitHub API
on:
workflow_dispatch:
env:
NUMBER_OF_COMMITS: 20000 # Total number of commits to create
BATCH_SIZE: 100 # Number of commits to push in each batch
USER_NAME: '' # Global Git user name
USER_EMAIL: '' # Global Git user email
WORKFLOW_FILE_NAME: '' # Workflow file to trigger
jobs:
create-commits:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.PAT }}
persist-credentials: false
- name: Configure Git
run: |
git config --global user.name ${{ env.USER_NAME }}
git config --global user.email ${{ env.USER_EMAIL }}
- name: Get commit count using GitHub API
id: commit_count
run: |
repo_name=${{ github.repository }}
# Get the total number of commits using the GitHub API and handle pagination
last_page=$(curl -s -I -H "Authorization: token ${{ secrets.PAT }}" \
"https://api.github.com/repos/${repo_name}/commits?per_page=1" \
| grep -i 'link:' | sed -n 's/.*page=\([0-9]*\)>; rel="last".*/\1/p')
total_commits=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \
"https://api.github.com/repos/${repo_name}/commits?per_page=1&page=$last_page" \
| jq length)
total_commits=$(( (last_page - 1) * 1 + total_commits ))
echo "::set-output name=total_commits::$total_commits"
- name: Create commits based on total commits
run: |
total_commits=${{ steps.commit_count.outputs.total_commits }}
# Create commits in batches
actual_commits_created=0
for ((i=1; i<=${{ env.NUMBER_OF_COMMITS }}; i++)); do
total_commits=$((total_commits + 1))
actual_commits_created=$((actual_commits_created + 1))
git commit --allow-empty -m "Empty commit $total_commits"
if ((i % ${{ env.BATCH_SIZE }} == 0)); then
git push https://${{ secrets.PAT }}@github.com/${{ github.repository }}.git HEAD:main
fi
done
# Push any remaining commits
remaining_commits=(${{ env.NUMBER_OF_COMMITS }} % ${{ env.BATCH_SIZE }})
if ((remaining_commits > 0)); then
git push https://${{ secrets.PAT }}@github.com/${{ github.repository }}.git HEAD:main
fi
echo "actual_commits_created=$actual_commits_created" >> $GITHUB_ENV
- name: Trigger Bulk Projects Creation workflow
run: |
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ secrets.PAT }}" \
https://api.github.com/repos/${{ github.repository }}/actions/workflows/${{ env.WORKFLOW_FILE_NAME }}/dispatches \
-d '{"ref":"main"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment