Skip to content

Instantly share code, notes, and snippets.

@MinhOmega
Last active August 20, 2024 03:44
Show Gist options
  • Select an option

  • Save MinhOmega/2c0527e57a907f8c1d20f7a78c8da280 to your computer and use it in GitHub Desktop.

Select an option

Save MinhOmega/2c0527e57a907f8c1d20f7a78c8da280 to your computer and use it in GitHub Desktop.
Script to create a bulk of empty commits in GitHub and trigger a rerun when the process is done. It is not recommended to use it, as it may pose a risk to the GitHub Actions server.
name: Create Bulk Empty Commits And Trigger After Done
on:
workflow_dispatch:
env:
TOTAL_COMMITS: 100 # Total number of commits to create
USER_NAME: '' # Global user name for git config
USER_EMAIL: '' # Global user email for git config
WORKFLOW_FILE_NAME: '' # Workflow file to trigger
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.PAT }}
- 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 }}
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 - 2 ))
echo "API total commits: $total_commits"
echo "::set-output name=total_commits::$total_commits"
- name: Read Commit Count from README.md and Compare
id: compare_commit_count
run: |
if [ -f README.md ]; then
readme_count=$(grep "Total Commits:" README.md | tail -1 | awk '{print $3}')
echo "README.md total commits: $readme_count"
if [ "$readme_count" -ge "${{ steps.commit_count.outputs.total_commits }}" ]; then
echo "::set-output name=final_count::$readme_count"
else
echo "::set-output name=final_count::${{ steps.commit_count.outputs.total_commits }}"
fi
else
echo "::set-output name=final_count::${{ steps.commit_count.outputs.total_commits }}"
fi
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.17'
- name: Create Go script
run: |
echo 'package main
import (
"log"
"os"
"strconv"
)
func main() {
// Get the initial commit count from the environment variable
initialCommitCount, err := strconv.Atoi(os.Getenv("INITIAL_COMMIT_COUNT"))
if err != nil {
log.Fatalf("failed to convert INITIAL_COMMIT_COUNT to int: %v", err)
}
totalCommits, err := strconv.Atoi(os.Getenv("TOTAL_COMMITS"))
if err != nil {
log.Fatalf("failed to convert TOTAL_COMMITS to int: %v", err)
}
// Ensure git is installed and initialized
checkGit()
// Create empty commits based on the specified total
for i := 1; i <= totalCommits; i++ {
createEmptyCommit(initialCommitCount + i)
}
}
// checkGit ensures that git is installed and initializes a new repository if needed
func checkGit() {
if _, err := exec.LookPath("git"); err != nil {
log.Fatal("git is not installed")
}
if _, err := os.Stat(".git"); os.IsNotExist(err) {
cmd := exec.Command("git", "init")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("failed to initialize git repository: %v", err)
}
}
}
// createEmptyCommit creates an empty commit with a unique message
func createEmptyCommit(commitNumber int) {
message := fmt.Sprintf("Empty commit #%s", humanize.Comma(int64(commitNumber)))
cmd := exec.Command("git", "commit", "--allow-empty", "-m", message)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("failed to create empty commit #%d: %v", commitNumber, err)
}
}' > main.go
- name: Install dependencies
run: go mod init git-committer && go mod tidy
- name: Run commit script
env:
INITIAL_COMMIT_COUNT: ${{ steps.compare_commit_count.outputs.final_count }}
run: go run main.go
- name: Push changes
run: git push origin HEAD:main
- name: Update README and Push Final Commit Count
run: |
new_final_count=$(( ${{ steps.compare_commit_count.outputs.final_count }} + ${{ env.TOTAL_COMMITS }} + 1 ))
echo "Total Commits: $new_final_count" > README.md
git add README.md
git commit -m "Commit #$new_final_count"
git push origin HEAD:main
- name: Trigger workflow again
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