Skip to content

Instantly share code, notes, and snippets.

@luizkowalski
Last active August 30, 2024 18:46
Show Gist options
  • Select an option

  • Save luizkowalski/6e7eb3b318dacc01c80754d2599f47ee to your computer and use it in GitHub Desktop.

Select an option

Save luizkowalski/6e7eb3b318dacc01c80754d2599f47ee to your computer and use it in GitHub Desktop.
Check if there are any new images available for Kamal accessories
#!/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