Skip to content

Remove Workflow from All Branches #1

Remove Workflow from All Branches

Remove Workflow from All Branches #1

name: Remove Workflow from All Branches
on:
workflow_dispatch:
inputs:
workflow_path:
description: 'Path to the workflow file to remove (relative to .github/workflows/)'
required: true
default: 'sync-to-public-mirror.yml'
skip_branches:
description: 'Comma-separated list of branches to skip (optional)'
required: false
default: ''
dry_run:
description: 'Dry run (will not make actual changes)'
required: false
default: false
type: boolean
jobs:
remove-from-all-branches:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
# Need to use PAT for workflow files
token: ${{ secrets.WORKFLOW_AUTOMATION }}
- name: Set up Git identity
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
- name: Get all branches and save to file
run: |
# Save branches to a file
git branch -r | grep -v HEAD | sed 's/origin\///' > all_branches.txt
echo "All branches found:"
cat all_branches.txt
- name: Remove workflow from all branches
run: |
WORKFLOW_PATH=".github/workflows/${{ github.event.inputs.workflow_path }}"
WORKFLOW_DIR=".github/workflows"
GITHUB_DIR=".github"
SKIP_BRANCHES="${{ github.event.inputs.skip_branches }}"
DRY_RUN="${{ github.event.inputs.dry_run }}"
IFS=',' read -ra SKIP_ARRAY <<< "$SKIP_BRANCHES"
CURRENT_BRANCH=$(git branch --show-current)
echo "Current branch: $CURRENT_BRANCH"
# Process each branch from the file
while read branch; do
# Trim whitespace from branch name
branch=$(echo "$branch" | xargs)
# Skip empty lines
if [ -z "$branch" ]; then
continue
fi
# Check if branch is in skip list
SKIP=false
for skip_branch in "${SKIP_ARRAY[@]}"; do
if [ "$branch" = "$skip_branch" ]; then
SKIP=true
echo "Skipping branch: $branch (in skip list)"
break
fi
done
if [ "$SKIP" = true ]; then
continue
fi
echo "Processing branch: $branch"
# Checkout branch
git checkout "$branch" || { echo "Failed to checkout $branch, skipping"; continue; }
# Check if workflow file exists
if [ ! -f "$WORKFLOW_PATH" ]; then
echo "Workflow file doesn't exist in $branch. Skipping."
continue
fi
echo "Found workflow file in branch: $branch"
# Check if we're doing a dry run
if [ "$DRY_RUN" = "true" ]; then
echo "DRY RUN: Would remove $WORKFLOW_PATH from branch $branch"
continue
fi
# Remove the workflow file
rm "$WORKFLOW_PATH"
# Check if workflows directory is empty
if [ -d "$WORKFLOW_DIR" ] && [ -z "$(ls -A $WORKFLOW_DIR)" ]; then
echo "Workflows directory is empty, removing it"
rm -rf "$WORKFLOW_DIR"
# Check if .github directory is empty
if [ -d "$GITHUB_DIR" ] && [ -z "$(ls -A $GITHUB_DIR)" ]; then
echo "GitHub directory is empty, removing it"
rm -rf "$GITHUB_DIR"
fi
fi
# Commit and push changes
git add -A
git commit -m "Remove workflow file from branch $branch" || echo "No changes to commit for $branch"
git push origin "$branch" || echo "Failed to push changes to $branch"
echo "Successfully removed workflow from $branch"
done < all_branches.txt
# Return to original branch
git checkout "$CURRENT_BRANCH"