Skip to content

Commit 44d3b4e

Browse files
Add workflow for automation of client binaries update (#27)
* Add workflow to automatic client update * Add commentary about what should be enabled to be able use workflow * Add comment about cron behavior * Change windows-latest to windows-2022 * Update echo about update Co-authored-by: Kerbiter <[email protected]> * Fix code style Co-authored-by: Kerbiter <[email protected]> * Add comment about to not write trailing slash in `CLIENT_PATH` var * Update PR body text Co-authored-by: Kerbiter <[email protected]> * Add version to the pushed branch * Improve update [Delete] section in updateexec * Add tag to the gh command * Remove `REPO_NAME` variable * Add more comments to the environment variables * Apply suggestion from @Metadorius * Apply suggestion from @Metadorius --------- Co-authored-by: Kerbiter <[email protected]>
1 parent 0a375c2 commit 44d3b4e

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# To be able use this workflow you must ensure that your repository have enable write and right access for GitHub Actions.
2+
#
3+
# GUI Reference to enable access:
4+
# Repository --> Actions --> General --> Workflow permissions --> Read and write permissions
5+
# \--> Allow Github Actions to create and approve pull requests
6+
7+
name: Update XNA CnCNet Client
8+
9+
on:
10+
workflow_dispatch:
11+
schedule:
12+
- cron: "0 0 * * *" # Starts everyday at 00:00 UTC+0
13+
14+
env:
15+
CLIENT_PATH: ClientFiles # Do not add trailing slash
16+
UPDATE_BRANCH_NAME: update-client-binaries # The workflow adds the release tag after this value, i.e. "update-client-binaries-2.12.13"
17+
COMMIT_MESSAGE: Update client binaries to the latest version
18+
PR_TITLE: Update client binaries to the version # The workflow adds the release tag after this value, i.e. "Update client binaries to the version 2.12.13"
19+
PR_BODY: This is an automatic pull request to update client binaries to the latest release published in [CnCNet/xna-cncnet-client](https://github.com/CnCNet/xna-cncnet-client) repository.
20+
21+
jobs:
22+
publish-update-pr:
23+
runs-on: windows-2022
24+
25+
steps:
26+
- name: Checkout Repo
27+
uses: actions/checkout@v3
28+
with:
29+
lfs: true
30+
31+
- name: Download Latest Release
32+
id: download
33+
uses: robinraju/release-downloader@v1
34+
with:
35+
repository: CnCNet/xna-cncnet-client
36+
latest: true
37+
fileName: xna-cncnet-client*
38+
39+
- name: Open PR
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
run: |
43+
$repoDir = (Get-Location).Path
44+
$currentVersion = (Get-Item -Path "$repoDir\Resources\clientdx.exe").VersionInfo.FileVersion -Replace "\.0$", ""
45+
$downloadVersion = '${{ steps.download.outputs.tag_name }}'
46+
47+
echo "Current client version: '$currentVersion'"
48+
echo "Downloaded client version: '$downloadVersion'"
49+
50+
if ($currentVersion -eq $downloadVersion)
51+
{
52+
echo "Update is not required"
53+
}
54+
else
55+
{
56+
echo "Update required"
57+
58+
# Configure git
59+
git config user.name "github-actions[bot]"
60+
git config user.email "github-actions[bot]@users.noreply.github.com"
61+
62+
# Remove old branch if exist and make new one
63+
git branch -D ${{ env.UPDATE_BRANCH_NAME }}-${{ steps.download.outputs.tag_name }}
64+
git checkout -b ${{ env.UPDATE_BRANCH_NAME }}-${{ steps.download.outputs.tag_name }}
65+
66+
# Delete old binaries, unpack archive, remove archive
67+
rm -r -fo "$repoDir\Resources\Binaries"
68+
rm -r -fo "$repoDir\Resources\BinariesNET8"
69+
7z x xna-cncnet-client-*.7z -y
70+
rm xna-cncnet-client-*.7z
71+
72+
# Commit changes
73+
git commit -am "${{ env.COMMIT_MESSAGE }}"
74+
75+
# Path to the updateexec file
76+
$updateExecPath = "$repoDir\${{ env.CLIENT_PATH }}\updateexec"
77+
78+
# Check if the updateexec file exists
79+
if (-Not (Test-Path -Path $updateExecPath))
80+
{
81+
echo "The file 'updateexec' does not exist. Skip updating [Delete] section."
82+
}
83+
else
84+
{
85+
# Get the deleted and moved files from git diff
86+
$deletedFiles = git diff --diff-filter=D --name-status HEAD~1 HEAD | Where-Object { $_.StartsWith('D') } | ForEach-Object { $_.Substring(1).Trim().Replace('/', '\') }
87+
$deletedFiles += [Environment]::NewLine
88+
$deletedFiles += git diff --diff-filter=R --name-status --diff-filter=R HEAD~1 HEAD | Where-Object { $_.StartsWith('R100') } | ForEach-Object { $_.Substring(4).Trim().Split(' ')[0].Replace('/', '\') }
89+
90+
# If exclude is empty, ignore
91+
if ($exclude.Length -ne 0)
92+
{
93+
# Clearing delete files from exclude path
94+
$tmp = ""
95+
foreach($delete_file in $deletedFiles)
96+
{
97+
if ($delete_file.StartsWith($exclude + '\'))
98+
{
99+
$tmp += ($delete_file.Remove(0, ($exclude.Length + 1)))
100+
$tmp += [Environment]::NewLine
101+
}
102+
else
103+
{
104+
$tmp += $delete_file
105+
$tmp += [Environment]::NewLine
106+
}
107+
}
108+
$deletedFiles = $tmp
109+
}
110+
111+
# Check if there are any deleted files
112+
if ($deletedFiles.Count -eq 0)
113+
{
114+
echo "No deleted files found in the git diff. Skip updating [Delete] section."
115+
}
116+
else
117+
{
118+
# Read the content of the updateexec file
119+
$updateexecContent_old = Get-Content -Path $updateExecPath
120+
$updateexecContent_new = ""
121+
122+
# Find the [Delete] section and its position
123+
$deleteSectionIndex = $updateexecContent_old.IndexOf("[Delete]")
124+
125+
# If not exist, create
126+
if ($deleteSectionIndex -eq -1)
127+
{
128+
$updateexecContent_old += [Environment]::NewLine
129+
$updateexecContent_old += "[Delete]"
130+
$deleteSectionIndex = $updateexecContent_old.IndexOf("[Delete]")
131+
}
132+
133+
# Exclude path from string for config
134+
$exclude = "${{ env.CLIENT_PATH }}"
135+
136+
# Iterate all content of old updateexec
137+
foreach($old_line in $updateexecContent_old)
138+
{
139+
$index = $updateexecContent_old.IndexOf($old_line)
140+
$updateexecContent_new += $old_line + [Environment]::NewLine
141+
142+
# If we find section [Delete], add new deleted files
143+
if ($deleteSectionIndex -eq $index)
144+
{
145+
$updateexecContent_new += "; ${{ steps.download.outputs.tag_name }} (auto-generated entries for removed/renamed files)" + [Environment]::NewLine
146+
147+
foreach($new_delete in $deletedFiles)
148+
{
149+
$updateexecContent_new += $new_delete + [Environment]::NewLine
150+
}
151+
152+
$updateexecContent_new += "; end entries" + [Environment]::NewLine + [Environment]::NewLine
153+
}
154+
}
155+
156+
# Save the modified content back to the file
157+
$updateexecContent_new | Set-Content -Path $updateExecPath
158+
}
159+
}
160+
161+
# Commit changes if there exist updateexec and client binaries have new deleted files
162+
git commit --amend -am "${{ env.COMMIT_MESSAGE }}"
163+
164+
# Push changes
165+
git push --force origin ${{ env.UPDATE_BRANCH_NAME }}-${{ steps.download.outputs.tag_name }}
166+
167+
# This timeout is for the remote server to index received content
168+
timeout /t 10 > nul
169+
170+
# Open a PR
171+
gh pr create -B main -H ${{ env.UPDATE_BRANCH_NAME }}-${{ steps.download.outputs.tag_name }} --title '${{ env.PR_TITLE }} ${{ steps.download.outputs.tag_name }}' --body '${{ env.PR_BODY }}'
172+
}

0 commit comments

Comments
 (0)