Skip to content

Commit

Permalink
feat: refactor all CI
Browse files Browse the repository at this point in the history
  • Loading branch information
he3als committed Jun 7, 2024
1 parent 660b503 commit 9c66604
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 287 deletions.
39 changes: 9 additions & 30 deletions .github/workflows/apbx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ env:
SXSC_REPO: "https://github.com/Atlas-OS/sxsc"

jobs:
package-build:
build:
runs-on: windows-latest

steps:
Expand Down Expand Up @@ -105,44 +105,23 @@ jobs:
working-directory: src\playbook\Executables\AtlasModules\Packages
if: env.runSxsc == 'true'

build:
needs: package-build
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: Validate YAML files
run: 'yamllint -d "{extends: relaxed, rules: {empty-lines: disable, line-length: disable, new-line-at-end-of-file: disable, trailing-spaces: disable}}" src/playbook/.'

- name: Configure playbook
id: config-playbook
if: ${{ !startsWith(github.ref, 'refs/heads/na-') }}
run: |
cd src/playbook
echo "Making playbook display as unverified (remove ProductCode) so that it is not marked as malicious..."
sed -i '/<ProductCode>/d' playbook.conf
echo "Change description of playbook..."
sed -i 's|<Description>.*<\/Description>|<Description>Experimental testing version of the Atlas Playbook, built with GitHub Actions from commit ${{ github.sha }}. Be aware of these builds being potentially unstable and buggy!</Description>|g' playbook.conf
cd Configuration
echo "Enabling AME Wizard Live Log..."
sed -i '7s/ #//' custom.yml
- name: Create playbook (ZIP/APBX password is malte)
if: ${{ steps.config-playbook.outcome != 'skipped' }}
id: create-pb
if: ${{ !startsWith(github.ref, 'refs/heads/na-') }}
run: |
cd src/playbook
echo "Making a renamed password protected (malte) ZIP of playbook files..."
zip -r -P malte "Atlas Playbook ${GITHUB_SHA::8}.apbx" . -x "local-build.cmd"
echo "Move the .abpx playbook into the 'Release ZIP' to be released as an artifact with the additional files..."
mv "Atlas Playbook ${GITHUB_SHA::8}.apbx" "../release-zip"
$pbName = "Atlas Playbook $($env:GITHUB_SHA.Substring(0,8)).apbx"
& ..\local-build.ps1 -ReplaceOldPlaybook -AddLiveLog -Removals Verification, WinverRequirement -FileName $pbName
echo "Move the .abpx playbook into 'Release ZIP' to be released as an artifact with the additional files..."
Move-Item $pbName "../release-zip"
working-directory: src\playbook

- name: Upload artifact
uses: actions/upload-artifact@v4
if: ${{ steps.config-playbook.outcome != 'skipped' }}
if: ${{ steps.create-pb.outcome != 'skipped' }}
with:
name: Atlas Playbook
path: |
Expand Down
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"ms-vscode.powershell",
"ionutvmi.reg",
"redhat.vscode-xml",
"redhat.vscode-yaml"
]
}
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "Build Playbook (Test)",
"script": "& '${workspaceFolder}/src/local-build.ps1' -AddLiveLog -ReplaceOldPlaybook -Removals WinverRequirement, Verification",
"cwd": "${workspaceFolder}/src/playbook",
"args": []
},
{
"type": "PowerShell",
"request": "launch",
"name": "Build Playbook (Test w/o deps)",
"script": "& '${workspaceFolder}/src/local-build.ps1' -AddLiveLog -ReplaceOldPlaybook -Removals Dependencies, WinverRequirement, Verification",
"cwd": "${workspaceFolder}/src/playbook",
"args": []
},
{
"type": "PowerShell",
"request": "launch",
"name": "Build Playbook (Release)",
"script": "& '${workspaceFolder}/src/local-build.ps1'",
"cwd": "${workspaceFolder}/src/playbook",
"args": []
}
]
}
140 changes: 140 additions & 0 deletions src/local-build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
param (
[switch]$AddLiveLog,
[switch]$ReplaceOldPlaybook,
[switch]$DontOpenPbLocation,
[ValidateSet('Dependencies', 'Requirements', 'WinverRequirement', 'Verification', IgnoreCase = $true)]
[array]$Removals,
[string]$FileName = "Atlas Test"
)

$removals | % { Set-Variable -Name "remove$_" -Value $true }

# check 7z
if (Get-Command '7z' -EA 0) {
$7zPath = '7z'
} elseif (Get-Command '7zz' -EA 0) {
$7zPath = '7zz'
} elseif (!$IsLinux -and !$IsMacOS -and (Test-Path "$([Environment]::GetFolderPath('ProgramFiles'))\7-Zip\7z.exe")) {
$7zPath = "$([Environment]::GetFolderPath('ProgramFiles'))\7-Zip\7z.exe"
} else {
throw "This script requires 7-Zip to be installed to continue."
}

# check if playbook dir
if (!(Test-Path playbook.conf -PathType Leaf)) {
if (Test-Path playbook -PathType Container) {
$currentDir = $PWD
Set-Location playbook
if (!(Test-Path playbook.conf -PathType Leaf)) { throw "playbook.conf file not found in playbook directory." }
} else {
throw "playbook.conf file not found in the current directory."
}
}

# check if old files are in use
$apbxFileName = "$fileName.apbx"
function GetNewName {
$num = 1
while (Test-Path -Path "$fileName ($num).apbx") {
$num++
$script:apbxFileName = "$fileName ($num).abpx"
}
}
if ($replaceOldPlaybook -and (Test-Path -Path $apbxFileName)) {
try {
$stream = [System.IO.File]::Open("$PWD\$apbxFileName", 'Open', 'Read', 'Write')
$stream.Close()
Remove-Item -Path $apbxFileName -Force -EA 0
} catch {
Write-Warning "Couldn't replace '$apbxFileName', it's in use."
GetNewName
}
} elseif (Test-Path -Path $apbxFileName) {
GetNewName
}
$apbxPath = "$PWD\$fileName.apbx"

# make temp directories
$rootTemp = New-Item (Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $([System.Guid]::NewGuid())) -ItemType Directory -Force
if (!(Test-Path -Path "$rootTemp")) { throw "Failed to create temporary directory!" }
$playbookTemp = New-Item "$rootTemp\playbook" -Type Directory

try {
# remove entries in playbook config that make it awkward for testing
$patterns = @()
# 0.6.5 has a bug where it will crash without the 'Requirements' field, but all of the requirements are removed
# "<Requirements>" and # "</Requirements>"
if ($removeRequirements) {$patterns += "<Requirement>"}
if ($removeWinverRequirement) {$patterns += "<string>", "</SupportedBuilds>", "<SupportedBuilds>"}
if ($removeVerification) {$patterns += "<ProductCode>"}

$tempPbConfPath = "$playbookTemp\playbook.conf"
if ($patterns.Count -gt 0) {
Get-Content "playbook.conf" | Where-Object { $_ -notmatch ($patterns -join '|') } | Set-Content $tempPbConfPath
}

$customYmlPath = "Configuration\custom.yml"
$tempCustomYmlPath = "$playbookTemp\$customYmlPath"
if ($AddLiveLog) {
if (Test-Path $customYmlPath -PathType Leaf) {
New-Item (Split-Path $tempCustomYmlPath -Parent) -ItemType Directory -Force | Out-Null
Copy-Item -Path $customYmlPath -Destination $tempCustomYmlPath -Force
$customYml = Get-Content -Path $tempCustomYmlPath

$actionsIndex = $customYml.IndexOf('actions:')
$newCustomYml = $customYml[0..$actionsIndex] + `
" - !powerShell: {command: 'gc -Wait Logs\TIOutput.txt -EA 0 | Write-Output; pause', baseDir: true, wait: false}" + `
$customYml[($actionsIndex + 1)..($customYml.Count)]

Set-Content -Path $tempCustomYmlPath -Value $newCustomYml
} else {
Write-Error "Can't find "$customYmlPath", not adding live log."
}
}

$startYmlPath = "Configuration\atlas\start.yml"
$tempStartYmlPath = "$playbookTemp\$startYmlPath"
if ($removeDependencies) {
if (Test-Path $startYmlPath -PathType Leaf) {
New-Item (Split-Path $tempStartYmlPath -Parent) -ItemType Directory -Force | Out-Null
Copy-Item -Path $startYmlPath -Destination $tempStartYmlPath -Force
$startYml = Get-Content -Path $tempStartYmlPath

$noLocalBuildStart = $startYml.IndexOf(' ################ NO LOCAL BUILD ################')
$noLocalBuildEnd = $startYml.IndexOf(' ################ END NO LOCAL BUILD ################')
$newStartYml = $startYml[0..($noLocalBuildStart - 1)] + `
$startYml[($noLocalBuildEnd + 1)..($startYml.Count)]

Set-Content -Path $tempStartYmlPath -Value $newStartYml
} else {
Write-Error "Can't find "$startYmlPath", not removing dependencies."
}
}

# exclude files
$excludeFiles = @(
"local-build.cmd",
"*.apbx"
)
if (Test-Path $tempCustomYmlPath) { $excludeFiles += "custom.yml" }
if (Test-Path $tempStartYmlPath) { $excludeFiles += "start.yml" }
if (Test-Path $tempPbConfPath) { $excludeFiles += "playbook.conf" }
$files = "$rootTemp\7zFiles.txt"
(Get-ChildItem -File -Exclude $excludeFiles -Recurse).FullName | Resolve-Path -Relative | ForEach-Object {$_.Substring(2)} | Out-File $files -Encoding utf8

& $7zPath a -spf -y -mx1 -tzip "$apbxPath" `@"$files" | Out-Null
# add edited files
if (Test-Path "$playbookTemp\*.*") {
Push-Location "$playbookTemp"
& $7zPath u "$apbxPath" * | Out-Null
Pop-Location
}

Write-Host "Built successfully! Path: `"$apbxPath`"" -ForegroundColor Green
if (!$IsLinux -and !$IsMacOS -and !$DontOpenPbLocation) {
explorer /select,"$apbxPath"
}
} finally {
Remove-Item $rootTemp -Force -EA 0 -Recurse | Out-Null
if ($currentDir) { Set-Location $currentDir }
}
4 changes: 0 additions & 4 deletions src/playbook/Configuration/custom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
title: Root Playbook File
description: Runs all of the playbook files
actions:
# AME Wizard Live Log for development playbooks
# Do not change the line position of this, otherwise things will break when using local-build
# - !run: {exe: 'cmd.exe', args: '/c start "AME Wizard Live Log" PowerShell -NoP -C "gc -Wait Logs\TIOutput.txt -EA SilentlyContinue | Write-Output; pause"', baseDir: true, wait: false}

# Check various conditions to see if the user should run Atlas or not
- !powerShell:
command: '.\UPGRADECHECK.ps1'
Expand Down
Loading

0 comments on commit 9c66604

Please sign in to comment.