Skip to content

Update update-readme.yaml #128

Update update-readme.yaml

Update update-readme.yaml #128

name: Update README
on:
push:
branches:
- gh-pages
- main
jobs:
update_readme:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Perl
uses: shogo82148/actions-setup-perl@v1
with:
perl-version: '5.32'
- name: Install dependencies
run: cpanm YAML::Tiny
- name: Update README.md
run: |
perl -e '
use strict;
use warnings;
use YAML::Tiny;
# Read the index.yaml file
my $yaml = YAML::Tiny->read("index.yaml");
# Get the entries
my %entries = %{$yaml->[0]->{entries}};
# Find the latest entry for each app
my %latest_entries;
foreach my $app (keys %entries) {
my @sorted_entries = sort { $b->{version} cmp $a->{version} } @{$entries{$app}};
$latest_entries{$app} = $sorted_entries[0];
}
# Update the README.md file
my $readme_file = "README.md";
my $readme_content = read_file($readme_file);
# Generate the new content
my $new_content = "## Latest Releases\n\n";
foreach my $app (sort keys %latest_entries) {
my $entry = $latest_entries{$app};
$new_content .= "### $app\n";
$new_content .= "- Version: $entry->{version}\n";
$new_content .= "- URL: $entry->{urls}[0]\n\n";
}
# Check if the "## Latest Releases" section exists in the README
if (index($readme_content, "## Latest Releases") == -1) {
# If the section doesn'"'"'t exist, append the new content to the README
$readme_content .= "\n$new_content";
} else {
# If the section exists, replace it with the new content
$readme_content =~ s/## Latest Releases.*/$new_content/s;
}
# Write the updated content back to the file
write_file($readme_file, $readme_content);
# Set the flag to indicate changes were made
print "::set-output name=changes_made::true\n";
sub read_file {
my ($file) = @_;
open my $fh, "<", $file or die "Could not open file \"$file\": $!";
local $/;
my $content = <$fh>;
close $fh;
return $content;
}
sub write_file {
my ($file, $content) = @_;
open my $fh, ">", $file or die "Could not open file \"$file\": $!";
print $fh $content;
close $fh;
}
'
id: update_readme
- name: Commit changes
if: steps.update_readme.outputs.changes_made == 'true'
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add README.md
git commit -m "Update README.md with the latest releases"
- name: Push changes
if: steps.update_readme.outputs.changes_made == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: gh-pages