Last active
August 30, 2024 18:46
-
-
Save luizkowalski/6e7eb3b318dacc01c80754d2599f47ee to your computer and use it in GitHub Desktop.
Check if there are any new images available for Kamal accessories
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
| #!/usr/bin/env ruby | |
| # frozen_string_literal: true | |
| # lives in .kamal/update | |
| require "json" | |
| require "net/http" | |
| require "uri" | |
| require "yaml" | |
| CONFIG = YAML.load_file("config/deploy.yml") | |
| ACCESSORIES = CONFIG["accessories"] | |
| MAX_RESULTS = 30 | |
| UNSUPPORTED = ["gcr.io", "ghcr.io", "quay.io"].freeze | |
| # These suffixes are just different versions of the same image. | |
| # When it comes to versioning, we only care about the version number. | |
| def normalize_tag(tag) | |
| tag.chomp("-ubuntu").chomp("-alpine").chomp("-bookworm").chomp("-bullseye") | |
| end | |
| ACCESSORIES.each_value do |config| | |
| image, tag = config["image"].split(":") | |
| next puts "π Skipping #{image}, not supported...yet\n\n" if image.start_with?(*UNSUPPORTED) | |
| image = image.gsub("lscr.io/", "") # Remove `lscr.io/` from linuxserver images | |
| namespace, repository = image.include?("/") ? image.split("/") : ["library", image] | |
| puts "π Checking image updates for #{image} (Current version: #{tag})" | |
| url = URI("https://hub.docker.com/v2/repositories/#{namespace}/#{repository}/tags?page_size=#{MAX_RESULTS}") | |
| response = Net::HTTP.get_response(url) | |
| if response.is_a?(Net::HTTPSuccess) | |
| data = JSON.parse(response.body) | |
| local_tag = normalize_tag(tag) | |
| data["results"].each do |result| | |
| upstream_tag = normalize_tag(result["name"]) | |
| if Gem::Version.new(local_tag) < Gem::Version.new(upstream_tag) # rubocop:disable Style/IfUnlessModifier | |
| break puts " π New version available: #{result['name']}" | |
| end | |
| rescue ArgumentError # Skip, `Gem::Version.new` fails for "latest" tag, for example | |
| next | |
| end | |
| puts "\n" | |
| else | |
| puts "π£ Error: #{response.code}\n\n" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment