Run Automation Scripts Daily #175
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Run Automation Scripts Daily | |
| on: | |
| schedule: | |
| - cron: "0 3 * * *" | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| concurrency: | |
| group: run-scripts-daily-${{ github.ref }} | |
| cancel-in-progress: false | |
| jobs: | |
| discover-update-scripts: | |
| runs-on: ubuntu-slim | |
| outputs: | |
| has_scripts: ${{ steps.discover.outputs.has_scripts }} | |
| matrix: ${{ steps.discover.outputs.matrix }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v7 | |
| - name: Discover update scripts | |
| id: discover | |
| shell: pwsh | |
| run: | | |
| $scriptFiles = Get-ChildItem -Path 'Scripts' -Filter 'Update-*.ps1' -File | Sort-Object -Property Name | |
| if (-not $scriptFiles -or $scriptFiles.Count -eq 0) { | |
| "has_scripts=false" >> $env:GITHUB_OUTPUT | |
| "matrix={""include"":[]}" >> $env:GITHUB_OUTPUT | |
| exit 0 | |
| } | |
| $include = foreach ($file in $scriptFiles) { | |
| $runsOn = if ($file.Name -eq 'Update-OSCatalog.ps1') { 'windows-latest' } else { 'ubuntu-slim' } | |
| $requires7Zip = $file.Name -in @('Update-DellCatalog.ps1', 'Update-HPCatalog.ps1') | |
| [pscustomobject]@{ | |
| script_path = "Scripts/$($file.Name)" | |
| artifact_name = "cache-update-$($file.BaseName)" | |
| runs_on = $runsOn | |
| requires_7zip = $requires7Zip | |
| } | |
| } | |
| $matrix = @{ include = @($include) } | ConvertTo-Json -Compress | |
| "has_scripts=true" >> $env:GITHUB_OUTPUT | |
| "matrix=$matrix" >> $env:GITHUB_OUTPUT | |
| run-update-scripts: | |
| needs: discover-update-scripts | |
| if: needs.discover-update-scripts.outputs.has_scripts == 'true' | |
| runs-on: ${{ matrix.runs_on }} | |
| strategy: | |
| fail-fast: false | |
| matrix: ${{ fromJson(needs.discover-update-scripts.outputs.matrix) }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v7 | |
| - name: Install 7-Zip | |
| if: runner.os == 'Linux' && matrix.requires_7zip | |
| shell: pwsh | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y p7zip-full | |
| - name: Ensure 7-Zip | |
| if: runner.os == 'Windows' && matrix.requires_7zip | |
| shell: pwsh | |
| run: | | |
| $sevenZipPath = 'C:\Program Files\7-Zip' | |
| if (Test-Path -Path $sevenZipPath) { | |
| $sevenZipPath | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append | |
| } | |
| $sevenZipCommand = Get-Command -Name '7z' -ErrorAction SilentlyContinue | |
| if (-not $sevenZipCommand) { | |
| throw '7-Zip is not available on the Windows runner.' | |
| } | |
| - name: Execute script | |
| shell: pwsh | |
| run: pwsh -NoProfile -File "${{ matrix.script_path }}" | |
| - name: Build patch artifact from changes | |
| id: patch | |
| shell: pwsh | |
| run: | | |
| git add -A | |
| git diff --cached --quiet | |
| if ($LASTEXITCODE -eq 0) { | |
| "has_changes=false" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| git reset | |
| exit 0 | |
| } | |
| $patchPath = "${{ matrix.artifact_name }}.patch" | |
| git diff --cached --binary --output="$patchPath" | |
| git reset | |
| "has_changes=true" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| - name: Upload patch artifact | |
| if: steps.patch.outputs.has_changes == 'true' | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: ${{ matrix.artifact_name }} | |
| path: ${{ matrix.artifact_name }}.patch | |
| if-no-files-found: error | |
| run-unified: | |
| needs: [discover-update-scripts, run-update-scripts] | |
| if: needs.discover-update-scripts.outputs.has_scripts == 'true' && needs.run-update-scripts.result == 'success' | |
| runs-on: ubuntu-slim | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v7 | |
| - name: Execute unified build | |
| shell: pwsh | |
| run: pwsh -NoProfile -File "Scripts/Build-UnifiedDriverPackCatalog.ps1" | |
| - name: Build patch artifact from changes | |
| id: patch | |
| shell: pwsh | |
| run: | | |
| git add -A | |
| git diff --cached --quiet | |
| if ($LASTEXITCODE -eq 0) { | |
| "has_changes=false" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| git reset | |
| exit 0 | |
| } | |
| $patchPath = "cache-build-unified.patch" | |
| git diff --cached --binary --output="$patchPath" | |
| git reset | |
| "has_changes=true" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| - name: Upload patch artifact | |
| if: steps.patch.outputs.has_changes == 'true' | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: cache-build-unified | |
| path: cache-build-unified.patch | |
| if-no-files-found: error | |
| commit-updates: | |
| needs: [discover-update-scripts, run-update-scripts, run-unified] | |
| if: needs.discover-update-scripts.outputs.has_scripts == 'true' && needs.run-update-scripts.result == 'success' && needs.run-unified.result == 'success' | |
| runs-on: ubuntu-slim | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v7 | |
| with: | |
| fetch-depth: 0 | |
| - name: Download patch artifacts | |
| uses: actions/download-artifact@v8 | |
| continue-on-error: true | |
| with: | |
| pattern: cache-* | |
| merge-multiple: true | |
| path: patches | |
| - name: Apply patches | |
| shell: pwsh | |
| run: | | |
| if (-not (Test-Path -Path 'patches')) { | |
| Write-Host 'No patch artifacts directory found.' | |
| exit 0 | |
| } | |
| $patchFiles = @(Get-ChildItem -Path 'patches' -Filter '*.patch' -File | Sort-Object -Property Name) | |
| if ($patchFiles.Count -eq 0) { | |
| Write-Host 'No script changes detected from artifacts.' | |
| exit 0 | |
| } | |
| foreach ($patchFile in $patchFiles) { | |
| git apply --index --whitespace=nowarn $patchFile.FullName | |
| } | |
| - name: Commit and push changes | |
| shell: pwsh | |
| run: | | |
| git diff --cached --quiet | |
| if ($LASTEXITCODE -eq 0) { | |
| Write-Host 'No changes to commit after patch apply.' | |
| exit 0 | |
| } | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| $timestamp = (Get-Date).ToUniversalTime().ToString('yyyy-MM-dd') | |
| git commit -m "[$timestamp] Update automation" | |
| git push origin "HEAD:$env:GITHUB_REF_NAME" |