Skip to content

Random Daily Commits #101

Random Daily Commits

Random Daily Commits #101

name: Random Daily Commits
on:
schedule:
- cron: "0 11 * * *"
workflow_dispatch:
push:
branches:
- main
permissions:
contents: write
pull-requests: write
jobs:
create_branch_and_commit:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Git user
run: |
git config --global user.name "peregrinno"
git config --global user.email "[email protected]"
- name: Create new branch
run: |
BRANCH_NAME="random-commits-$(date +%Y%m%d_%H%M%S)"
echo "Branch criada: $BRANCH_NAME"
git checkout -b $BRANCH_NAME
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV # Define a variável de ambiente
- name: Generate random number of commits
run: |
# Gera um número aleatório entre 1 e 10
RANDOM_COMMITS=$(( ( RANDOM % 10 ) + 1 ))
echo "Gerando $RANDOM_COMMITS commits para o dia."
# Gera um prefixo aleatório
PREFIX=$(shuf -i 10000-99999 -n 1)
FILENAME="auto_src/${PREFIX}_commit.txt"
# Loop para gerar os commits
for i in $(seq 1 $RANDOM_COMMITS); do
echo "Commit número $i em $(date)" >> $FILENAME
git add $FILENAME
git commit -m "Commit automático $i de $RANDOM_COMMITS no dia $(date)"
done
- name: Create Python Hello World file with random prefix
run: |
PREFIX=$(shuf -i 10000-99999 -n 1)
FILENAME="auto_src/${PREFIX}_helloworld.py"
echo 'print("Hello, World!")' > $FILENAME
git add $FILENAME
git commit -m "Adicionando Hello World em Python com prefixo aleatório: $FILENAME"
- name: Create JavaScript Hello World file with random prefix
run: |
PREFIX=$(shuf -i 10000-99999 -n 1)
FILENAME="auto_src/${PREFIX}_helloworld.js"
echo 'console.log("Hello, World!");' > $FILENAME
git add $FILENAME
git commit -m "Adicionando Hello World em JavaScript com prefixo aleatório: $FILENAME"
- name: Push commits to new branch
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
# Certifique-se de que a nova branch seja enviada corretamente ao repositório remoto
git push --set-upstream https://x-access-token:${{ secrets.PAT_TOKEN }}@github.com/peregrinno/auto-commits.git $BRANCH_NAME
merge_created_branches:
runs-on: ubuntu-latest
needs: create_branch_and_commit
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Git user for merge
run: |
git config --global user.name "peregrinno"
git config --global user.email "[email protected]"
- name: List all branches that start with 'random-commits'
run: |
git fetch --all
# Lista todas as branches remotas que começam com 'random-commits'
BRANCHES=$(git branch -r | grep 'origin/random-commits' | sed 's/origin\///g' | xargs)
echo "Branches encontradas: $BRANCHES"
echo "BRANCHES=\"$BRANCHES\"" >> $GITHUB_ENV
- name: Merge branches into chaos and delete merged branches
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
BRANCHES: ${{ env.BRANCHES }}
run: |
git checkout chaos
git pull origin chaos
# Itera sobre as branches e faz merge de cada uma usando a referência origin/<branch>
for BRANCH in $(echo $BRANCHES); do
BRANCH=$(echo $BRANCH | tr -d '"') # Remove quaisquer aspas que ainda possam estar presentes
echo "Fazendo merge da branch origin/$BRANCH para chaos"
git merge --no-ff "origin/$BRANCH" || { echo "Falha ao fazer merge da branch origin/$BRANCH"; continue; }
# Se o merge for bem-sucedido, apaga a branch
echo "Merge bem-sucedido. Apagando branch origin/$BRANCH"
git push origin --delete "$BRANCH" || echo "Falha ao apagar a branch origin/$BRANCH"
done
# Push após o merge
git push origin chaos