Generated posts not shown in index

I have a custom Generator that generates posts, which works as expected. I have a gitlab pipeline and the resulting posts are not shown in my page, only if I run the pipeline twice and the posts are there right at the beginning. So I assume this is an issue with the order of post generation and index rendering. What should I change?

Thanks a lot in advance!
Progsi

My Generator Code

# _plugins/post_generator.rb

module Jekyll
  class PostGenerator < Generator
    def generate(site)
      # Your logic for generating posts here
      source_dir = site.config['source']
      notebooks_dir = File.join(source_dir, 'notebooks')
      posts_dir = File.join(source_dir, '_posts')

      FileUtils.mkdir_p(posts_dir) # Create _posts directory if it doesn't exist

      notebooks = Dir.glob(File.join(notebooks_dir, '**', '*.ipynb'))

      notebooks.each do |notebook|
        notebook_basename = File.basename(notebook, '.*')
        post_content = <<~POST
          ---
          layout: post
          title: #{notebook_basename}
          permalink: /notebooks/:title
          ---

          [Download Notebook](https:/MYWEBSITE/#{notebook_basename}.ipynb?ref_type=heads&inline=false)

          {% jupyter_notebook "#{notebook_basename}.ipynb" %}
        POST

        post_filename = File.join(posts_dir, "#{Time.now.strftime('%Y-%m-%d')}-#{notebook_basename}.md")
        File.write(post_filename, post_content)
      end
    end
  end
end

My index markdown code

---
# Feel free to add content and custom Front Matter to this file.
# To modify the layout, see https://jekyllrb.com/docs/themes/#overriding-theme-defaults

layout: default
---


{% assign sorted_posts = site.posts | sort: 'title' %}

{% for post in sorted_posts %}
  - [{{ post.title }}]({{ post.url | relative_url }})
{% endfor %}