Skip to content

Commit

Permalink
Feature: Add games.json and script to create / update branches for v2
Browse files Browse the repository at this point in the history
Once PR is merged, to create / update game branches:

```powershell
./Generate-GitBranches.ps1 -TargetRepoPath . -Pull
```
  • Loading branch information
leojonathanoh committed Sep 15, 2023
1 parent 28892d7 commit d9c5ffe
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 0 deletions.
158 changes: 158 additions & 0 deletions Generate-GitBranches.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# 1. This script create / updates git branches (named <game_platform>-<game_engine>-<game>) based on games.json
# 2. By default it creates a git branch for each game found in games.json. To limit to one game, specify -GamePlatform, -GameEngine, and -Game
# 3. To build a game, checkout to its branch, edit .env, mutate .trigger, commit and push
param(
[string]$TargetRepoPath, # Specifies a target git repo
[switch]$Pull, # Whether to pull changes from remote repo before creating / updating branches
[string]$GamePlatform,
[string]$GameEngine,
[string]$Game
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

if (!$TargetRepoPath) {
throw "-Path cannot be empty"
}

# Get games
$games = Get-Content $PSScriptRoot/games.json -Encoding utf8 -Force | ConvertFrom-Json -Depth 100 -AsHashtable
if ($GamePlatform) {
$games = $games | ? { $_['game_platform'] -eq $GamePlatform }
}
if ($GameEngine) {
$games = $games | ? { $_['game_engine'] -eq $GameEngine }
}
if ($Game) {
$games = $games | ? { $_['game'] -eq $Game }
}
if ($games.Count -eq 0) {
throw "No games found"
}

# Create new branch, remove all files except .git, create .trigger file, create .gitlab-ci.yml, commit files
try {
Push-Location $TargetRepoPath
git status 2>&1 > $null
if ($LASTEXITCODE) { throw "$TargetRepoPath is not a git repo" }
foreach ($g in $games) {
$branch = "$( $g['game_platform'] )-$( $g['game_engine'] )-$( $g['game'] )"

git checkout -f master
if ($LASTEXITCODE) { throw }
if ($Pull) {
git pull origin master
if ($LASTEXITCODE) { throw }
}
$masterTrackedFiles = git ls-files
if ($LASTEXITCODE) { throw }
$existingBranch = git rev-parse --verify $branch
if ($existingBranch) {
"Updating branch '$branch'" | Write-Host -ForegroundColor Green
if ($Pull) {
git fetch origin
git branch -f $branch origin/$branch
}
if ($LASTEXITCODE) { throw }
git checkout -f $branch
if ($LASTEXITCODE) { throw }
}else {
"Creating new branch '$branch'" | Write-Host -ForegroundColor Green
git checkout -b $branch
if ($LASTEXITCODE) { throw }
}

$masterTrackedFiles | Get-Item -Force | Remove-Item -Recurse -Force
git checkout master -- build
if ($LASTEXITCODE) { throw }
git checkout master -- update
if ($LASTEXITCODE) { throw }
git checkout master -- build.sh
if ($LASTEXITCODE) { throw }
git checkout master -- notify.sh
if ($LASTEXITCODE) { throw }

@"
PIPELINE=build
GAME_UPDATE_COUNT=$( $g['game_update_count'] )
GAME_VERSION=$( $g['game_version'] )
GAME_PLATFORM=$( $g['game_platform'] )
GAME_ENGINE=$( $g['game_engine'] )
APPID=$( $g['appid'] )
CLIENT_APPID=$( $g['client_appid'] )
GAME=$( $g['game'] )
MOD=$( $g['mod'] )
FIX_APPMANIFEST=$( $g['fix_app_manifest'].ToString().ToLower() )
LATEST=true
DOCKER_REPOSITORY=$( $g['docker_repository'] )
STEAM_LOGIN=
"@ | Out-File .env -Encoding utf8 -Force

@'
BUILD_STATUS=
BUILD_EPOCH=0
BASE_SIZE=0
LAYERED_SIZE=0
'@ | Out-File .state -Encoding utf8 -Force

@'
stages:
- build
- notify
build-image:
stage: build
image: docker:20.10.18-git
services:
- docker:20.10.18-dind
variables:
DOCKER_DRIVER: overlay2
rules:
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH != "master" # Run on all branches except master
changes:
- .trigger # Run only when .trigger is added or modified
timeout: 120m
script:
- |
set +e
./build.sh
if [ $? = 0 ]; then
echo BUILD_STATUS=success >> .build.state
else
echo BUILD_STATUS=failed >> .build.state
exit 1
fi
artifacts:
name: .build.state-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
paths:
- .build.state
expire_in: never
when: always
notify:
stage: notify
image: alpine:3.15
rules:
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH != "master" # Run on all branches except master
changes:
- .trigger # Run only when .trigger is added or modified
when: always
timeout: 60m
script:
- apk add --no-cache curl
- ./notify.sh
'@ | Out-File .gitlab-ci.yml -Encoding utf8 -Force

git add .
if ($existingBranch) {
git commit -m "Update files"
}else {
git commit -m "Add files"
}
}
}catch {
throw
}finally {
git checkout master
Pop-Location
}
28 changes: 28 additions & 0 deletions Generate-games.json.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# Save the game image objects
$endpoint = ".../game_images?order=game_engine,game"
Invoke-WebRequest -Uri "$endpoint" -Method GET | Select -ExpandProperty Content | Out-File game_images.json -Encoding utf8 -Force
$endpoint = ".../game_images?order=game_engine,game" # beta
Invoke-WebRequest -Uri "$endpoint" -Method GET | Select -ExpandProperty Content | Out-File game_images-beta.json -Encoding utf8

# Create our simple games.json
$games = [System.Collections.ArrayList]@()
$gameImages = Get-Content game_images.json -Encoding utf8 -Force | ConvertFrom-Json -Depth 100 | Sort-Object -Property game_engine,game
foreach ($g in $gameImages) {
$g2 = [ordered]@{
game_version = $g.version_layered
game_update_count = $g.game_update_count
game_platform = 'steam'
game_engine = $g.game_engine
game = $g.game
mod = $g.mod
appid = $g.appid
client_appid = $g.client_appid
docker_repository = "$( $g.image_namespace )/$( $g.game )"
fix_app_manifest = $g.fix_app_manifest
}
$games.Add($g2) > $null
}
$games | ConvertTo-Json -Depth 100 | Out-File games.json -Encoding utf8 -Force
182 changes: 182 additions & 0 deletions games.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
[
{
"game_version": 1127,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "cstrike",
"mod": "cstrike",
"appid": 90,
"client_appid": 10,
"docker_repository": "goldsourceservers/cstrike",
"fix_app_manifest": true
},
{
"game_version": 1003,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "czero",
"mod": "czero",
"appid": 90,
"client_appid": 80,
"docker_repository": "goldsourceservers/czero",
"fix_app_manifest": true
},
{
"game_version": 1123,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "dmc",
"mod": "dmc",
"appid": 90,
"client_appid": 40,
"docker_repository": "goldsourceservers/dmc",
"fix_app_manifest": true
},
{
"game_version": 1126,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "dod",
"mod": "dod",
"appid": 90,
"client_appid": 30,
"docker_repository": "goldsourceservers/dod",
"fix_app_manifest": true
},
{
"game_version": 1122,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "gearbox",
"mod": "gearbox",
"appid": 90,
"client_appid": 70,
"docker_repository": "goldsourceservers/gearbox",
"fix_app_manifest": true
},
{
"game_version": 1121,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "ricochet",
"mod": "ricochet",
"appid": 90,
"client_appid": 60,
"docker_repository": "goldsourceservers/ricochet",
"fix_app_manifest": true
},
{
"game_version": 1122,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "tfc",
"mod": "tfc",
"appid": 90,
"client_appid": 20,
"docker_repository": "goldsourceservers/tfc",
"fix_app_manifest": true
},
{
"game_version": 1122,
"game_update_count": 0,
"game_platform": "steam",
"game_engine": "hlds",
"game": "valve",
"mod": "",
"appid": 90,
"client_appid": 70,
"docker_repository": "goldsourceservers/valve",
"fix_app_manifest": true
},
{
"game_version": 13879,
"game_update_count": 4,
"game_platform": "steam",
"game_engine": "srcds",
"game": "csgo",
"mod": "",
"appid": 740,
"client_appid": 730,
"docker_repository": "sourceservers/csgo",
"fix_app_manifest": false
},
{
"game_version": 6630498,
"game_update_count": 2,
"game_platform": "steam",
"game_engine": "srcds",
"game": "cstrike",
"mod": "",
"appid": 232330,
"client_appid": 240,
"docker_repository": "sourceservers/cstrike",
"fix_app_manifest": false
},
{
"game_version": 6630498,
"game_update_count": 2,
"game_platform": "steam",
"game_engine": "srcds",
"game": "dod",
"mod": "",
"appid": 232290,
"client_appid": 300,
"docker_repository": "sourceservers/dod",
"fix_app_manifest": false
},
{
"game_version": 6630498,
"game_update_count": 2,
"game_platform": "steam",
"game_engine": "srcds",
"game": "hl2mp",
"mod": "",
"appid": 232370,
"client_appid": 320,
"docker_repository": "sourceservers/hl2mp",
"fix_app_manifest": false
},
{
"game_version": 1041,
"game_update_count": 3,
"game_platform": "steam",
"game_engine": "srcds",
"game": "left4dead",
"mod": "",
"appid": 222840,
"client_appid": 500,
"docker_repository": "sourceservers/left4dead",
"fix_app_manifest": false
},
{
"game_version": 2229,
"game_update_count": 9,
"game_platform": "steam",
"game_engine": "srcds",
"game": "left4dead2",
"mod": "",
"appid": 222860,
"client_appid": 550,
"docker_repository": "sourceservers/left4dead2",
"fix_app_manifest": false
},
{
"game_version": 8308684,
"game_update_count": 11,
"game_platform": "steam",
"game_engine": "srcds",
"game": "tf",
"mod": "",
"appid": 232250,
"client_appid": 440,
"docker_repository": "sourceservers/tf",
"fix_app_manifest": false
}
]

0 comments on commit d9c5ffe

Please sign in to comment.