Skip to content

Commit

Permalink
Feature: Add games.json and script to create and update branches (s…
Browse files Browse the repository at this point in the history
…tartersclan#152)

Once PR is merged, to create / update game branches:

```powershell
./Generate-GitBranches.ps1 -Repo . -WhatIf # Dry run
./Generate-GitBranches.ps1 -Repo .
```

Co-authored-by: Joel Timothy Oh <[email protected]>
  • Loading branch information
leojonathanoh and joeltimothyoh authored Dec 9, 2023
1 parent 79885f4 commit 3348f92
Show file tree
Hide file tree
Showing 26 changed files with 887 additions and 540 deletions.
34 changes: 8 additions & 26 deletions .generate/Generate-GameImageRepositoryReadme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,41 +98,23 @@ $gameList | % {

# Generate game image repository readme content
$content = @"
| ``master`` | ``build`` | ``update`` |
|:-:|:-:|:-:|
[![pipeline-github-master-badge][]][pipeline-github-master-link] | [![pipeline-travis-build-badge][]][pipeline-travis-build-link] [![pipeline-azurepipelines-build-badge][]][pipeline-azurepipelines-build-link] [![pipeline-circleci-build-badge][]][pipeline-circleci-build-link] [![pipeline-gitlab-build-badge][]][pipeline-gitlab-build-link] | [![pipeline-travis-update-badge][]][pipeline-travis-update-link] [![pipeline-azurepipelines-update-badge][]][pipeline-azurepipelines-update-link] [![pipeline-circleci-update-badge][]][pipeline-circleci-update-link] [![pipeline-gitlab-update-badge][]][pipeline-gitlab-update-link]
| ``master`` |
|:-:|
[![pipeline-github-master-badge][]][pipeline-github-master-link] |
[pipeline-github-master-badge]: https://img.shields.io/github/actions/workflow/status/startersclan/docker-sourceservers/ci-master-pr.yml?branch=master&label=&logo=github&style=flat-square
[pipeline-github-master-link]: https://github.com/startersclan/docker-sourceservers/actions?query=branch%3Amaster
[pipeline-travis-build-badge]: https://img.shields.io/travis/com/startersclan/docker-sourceservers/build.svg?label=&logo=travis&style=flat-square
[pipeline-travis-build-link]: https://app.travis-ci.com/startersclan/docker-sourceservers/builds
[pipeline-travis-update-badge]: https://img.shields.io/travis/com/startersclan/docker-sourceservers/update.svg?label=&logo=travis&style=flat-square
[pipeline-travis-update-link]: https://app.travis-ci.com/startersclan/docker-sourceservers/builds
[pipeline-azurepipelines-build-badge]: https://img.shields.io/azure-devops/build/startersclan/docker-sourceservers/2/build.svg?label=&logo=azure-pipelines&style=flat-square
[pipeline-azurepipelines-build-link]: https://dev.azure.com/startersclan/docker-sourceservers/_build?definitionId=2
[pipeline-azurepipelines-update-badge]: https://img.shields.io/azure-devops/build/startersclan/docker-sourceservers/3/update.svg?label=&logo=azure-pipelines&style=flat-square
[pipeline-azurepipelines-update-link]: https://dev.azure.com/startersclan/docker-sourceservers/_build?definitionId=3
[pipeline-circleci-build-badge]: https://img.shields.io/circleci/build/gh/startersclan/docker-sourceservers/build.svg?label=&logo=circleci&style=flat-square
[pipeline-circleci-build-link]: https://app.circleci.com/pipelines/github/startersclan/docker-sourceservers?branch=build
[pipeline-circleci-update-badge]: https://img.shields.io/circleci/build/gh/startersclan/docker-sourceservers/update.svg?label=&logo=circleci&style=flat-square
[pipeline-circleci-update-link]: https://app.circleci.com/pipelines/github/startersclan/docker-sourceservers?branch=update
[pipeline-gitlab-build-badge]: https://img.shields.io/gitlab/pipeline-status/startersclan/docker-sourceservers?branch=build&label=&logo=gitlab&style=flat-square
[pipeline-gitlab-build-link]: https://gitlab.com/startersclan/docker-sourceservers/-/pipelines?page=1&scope=all&ref=build
[pipeline-gitlab-update-badge]: https://img.shields.io/gitlab/pipeline-status/startersclan/docker-sourceservers?branch=update&label=&logo=gitlab&style=flat-square
[pipeline-gitlab-update-link]: https://gitlab.com/startersclan/docker-sourceservers/-/pipelines?page=1&scope=all&ref=update
| Game | Image | Tag ``v<tag>`` | Size |
|:-:|:-:|:-:|:-:|
| $($_.FullName) | [``$RegistryNamespace/$($_.Name)``][$engine-$($_.Name)-dockerhub-link] | [![$engine-$($_.Name)-version-badge][]][$engine-$($_.Name)-metadata-link] | [![$engine-$($_.Name)-size-badge][]][$engine-$($_.Name)-metadata-link] | [![$engine-$($_.Name)-layers-badge][]][$engine-$($_.Name)-metadata-link] |
| Game | Image | Tag ``v<tag>`` | Size | Status |
|:-:|:-:|:-:|:-:|:-:|
| $($_.FullName) | [``$RegistryNamespace/$($_.Name)``][$engine-$($_.Name)-dockerhub-link] | [![$engine-$($_.Name)-version-badge][]][$engine-$($_.Name)-metadata-link] | [![$engine-$($_.Name)-size-badge][]][$engine-$($_.Name)-metadata-link] | [![pipeline-gitlab-$engine-$($_.Name)-badge][]][pipeline-gitlab-$engine-$($_.Name)-link] |
[$engine-$($_.Name)-dockerhub-link]: https://hub.docker.com/r/$RegistryNamespace/$($_.Name)
[$engine-$($_.Name)-version-badge]: https://img.shields.io/docker/v/$RegistryNamespace/$($_.Name)/latest?label=&style=flat-square
[$engine-$($_.Name)-size-badge]: https://img.shields.io/docker/image-size/$RegistryNamespace/$($_.Name)/latest?label=&style=flat-square
[$engine-$($_.Name)-metadata-link]: https://hub.docker.com/r/$RegistryNamespace/$($_.Name)/tags
[pipeline-gitlab-$engine-$($_.Name)-badge]: https://img.shields.io/gitlab/pipeline-status/startersclan/docker-sourceservers?branch=$engine-$($_.Name)&label=&logo=gitlab&style=flat-square
[pipeline-gitlab-$engine-$($_.Name)-link]: https://gitlab.com/startersclan/docker-sourceservers/-/pipelines?page=1&scope=all&ref=$engine-$($_.Name)
"@
$content
$outFile = "$readmePath/$engine-$($_.Name).md"
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/ci-master-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ jobs:
run: |
./test/test-game-image-game-version.sh
test-generate-gitbranches:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all branches and tags
- name: Test
shell: pwsh
run: |
./Generate-GitBranches.Tests.ps1
update-draft-release:
needs:
- test-build-hlds
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.*
!/.circleci
!/.github
!/.generate
!/.gitignore
!/.gitlab-ci.yml
!/.travis.yml
38 changes: 21 additions & 17 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
stages:
- build
- post-build
- notify

build-image:
stage: build
image: docker:20.10.18-git
image: docker:20.10.23-git
# See: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker
# For stateless builds, a sidecar dockerd service is needed
# For stateful builds (i.e. DOCKER_HOST=unix:///var/run/docker.sock mounted from the host), a sidecar dockerd service is not needed
# services:
# - docker:20.10.18-dind
rules:
- if: $CI_PIPELINE_SOURCE == "trigger"
- if: $CI_PIPELINE_SOURCE == "web"
services:
- docker:20.10.23-dind
variables:
DOCKER_DRIVER: overlay2
# DOCKER_HOST: unix:///var/run/docker.sock # For stateful builds, uncomment this, or set it in CI/CD variables
rules:
# Run on all branches except master, when .trigger is added or modified
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH != "master"
changes:
- .trigger
timeout: 120m
script:
- |
export PIPELINE="$CI_COMMIT_BRANCH"
set +e
./build.sh
if [ $? = 0 ]; then
Expand All @@ -28,20 +30,22 @@ build-image:
exit 1
fi
artifacts:
name: .build.state-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
paths:
- .build.state
when: always
expire_in: never

send-notification:
stage: post-build
image: alpine:3.14.8
notify:
stage: notify
image: alpine:3.15
rules:
- if: $CI_PIPELINE_SOURCE == "trigger"
- if: $CI_PIPELINE_SOURCE == "web"
# Run on all branches except master, when .trigger is added or modified
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH != "master"
changes:
- .trigger
when: always
timeout: 5m
script:
- |
apk add --no-cache curl
export X_CI_WEBHOOK_SECRET_HEADER="x-gitlab-webhook-secret: $X_GITLAB_WEBHOOK_SECRET"
./notify.sh
- apk add --no-cache curl
- ./notify.sh
142 changes: 142 additions & 0 deletions Generate-GitBranches.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
Get-Module Pester -ListAvailable

Describe "Generate-GitBranches.ps1" {

BeforeEach {
$testDrive = "TestDrive:"
$sourceRepo = $PSScriptRoot

$games = Get-Content $PSScriptRoot/games.json -Encoding utf8 | ConvertFrom-Json -AsHashtable
$remote = 'origin'
$remoteUrl = git remote get-url $

$expectedFiles = @(
'.env'
'.gitignore'
'.gitlab-ci.yml'
'.state'
'build.sh'
'build/Dockerfile'
'notify.sh'
'update/Dockerfile'
)
}

AfterEach {
cd $sourceRepo
Remove-Item $testDrive/* -Recurse -Force
}

It "Parameter validation" {
{
cd $PSScriptRoot
& ./Generate-GitBranches.ps1 -Repo '' -ErrorAction Stop
} | Should -Throw
}

Context 'Same repo' {

BeforeEach {
$sameRepo = "$testDrive/$( (Get-Item $sourceRepo).Name )"
Copy-Item $sourceRepo $sameRepo -Recurse -Force
cd $sameRepo
git config user.name "bot"
git config user.email "[email protected]"
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
foreach ($b in $branches) {
git branch -D $b
}
}

It "Creates and updates branches of a same repo (dry-run)" {
$currentRef = git rev-parse --short HEAD
if ($LASTEXITCODE) { throw }
& ./Generate-GitBranches.ps1 -Repo . -ErrorAction Stop -WhatIf 6>$null # Create
git checkout $currentRef
& ./Generate-GitBranches.ps1 -Repo . -ErrorAction Stop -WhatIf 6>$null # Update

cd $sameRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be 0
}

It "Creates and updates branches of a same repo" {
$currentRef = git rev-parse --short HEAD
if ($LASTEXITCODE) { throw }
& ./Generate-GitBranches.ps1 -Repo $sameRepo -ErrorAction Stop 6>$null # Create
git checkout $currentRef
& ./Generate-GitBranches.ps1 -Repo $sameRepo -ErrorAction Stop 6>$null # Update

cd $sameRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be $games.Count
foreach ($b in $branches) {
git ls-tree -r --name-only $b | Should -Be $expectedFiles
}
}

It "Creates and updates branches of a same repo of (one game)" {
& $sourceRepo/Generate-GitBranches.ps1 -Repo $sameRepo -GamePlatform steam -GameEngine hlds -Game valve -ErrorAction Stop 6>$null # Create
& $sourceRepo/Generate-GitBranches.ps1 -Repo $sameRepo -Remote $remote -Pull -GamePlatform steam -GameEngine hlds -Game valve -ErrorAction Stop 6>$null # Update

cd $sameRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be 1
foreach ($b in $branches) {
git ls-tree -r --name-only $b | Should -Be $expectedFiles
}
}
}

Context 'Different repo' {

BeforeEach {
$differentRepo = "$testDrive/$( (Get-Item $sourceRepo).Name )"
New-Item $differentRepo -ItemType Directory > $null
cd $differentRepo
git init --initial-branch master
git config user.name "bot"
git config user.email "[email protected]"
git commit --allow-empty -m 'Init'
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
foreach ($b in $branches) {
git branch -D $b
}
}

It "Creates and updates branches of a different repo (dry-run)" {
& $sourceRepo/Generate-GitBranches.ps1 -Repo $differentRepo -ErrorAction Stop 6>$null -WhatIf # Create
& $sourceRepo/Generate-GitBranches.ps1 -Repo $differentRepo -ErrorAction Stop 6>$null -WhatIf # Update

cd $differentRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be 0
}

It "Creates and updates branches of a different repo" {
& $sourceRepo/Generate-GitBranches.ps1 -Repo $differentRepo -ErrorAction Stop 6>$null # Create
& $sourceRepo/Generate-GitBranches.ps1 -Repo $differentRepo -ErrorAction Stop 6>$null # Update

cd $differentRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be $games.Count
foreach ($b in $branches) {
git ls-tree -r --name-only $b | Should -Be $expectedFiles
}
}

It "Creates and updates branches of a different repo (one game)" {
& $sourceRepo/Generate-GitBranches.ps1 -Repo $differentRepo -GamePlatform steam -GameEngine hlds -Game valve -ErrorAction Stop 6>$null # Create
& $sourceRepo/Generate-GitBranches.ps1 -Repo $differentRepo -GamePlatform steam -GameEngine hlds -Game valve -ErrorAction Stop 6>$null # Update

cd $differentRepo
$branches = git branch | % { $_.Replace('*', '').Trim() } | ? { $_ -match '^steam-' }
$branches.Count | Should -Be 1
foreach ($b in $branches) {
git ls-tree -r --name-only $b | Should -Be $expectedFiles
}
}

}

}
Loading

0 comments on commit 3348f92

Please sign in to comment.