|
| 1 | +name: Deploy Workflow to All Branches |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + workflow_path: |
| 7 | + description: 'Path to the workflow file to deploy (relative to .github/workflows/)' |
| 8 | + required: true |
| 9 | + default: 'sync-to-public-mirror.yml' |
| 10 | + workflow_content: |
| 11 | + description: 'Content of the workflow file (base64 encoded)' |
| 12 | + required: true |
| 13 | + skip_branches: |
| 14 | + description: 'Comma-separated list of branches to skip (optional)' |
| 15 | + required: false |
| 16 | + default: '' |
| 17 | + force_deploy: |
| 18 | + description: 'Force deploy even if workflow file exists' |
| 19 | + required: false |
| 20 | + default: 'true' |
| 21 | + type: boolean |
| 22 | + use_github_app: |
| 23 | + description: 'Use GitHub App token instead of SSH signing (for protected branches)' |
| 24 | + required: false |
| 25 | + default: 'true' |
| 26 | + type: boolean |
| 27 | + |
| 28 | +jobs: |
| 29 | + deploy-to-all-branches: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + permissions: |
| 32 | + contents: write |
| 33 | + actions: write |
| 34 | + steps: |
| 35 | + - name: Checkout repository |
| 36 | + uses: actions/checkout@v3 |
| 37 | + with: |
| 38 | + fetch-depth: 0 |
| 39 | + token: ${{ secrets.WORKFLOW_AUTOMATION }} |
| 40 | + |
| 41 | + - name: Set up Git identity |
| 42 | + run: | |
| 43 | + # Configure git with identity |
| 44 | + git config --global user.name "GitHub Actions" |
| 45 | + git config --global user.email "[email protected]" |
| 46 | + |
| 47 | + # Disable signing by default - we'll use GitHub's verified commits instead |
| 48 | + git config --global commit.gpgsign false |
| 49 | +
|
| 50 | + - name: Decode workflow content |
| 51 | + run: | |
| 52 | + echo "${{ github.event.inputs.workflow_content }}" | base64 -d > workflow_content.yml |
| 53 | + mkdir -p .github/workflows |
| 54 | + |
| 55 | + - name: Get all branches and save to file |
| 56 | + run: | |
| 57 | + # Save branches to a file instead of environment variable |
| 58 | + git branch -r | grep -v HEAD | sed 's/origin\///' > all_branches.txt |
| 59 | + echo "All branches found:" |
| 60 | + cat all_branches.txt |
| 61 | +
|
| 62 | + - name: Deploy workflow to all branches |
| 63 | + run: | |
| 64 | + WORKFLOW_PATH=".github/workflows/${{ github.event.inputs.workflow_path }}" |
| 65 | + SKIP_BRANCHES="${{ github.event.inputs.skip_branches }}" |
| 66 | + FORCE_DEPLOY="${{ github.event.inputs.force_deploy }}" |
| 67 | + USE_GITHUB_APP="${{ github.event.inputs.use_github_app }}" |
| 68 | + IFS=',' read -ra SKIP_ARRAY <<< "$SKIP_BRANCHES" |
| 69 | + |
| 70 | + CURRENT_BRANCH=$(git branch --show-current) |
| 71 | + echo "Current branch: $CURRENT_BRANCH" |
| 72 | + |
| 73 | + # Process each branch from the file |
| 74 | + while read branch; do |
| 75 | + # Trim whitespace from branch name |
| 76 | + branch=$(echo "$branch" | xargs) |
| 77 | + |
| 78 | + # Skip empty lines |
| 79 | + if [ -z "$branch" ]; then |
| 80 | + continue |
| 81 | + fi |
| 82 | + |
| 83 | + # Check if branch is in skip list |
| 84 | + SKIP=false |
| 85 | + for skip_branch in "${SKIP_ARRAY[@]}"; do |
| 86 | + if [ "$branch" = "$skip_branch" ]; then |
| 87 | + SKIP=true |
| 88 | + echo "Skipping branch: $branch (in skip list)" |
| 89 | + break |
| 90 | + fi |
| 91 | + done |
| 92 | + |
| 93 | + if [ "$SKIP" = true ]; then |
| 94 | + continue |
| 95 | + fi |
| 96 | + |
| 97 | + echo "Processing branch: $branch" |
| 98 | + |
| 99 | + # Checkout branch |
| 100 | + git checkout "$branch" || { echo "Failed to checkout $branch, skipping"; continue; } |
| 101 | + |
| 102 | + # Enhanced file existence check - check both tracked and untracked files |
| 103 | + FILE_EXISTS=false |
| 104 | + |
| 105 | + # Check if the file exists on disk |
| 106 | + if [ -f "$WORKFLOW_PATH" ]; then |
| 107 | + echo "Workflow file exists on disk in branch $branch" |
| 108 | + FILE_EXISTS=true |
| 109 | + fi |
| 110 | + |
| 111 | + # Check if the file is untracked by git |
| 112 | + UNTRACKED=$(git ls-files --others --exclude-standard "$WORKFLOW_PATH" 2>/dev/null) |
| 113 | + if [ -n "$UNTRACKED" ]; then |
| 114 | + echo "Workflow file exists but is untracked in branch $branch" |
| 115 | + FILE_EXISTS=true |
| 116 | + fi |
| 117 | + |
| 118 | + # Check if file is in git's index (tracked) |
| 119 | + TRACKED=$(git ls-files "$WORKFLOW_PATH" 2>/dev/null) |
| 120 | + if [ -n "$TRACKED" ]; then |
| 121 | + echo "Workflow file is tracked by git in branch $branch" |
| 122 | + FILE_EXISTS=true |
| 123 | + fi |
| 124 | + |
| 125 | + # Act based on file existence and force_deploy flag |
| 126 | + if [ "$FILE_EXISTS" = true ] && [ "$FORCE_DEPLOY" != "true" ]; then |
| 127 | + echo "Workflow file exists in $branch and force_deploy is not enabled. Skipping." |
| 128 | + continue |
| 129 | + elif [ "$FILE_EXISTS" = true ] && [ "$FORCE_DEPLOY" = "true" ]; then |
| 130 | + echo "Workflow file exists in $branch, but force_deploy is enabled. Overwriting." |
| 131 | + else |
| 132 | + echo "Workflow file does not exist in $branch. Creating." |
| 133 | + fi |
| 134 | + |
| 135 | + # Create directory structure if it doesn't exist |
| 136 | + mkdir -p "$(dirname "$WORKFLOW_PATH")" |
| 137 | + |
| 138 | + # Copy workflow file |
| 139 | + cp workflow_content.yml "$WORKFLOW_PATH" |
| 140 | + |
| 141 | + # Add diagnostic output to verify file was created |
| 142 | + echo "Verifying workflow file was created:" |
| 143 | + ls -la "$WORKFLOW_PATH" || echo "Error: File not found after creation" |
| 144 | + |
| 145 | + # Check if file is in .gitignore |
| 146 | + if grep -q "$WORKFLOW_PATH" .gitignore 2>/dev/null; then |
| 147 | + echo "WARNING: The workflow path '$WORKFLOW_PATH' appears to be in .gitignore" |
| 148 | + echo "This could prevent git from tracking the file" |
| 149 | + fi |
| 150 | + |
| 151 | + # Add the file and check if it was added successfully |
| 152 | + git add "$WORKFLOW_PATH" |
| 153 | + if ! git ls-files --stage | grep -q "$WORKFLOW_PATH"; then |
| 154 | + echo "WARNING: Failed to add file to git index. File may be ignored by gitignore rules." |
| 155 | + echo "Attempting to force-add the file..." |
| 156 | + git add -f "$WORKFLOW_PATH" |
| 157 | + fi |
| 158 | + |
| 159 | + # Verify the file is now in the index |
| 160 | + if git ls-files --stage | grep -q "$WORKFLOW_PATH"; then |
| 161 | + echo "File successfully added to git index" |
| 162 | + else |
| 163 | + echo "ERROR: Still unable to add file to git index despite force-add attempt" |
| 164 | + echo "This suggests a strong gitignore rule or other git configuration issue" |
| 165 | + continue |
| 166 | + fi |
| 167 | + |
| 168 | + # Commit changes |
| 169 | + echo "Committing changes" |
| 170 | + if ! git commit -m "Add workflow file to branch $branch"; then |
| 171 | + echo "No changes to commit for $branch or commit failed" |
| 172 | + continue |
| 173 | + fi |
| 174 | + |
| 175 | + if ! git push origin "$branch"; then |
| 176 | + echo "Failed to push changes to $branch" |
| 177 | + echo "This branch may be protected and require verified commits." |
| 178 | + continue |
| 179 | + fi |
| 180 | + |
| 181 | + echo "Successfully deployed workflow to $branch" |
| 182 | + done < all_branches.txt |
| 183 | + |
| 184 | + # Return to original branch |
| 185 | + git checkout "$CURRENT_BRANCH" |
0 commit comments