Images READMEs Generation #10
Workflow file for this run
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: Images READMEs Generator | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: | |
| - "images-readmes-generation" | |
| paths: | |
| - 'images/**' | |
| pull_request: | |
| branches: | |
| - "main" | |
| jobs: | |
| generate_readmes: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.head_ref || github.ref }} | |
| - name: Install ExifTool | |
| run: | | |
| sudo tee /etc/dpkg/dpkg.cfg.d/01_nodoc > /dev/null << 'EOF' | |
| path-exclude /usr/share/doc/* | |
| path-exclude /usr/share/man/* | |
| path-exclude /usr/share/info/* | |
| EOF | |
| sudo apt-get update | |
| sudo apt-get install -y libimage-exiftool-perl | |
| - name: Generate README.md files | |
| shell: pwsh | |
| run: | | |
| $imagesRoot = "images" | |
| # Use GITHUB_HEAD_REF if available (for PRs), otherwise GITHUB_REF_NAME (for pushes to branches) | |
| $branchOrRefName = if ($env:GITHUB_HEAD_REF) { $env:GITHUB_HEAD_REF } else { $env:GITHUB_REF_NAME } | |
| $repoFullName = $env:GITHUB_REPOSITORY | |
| $githubWorkspace = $env:GITHUB_WORKSPACE | |
| Write-Host "Starting Images READMEs Generator" | |
| Write-Host "Repository: $repoFullName, Branch/Ref: $branchOrRefName, Workspace: $githubWorkspace" | |
| $subDirectories = Get-ChildItem -Path $imagesRoot -Directory -Recurse | |
| $processedDirectories = 0 | |
| $createdReadmes = 0 | |
| $skippedDirectories = 0 | |
| foreach ($dirInfo in $subDirectories) { | |
| $processedDirectories++ | |
| $currentDirFullPath = $dirInfo.FullName | |
| # Get directory path relative to the repository root for display | |
| $currentDirRelativePath = $currentDirFullPath.Replace($githubWorkspace, '').TrimStart('\/') | |
| Write-Host "Processing directory: $currentDirRelativePath" | |
| # Get only image files in the current directory (not subdirectories) | |
| $imageFiles = Get-ChildItem -Path $currentDirFullPath -File -Include *.png | |
| Write-Host "Found $($imageFiles.Count) image(s) in '$currentDirRelativePath'" | |
| if ($imageFiles.Count -eq 0) { | |
| Write-Host "No image files found in '$currentDirRelativePath'. Skipping README generation for this directory." | |
| $skippedDirectories++ | |
| continue | |
| } | |
| $createdReadmes++ | |
| $readmePath = Join-Path -Path $currentDirFullPath -ChildPath "README.md" | |
| $markdownContent = "# Images in $($dirInfo.Name)`n`n" | |
| $markdownContent += "| Preview | Filename | Direct Link | License |`n" | |
| $markdownContent += "|---------|----------|-------------|---------|`n" | |
| foreach ($imageFile in $imageFiles) { | |
| $imageName = $imageFile.Name | |
| $imageName = $imageName -replace '(_original|_square|_landscape|_portrait)\.png$', '' | |
| # Construct path relative to GITHUB_WORKSPACE for raw URL, ensuring Unix-style separators | |
| $repoRelativeImagePath = ($imageFile.FullName.Replace($githubWorkspace, '')).TrimStart('\/').Replace('\', '/') | |
| $baseRawUrl = "https://raw.githubusercontent.com/$repoFullName/$branchOrRefName" | |
| $rawUrl = "$baseRawUrl/$repoRelativeImagePath" | |
| # Attempt to get copyright information from EXIF data | |
| $exifOutput = exiftool -s3 -PNG:Copyright $imageFile.FullName 2>&1 | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "Error retrieving EXIF data for file '$($imageFile.FullName)': $exifOutput" | |
| $copyright = "unknown" | |
| } else { | |
| $copyright = $exifOutput.Trim() | |
| } | |
| $markdownContent += "|  | $imageName | [$rawUrl]($rawUrl) | $copyright |`n" | |
| } | |
| # Write the README only if there are images | |
| Write-Host "Writing README to '$readmePath'" | |
| Set-Content -Path $readmePath -Value $markdownContent -Encoding UTF8 | |
| } | |
| Write-Host "Summary:" | |
| Write-Host "Directories processed: $processedDirectories" | |
| Write-Host "READMEs created: $createdReadmes" | |
| Write-Host "Directories skipped: $skippedDirectories" | |
| Write-Host "Finished generating README files." | |
| - name: Commit and push README files | |
| uses: stefanzweifel/git-auto-commit-action@v5 | |
| with: | |
| commit_message: "Auto-generate image READMEs" | |
| file_pattern: "images/**/README.md" | |
| commit_user_name: "github-actions[bot]" | |
| commit_user_email: "github-actions[bot]@users.noreply.github.com" | |
| commit_author: "github-actions[bot] <github-actions[bot]@users.noreply.github.com>" |