github_packages: support --keep-old

This commit is contained in:
Bo Anderson 2021-04-09 16:39:28 +01:00
parent 79940b2a27
commit 57e4209e9c
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
2 changed files with 72 additions and 23 deletions

View File

@ -164,6 +164,7 @@ module Homebrew
github_org = args.github_org || "homebrew" github_org = args.github_org || "homebrew"
github_packages = GitHubPackages.new(org: github_org) github_packages = GitHubPackages.new(org: github_org)
github_packages.upload_bottles(bottles_hash, github_packages.upload_bottles(bottles_hash,
keep_old: args.keep_old?,
dry_run: args.dry_run?, dry_run: args.dry_run?,
warn_on_error: args.warn_on_upload_failure?) warn_on_error: args.warn_on_upload_failure?)
else else

View File

@ -47,8 +47,15 @@ class GitHubPackages
ENV["HOMEBREW_FORCE_HOMEBREW_ON_LINUX"] = "1" if @github_org == "homebrew" && !OS.mac? ENV["HOMEBREW_FORCE_HOMEBREW_ON_LINUX"] = "1" if @github_org == "homebrew" && !OS.mac?
end end
sig { params(bottles_hash: T::Hash[String, T.untyped], dry_run: T::Boolean, warn_on_error: T::Boolean).void } sig {
def upload_bottles(bottles_hash, dry_run:, warn_on_error:) params(
bottles_hash: T::Hash[String, T.untyped],
keep_old: T::Boolean,
dry_run: T::Boolean,
warn_on_error: T::Boolean,
).void
}
def upload_bottles(bottles_hash, keep_old:, dry_run:, warn_on_error:)
user = Homebrew::EnvConfig.github_packages_user user = Homebrew::EnvConfig.github_packages_user
token = Homebrew::EnvConfig.github_packages_token token = Homebrew::EnvConfig.github_packages_token
@ -75,7 +82,7 @@ class GitHubPackages
bottles_hash.each do |formula_full_name, bottle_hash| bottles_hash.each do |formula_full_name, bottle_hash|
upload_bottle(user, token, skopeo, formula_full_name, bottle_hash, upload_bottle(user, token, skopeo, formula_full_name, bottle_hash,
dry_run: dry_run, warn_on_error: warn_on_error) keep_old: keep_old, dry_run: dry_run, warn_on_error: warn_on_error)
end end
end end
@ -194,7 +201,18 @@ class GitHubPackages
exit 1 exit 1
end end
def upload_bottle(user, token, skopeo, formula_full_name, bottle_hash, dry_run:, warn_on_error:) def download(user, token, skopeo, image_uri, root, dry_run:)
puts
args = ["copy", "--all", image_uri.to_s, "oci:#{root}"]
if dry_run
puts "#{skopeo} #{args.join(" ")} --src-creds=#{user}:$HOMEBREW_GITHUB_PACKAGES_TOKEN"
else
args << "--src-creds=#{user}:#{token}"
system_command!(skopeo, verbose: true, print_stdout: true, args: args)
end
end
def upload_bottle(user, token, skopeo, formula_full_name, bottle_hash, keep_old:, dry_run:, warn_on_error:)
formula_name = bottle_hash["formula"]["name"] formula_name = bottle_hash["formula"]["name"]
_, org, repo, = *bottle_hash["bottle"]["root_url"].match(URL_REGEX) _, org, repo, = *bottle_hash["bottle"]["root_url"].match(URL_REGEX)
@ -215,7 +233,10 @@ class GitHubPackages
else else
inspect_args << "--creds=#{user}:#{token}" inspect_args << "--creds=#{user}:#{token}"
inspect_result = system_command(skopeo, print_stderr: false, args: inspect_args) inspect_result = system_command(skopeo, print_stderr: false, args: inspect_args)
if inspect_result.status.success? if keep_old
# Tag doesn't exist so ignore --keep-old
keep_old = false unless inspect_result.status.success?
elsif inspect_result.status.success?
if warn_on_error if warn_on_error
opoo "#{image_uri} already exists, skipping upload!" opoo "#{image_uri} already exists, skipping upload!"
return return
@ -228,7 +249,11 @@ class GitHubPackages
root = Pathname("#{formula_name}--#{version_rebuild}") root = Pathname("#{formula_name}--#{version_rebuild}")
FileUtils.rm_rf root FileUtils.rm_rf root
write_image_layout(root) if keep_old
download(user, token, skopeo, image_uri, root, dry_run: dry_run)
else
write_image_layout(root)
end
blobs = root/"blobs/sha256" blobs = root/"blobs/sha256"
blobs.mkpath blobs.mkpath
@ -253,24 +278,49 @@ class GitHubPackages
end end
created_date = bottle_hash["bottle"]["date"] created_date = bottle_hash["bottle"]["date"]
formula_annotations_hash = { if keep_old
"com.github.package.type" => GITHUB_PACKAGE_TYPE, index = JSON.parse((root/"index.json").read)
"org.opencontainers.image.created" => created_date, image_index_sha256 = index["manifests"].first["digest"].delete_prefix("sha256:")
"org.opencontainers.image.description" => bottle_hash["formula"]["desc"], image_index = JSON.parse((blobs/image_index_sha256).read)
"org.opencontainers.image.documentation" => documentation, (blobs/image_index_sha256).unlink
"org.opencontainers.image.license" => bottle_hash["formula"]["license"],
"org.opencontainers.image.ref.name" => version_rebuild,
"org.opencontainers.image.revision" => git_revision,
"org.opencontainers.image.source" => source,
"org.opencontainers.image.title" => formula_full_name,
"org.opencontainers.image.url" => bottle_hash["formula"]["homepage"],
"org.opencontainers.image.vendor" => org,
"org.opencontainers.image.version" => version,
}.reject { |_, v| v.blank? }
manifests = bottle_hash["bottle"]["tags"].map do |bottle_tag, tag_hash| formula_annotations_hash = image_index["annotations"]
manifests = image_index["manifests"]
else
formula_annotations_hash = {
"com.github.package.type" => GITHUB_PACKAGE_TYPE,
"org.opencontainers.image.created" => created_date,
"org.opencontainers.image.description" => bottle_hash["formula"]["desc"],
"org.opencontainers.image.documentation" => documentation,
"org.opencontainers.image.license" => bottle_hash["formula"]["license"],
"org.opencontainers.image.ref.name" => version_rebuild,
"org.opencontainers.image.revision" => git_revision,
"org.opencontainers.image.source" => source,
"org.opencontainers.image.title" => formula_full_name,
"org.opencontainers.image.url" => bottle_hash["formula"]["homepage"],
"org.opencontainers.image.vendor" => org,
"org.opencontainers.image.version" => version,
}.reject { |_, v| v.blank? }
manifests = []
end
processed_image_refs = Set.new
manifests.each do |manifest|
processed_image_refs << manifest["annotations"]["org.opencontainers.image.ref.name"]
end
manifests += bottle_hash["bottle"]["tags"].map do |bottle_tag, tag_hash|
bottle_tag = Utils::Bottles::Tag.from_symbol(bottle_tag.to_sym) bottle_tag = Utils::Bottles::Tag.from_symbol(bottle_tag.to_sym)
tag = GitHubPackages.version_rebuild(version, rebuild, bottle_tag.to_s)
if processed_image_refs.include?(tag)
puts
odie "A bottle JSON for #{bottle_tag} is present, but it is already in the image index!"
else
processed_image_refs << tag
end
local_file = tag_hash["local_filename"] local_file = tag_hash["local_filename"]
odebug "Uploading #{local_file}" odebug "Uploading #{local_file}"
@ -316,8 +366,6 @@ class GitHubPackages
formulae_dir = tag_hash["formulae_brew_sh_path"] formulae_dir = tag_hash["formulae_brew_sh_path"]
documentation = "https://formulae.brew.sh/#{formulae_dir}/#{formula_name}" if formula_core_tap documentation = "https://formulae.brew.sh/#{formulae_dir}/#{formula_name}" if formula_core_tap
tag = GitHubPackages.version_rebuild(version, rebuild, bottle_tag.to_s)
descriptor_annotations_hash = { descriptor_annotations_hash = {
"org.opencontainers.image.ref.name" => tag, "org.opencontainers.image.ref.name" => tag,
"sh.brew.bottle.cpu.variant" => cpu_variant, "sh.brew.bottle.cpu.variant" => cpu_variant,