-
Notifications
You must be signed in to change notification settings - Fork 3
130 lines (114 loc) · 5.04 KB
/
check_server_update.yml
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
name: Check Servers and Update Configuration
on:
schedule:
- cron: "30 * * * *" # Runs every 1 minutes
workflow_dispatch: # Allows manual triggering
jobs:
check-servers:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Check Server Status
id: check_status
run: |
primary_server="http://zimbor.go.ro"
servers=(
"http://peviitor.go.ro"
"http://srvalx.duckdns.org:8983"
"http://zimbor.go.ro:8985"
)
available_server=""
echo "Checking $primary_server..."
response=$(curl -s -L -o /dev/null -w "%{http_code}" "$primary_server" || true)
if [ "$response" -eq 200 ]; then
echo "$primary_server is up and running (HTTP 200 OK)"
else
echo "$primary_server is not available or returned status code $response"
fi
if [ -z "$available_server" ]; then
for server in "${servers[@]}"; do
echo "Checking $server..."
response=$(curl -s -L -o /dev/null -w "%{http_code}" "$server" || true)
if [ "$response" -eq 200 ]; then
echo "$server is up and running (HTTP 200 OK)"
available_server="${server#http://}"
available_server="${available_server%%/*}"
echo "Available server: $available_server"
break
else
echo "$server is not available or returned status code $response"
fi
echo "---------------------------------"
done
fi
echo "available_server=$available_server" >> $GITHUB_ENV
- name: Check Current Active Server
id: check_current_active
run: |
current_active_server=$(curl -s https://api.peviitor.ro/devops/solr/)
current_active_server="${current_active_server#http://}"
current_active_server="${current_active_server%%/*}"
echo "Current_active_server=$current_active_server"
echo "current_active_server=$current_active_server" >> $GITHUB_ENV
- name: Update configuration config.php
id: update_config
run: |
CONFIG_FILE="v3/config.php"
if [ "$current_active_server" != "zimbor.go.ro" ] && [ -n "$available_server" ]; then
if [ "$current_active_server" = "$available_server" ]; then
echo "The current active server is $available_server. No changes needed."
exit 0
fi
echo "Switching to available server: $available_server"
cp $CONFIG_FILE $CONFIG_FILE.bak
sed -i 's/\$server = '\''zimbor.go.ro'\'';/\/\/\$server = '\''zimbor.go.ro'\'';/' $CONFIG_FILE
sed -i "s/\/\/\$server = '$available_server';/\$server = '$available_server';/" $CONFIG_FILE
if ! cmp -s $CONFIG_FILE $CONFIG_FILE.bak; then
git checkout -b update-server-${{ github.sha }}
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add $CONFIG_FILE
git commit -m "Switched to $available_server"
git push origin update-server-${{ github.sha }}
echo "needs_update=true" >> $GITHUB_ENV
else
echo "No changes to commit."
echo "needs_update=false" >> $GITHUB_ENV
fi
else
echo "zimbor.go.ro server is available or no change needed. No changes made."
echo "needs_update=false" >> $GITHUB_ENV
fi
- name: Create PR
if: env.needs_update == 'true'
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
echo "Creating PR from update-server-${{ github.sha }} to master..."
gh pr create --title "Update server to $available_server" --body "This PR updates the server configuration to use $available_server." --head update-server-${{ github.sha }} --base master
- name: Approve PR
if: env.needs_update == 'true'
env:
PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
echo "${{ secrets.PAT_TOKEN }}" | gh auth login --with-token
PR_NUMBER=$(gh pr list --head update-server-${{ github.sha }} --json number --jq '.[0].number')
if [ -n "$PR_NUMBER" ]; then
echo "Approving PR #$PR_NUMBER..."
gh pr review $PR_NUMBER --approve
else
echo "No PR found to approve."
exit 1
fi
- name: Merge PR
if: env.needs_update == 'true'
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
PR_NUMBER=$(gh pr list --head update-server-${{ github.sha }} --json number --jq '.[0].number')
if [ -n "$PR_NUMBER" ]; then
echo "Merging PR #$PR_NUMBER..."
gh pr merge $PR_NUMBER --squash --delete-branch
else
echo "No PR found to merge."
exit 1
fi