-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (123 loc) · 5.42 KB
/
sync.yaml
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: Update Workflow Files Across Repos
on:
push:
branches: [main]
workflow_dispatch:
jobs:
changesets:
name: Changesets
runs-on: ubuntu-latest
outputs:
hasChangesets: ${{ steps.changesets.outputs.hasChangesets }}
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup PNPM
uses: pnpm/action-setup@v3
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Dependencies
run: pnpm i
- name: Create Release Pull Request
id: changesets
uses: changesets/action@v1
with:
commit: "[ci] release"
title: "[ci] release"
env:
GITHUB_TOKEN: ${{ secrets.PUBLIC_GITHUB_TOKEN }}
sync-workflows:
runs-on: ubuntu-latest
needs: changesets
if: (needs.changesets.outputs.hasChangesets == 'false' && (contains(github.event.head_commit.message, 'deploy') || contains(github.event.head_commit.message, '[ci] release'))) || github.event_name == 'workflow_dispatch'
steps:
# Step 1: Checkout current repository
- name: Checkout current repository
uses: actions/checkout@v3
# Step 2: Install jq for JSON parsing
- name: Install jq
run: sudo apt-get install -y jq
# Step 4: Sync workflows to target repositories
- name: Update workflows in target repositories
env:
GH_TOKEN: ${{ secrets.PUBLIC_GITHUB_TOKEN }}
run: |
echo "Starting workflow sync..."
git config --global user.name "trueberryless"
git config --global user.email "[email protected]"
repos=$(jq -c '.repositories[]' repos.json)
for repo_config in $repos; do
repo_name=$(echo "$repo_config" | jq -r '.name')
echo "Processing repository: $repo_name"
# Clone the target repository
echo "Cloning repository $repo_name..."
git clone --depth 1 "https://x-access-token:${GH_TOKEN}@github.com/${repo_name}.git" target-repo
cd target-repo
# Create or switch to the branch
branch_name="update-workflows"
echo "Creating branch $branch_name..."
git checkout -b "$branch_name" || git checkout "$branch_name"
# Sync specified workflow files
for file_config in $(jq -c '.files[]' <<<"$repo_config"); do
src_file=$(echo "$file_config" | jq -r '.path')
dest_file=$(echo "$file_config" | jq -r '.targetPath')
echo "Processing file: $src_file"
# Check if the file exists in the workflow-files directory
if [ ! -f "../$src_file" ]; then
echo "Warning: File $src_file not found in /workflow-files."
continue
fi
# Prepare the destination directory
mkdir -p "$(dirname "$dest_file")"
# Merge global defaults with file-specific props
file_props=$(echo "$file_config" | jq -c '.props // {}')
merged_props=$(jq -c --argjson defaults "$global_defaults" --argjson fileProps "$file_props" '.defaults * .fileProps' <<<"{\"defaults\": $global_defaults, \"fileProps\": $file_props}")
# Handle dynamic content replacement
temp_file=$(mktemp)
cp "../$src_file" "$temp_file"
# Replace placeholders with their respective values
for key in $(echo "$merged_props" | jq -r 'keys[]'); do
value=$(echo "$merged_props" | jq -r --arg key "$key" '.[$key]')
placeholder="<%= $key %>"
echo "Replacing $placeholder with $value in $src_file..."
sed -i "s|$placeholder|$value|g" "$temp_file"
done
# Move the processed file to the target location
mv "$temp_file" "$dest_file"
# Commit and push changes if any
echo "Checking for changes..."
git add .github/workflows/
if git diff --cached --quiet; then
echo "No changes detected for $repo_name."
else
echo "Committing and pushing changes for $repo_name..."
git commit -m "Update GitHub workflow files"
git push --force origin "$branch_name"
# Check for existing pull request
echo "Checking for an open PR for branch $branch_name..."
existing_pr=$(gh pr list --base main --head "$branch_name" --json number --jq '.[0].number')
if [ -n "$existing_pr" ]; then
echo "Found existing PR #$existing_pr. Updating it..."
gh pr comment "$existing_pr" --body "The branch has been updated with the latest changes."
else
echo "No existing PR found. Creating a new one..."
gh pr create \
--base main \
--head "$branch_name" \
--title "Sync workflow files" \
--body "This PR syncs the specified GitHub workflow files from the central repository."
fi
fi
# Cleanup
cd ..
echo "Cleaning up repository clone for $repo_name..."
rm -rf target-repo
done