Update index #20913
This file contains 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: Update index | |
on: | |
schedule: | |
- cron: "0 */2 * * *" # every second hour | |
push: | |
branches: | |
- main | |
paths: | |
- "registry/**" | |
- ".github/workflows/update-index.yml" | |
- ".github/scripts/**" | |
env: | |
GALLERY_JSON: bsdata.catpkg-gallery.json | |
RELEASE_TAG: index-v1 | |
ENABLE_GHPAGES_UPDATE: false | |
jobs: | |
update-index: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout main | |
uses: actions/checkout@v4 | |
with: | |
ref: main | |
path: master | |
- name: Checkout index | |
uses: actions/checkout@v4 | |
with: | |
ref: index | |
path: index | |
- uses: ./master/.github/actions/install-yaml | |
- name: Compile index | |
uses: Amadevus/pwsh-script@v2 | |
id: compile | |
with: | |
script: | | |
$ErrorActionPreference = 'Stop' | |
$args = @{ | |
IndexPath = Resolve-Path ./index | |
RegistryPath = Resolve-Path ./master/registry | |
GalleryJsonPath = $env:GALLERY_JSON | |
Token = $github.token | |
} | |
./master/.github/scripts/Update-IndexCache.ps1 @args -Verbose | |
# save master's SHA to associate branches | |
"master-sha: $env:GITHUB_SHA" > index/master-sha.yml | |
- name: Upload gallery-json as workflow artifact | |
if: success() || failure() | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ env.GALLERY_JSON }} | |
path: ${{ env.GALLERY_JSON }} | |
continue-on-error: true | |
- name: Format index commit message | |
uses: Amadevus/pwsh-script@v2 | |
id: format_index_msg | |
with: | |
script: | | |
Set-Location ./index | |
# get list of changed files | |
$pkgids = @((git status --porcelain | % { $_.Substring(3) } | Split-Path -Leaf) -replace '\.catpkg\.yml$','') | |
if ($pkgids.Length -eq 0) { | |
Write-Host 'No changes in the index' -ForegroundColor Green | |
return | |
} | |
# create commit message | |
$summary = if ($pkgids.Length -eq 1) { | |
$pkgids | |
} else { | |
"{0} (+{1} more)" -f $pkgids[0],($pkgids.Length - 1) | |
} | |
$message = @" | |
Changed: $summary | |
$($pkgids -join "`n") | |
"@ | |
return $message | |
- name: Push index changes | |
uses: stefanzweifel/git-auto-commit-action@v5 | |
id: push-index | |
with: | |
commit_message: ":robot: ${{ steps.format_index_msg.outputs.result }}" | |
commit_author: '' | |
repository: index | |
- name: Replace ${{ env.GALLERY_JSON }} release asset | |
if: steps.push-index.outputs.changes_detected == 'true' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { data: release } = await github.rest.repos.getReleaseByTag({ | |
...context.repo, | |
tag: '${{ env.RELEASE_TAG }}' | |
}); | |
console.log('Release retrieved'); | |
const assetName = '${{ env.GALLERY_JSON }}'; | |
const assets = await github | |
.paginate(github.rest.repos.listReleaseAssets, { | |
...context.repo, | |
release_id: release.id | |
}); | |
const previousIndex = assets.find(x => x.name === assetName); | |
if (previousIndex) { | |
console.log('Deleting existing asset'); | |
await github.rest.repos.deleteReleaseAsset({ | |
...context.repo, | |
asset_id: previousIndex.id | |
}); | |
} | |
// upload the file | |
const fs = require('fs'); | |
const upload = await github.rest.repos.uploadReleaseAsset({ | |
...context.repo, | |
release_id: release.id, | |
name: assetName, | |
data: fs.readFileSync(assetName), | |
headers: { 'content-type': 'application/json', 'content-length': fs.statSync(assetName).size } | |
}) | |
console.log('Done'); | |
return upload; | |
- name: Checkout gh-pages | |
if: steps.push-index.outputs.changes_detected == 'true' && env.ENABLE_GHPAGES_UPDATE == 'true' | |
uses: actions/checkout@v4 | |
with: | |
ref: gh-pages | |
path: gh-pages | |
- name: Update dataindex | |
if: steps.push-index.outputs.changes_detected == 'true' && env.ENABLE_GHPAGES_UPDATE == 'true' | |
uses: Amadevus/pwsh-script@v2 | |
id: update | |
env: | |
COMPILE_RESULT: ${{ needs.update-index.outputs.compile_result }} | |
with: | |
script: | | |
$ErrorActionPreference = 'Stop' | |
$compileResult = $env:COMPILE_RESULT | ConvertFrom-Json | |
$owner, $repo = $github.repository -split '/' | |
$subpath = "dataindex" | |
$args = @{ | |
IndexPath = Resolve-Path ./index | |
DataPath = Resolve-Path (New-Item "./gh-pages/$subpath" -ItemType Directory -Force) | |
DataBaseUrl = "https://$owner.github.io/$repo/$subpath" | |
GalleryJsonPath = $env:GALLERY_JSON | |
ChangedIndexPath = $compileResult.changed_index_paths | |
Token = $github.token | |
} | |
./master/.github/scripts/Update-GhPagesData.ps1 @args -Verbose | |
- name: Push gh-pages changes | |
if: steps.push-index.outputs.changes_detected == 'true' && env.ENABLE_GHPAGES_UPDATE == 'true' | |
uses: stefanzweifel/git-auto-commit-action@v5 | |
with: | |
commit_message: ":robot: update data index" | |
repository: gh-pages |