forked from aaronTSmehar/thoughtspot.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
125 lines (103 loc) · 4.21 KB
/
remove-workflow.yml
File metadata and controls
125 lines (103 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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: 'workflow.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 "github-actions@github.com"
- 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"