-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: legg til ny workflow for å varsla abonnentar når nytt innlegg bli…
…r publisert
- Loading branch information
1 parent
0c78b82
commit f9563ff
Showing
7 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Notify subscribers of new posts | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
|
||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
pages: write | ||
id-token: write | ||
|
||
concurrency: | ||
group: 'pages' | ||
cancel-in-progress: false | ||
|
||
jobs: | ||
notify: | ||
runs-on: ubuntu-latest | ||
environment: subscriber-notification | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
bundler-cache: true | ||
cache-version: 0 # Increase this version to clear the cache | ||
|
||
- name: Post-install script | ||
run: rake postinstall | ||
|
||
- name: Setup Pages | ||
id: pages | ||
uses: actions/configure-pages@v4 | ||
|
||
- name: Build with Jekyll | ||
run: bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path }}" | ||
env: | ||
JEKYLL_ENV: production | ||
|
||
- name: Notify subscribers | ||
run: rake notify_new_post | ||
env: | ||
API_KEY: ${{ secrets.API_KEY }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ _sass/bootstrap | |
assets/js/bootstrap | ||
assets/js/lunr.js | ||
search_index.json | ||
new_post.json | ||
secrets.yml |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ pages: | |
url: / | ||
- title: Om | ||
url: /om/ | ||
- title: Abonner | ||
url: /subscribe/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'json' | ||
|
||
module Jekyll | ||
class LatestPostGenerator < Generator | ||
safe true | ||
priority :low | ||
|
||
def generate(site) | ||
puts "Jekyll New Post JSON Generator: Checking for new posts" | ||
|
||
begin | ||
File.delete('new_post.json') | ||
rescue | ||
end | ||
|
||
# Check for new files in the last commit in the _posts directory | ||
changed_files = `git diff-tree --no-commit-id --name-status -r HEAD`.lines | ||
new_posts = changed_files.select { |line| line.start_with?('A') && line.include?('_posts/') } | ||
|
||
if new_posts.empty? | ||
puts 'No new post was created' | ||
return | ||
end | ||
|
||
# map the new posts to their jekyll data | ||
new_posts = new_posts.map do |line| | ||
file = line.split("\t").last.strip | ||
post = site.posts.docs.find { |p| p.relative_path == file } | ||
|
||
if post.nil? | ||
next | ||
end | ||
|
||
{ | ||
title: post.data['title'], | ||
date: post.date, | ||
author: site.data['authors'][post.data['author']], | ||
url: site.config['url'] + post.url, | ||
summary: post.data['summary'] | ||
} | ||
end | ||
new_posts = new_posts.compact | ||
|
||
new_post = new_posts.max_by { |post| post[:date] } | ||
|
||
# safety check so we don't send accidentally notify about old posts | ||
# if the latest post is not from within the last 24 hours, don't notify | ||
if new_post[:date] < Time.now - 24 * 60 * 60 | ||
puts 'Latest post is older than 24 hours, not notifying' | ||
return | ||
end | ||
|
||
# Write to a JSON file in the repository root | ||
write_path = File.join('new_post.json') | ||
File.open(write_path, 'w') do |file| | ||
file.write(JSON.pretty_generate(new_post)) | ||
end | ||
end | ||
end | ||
end |