🐛 clean up image name

This commit is contained in:
Louis Orleans 2023-09-01 12:21:01 -07:00
parent 98e188a32f
commit 26ae5311d9
No known key found for this signature in database
4 changed files with 58 additions and 16 deletions

View File

@ -29,7 +29,8 @@ jobs:
run: | run: |
echo "image_tags=$( echo "image_tags=$(
.github/workflows/scripts/get-image-tags.rb \ .github/workflows/scripts/get-image-tags.rb \
"${{ github.repository }}" \ "${{ github.repository_owner }}" \
"${{ github.event.repository.name }}" \
"${{ github.ref_name }}" \ "${{ github.ref_name }}" \
"${{ github.ref_type }}" \ "${{ github.ref_type }}" \
"${{ github.event.repository.default_branch }}" \ "${{ github.event.repository.default_branch }}" \

View File

@ -4,15 +4,16 @@ require 'json'
require_relative 'lib' require_relative 'lib'
def main def main
git_repo = ARGV[0] repo_owner = ARGV[0]
git_ref_name = ARGV[1] repo_name = ARGV[1]
git_ref_type = ARGV[2] git_ref_name = ARGV[2]
git_default_branch = ARGV[3] git_ref_type = ARGV[3]
git_default_branch = ARGV[4]
# log to stderr so that stdout only contains the full tags # log to stderr so that stdout only contains the full tags
$stderr.puts "'#{git_repo}', '#{git_ref_name}', '#{git_ref_type}', '#{git_default_branch}'" $stderr.puts "'#{repo_owner}', '#{repo_name}', '#{git_ref_name}', '#{git_ref_type}', '#{git_default_branch}'"
image_name = get_image_name(git_repo: git_repo) image_name = get_image_name(username: repo_owner, project_name: repo_name)
tags = tags =
get_image_tags( get_image_tags(

View File

@ -40,14 +40,24 @@ def get_image_tags(
end end
# @param registry [String] # @param registry [String]
# @param git_repo [String] # @param username [String]
# @param sub_image [String?] # @param sub_image [String?]
# @return String # @return String
def get_image_name(registry: 'ghcr.io', git_repo: nil, sub_image: nil) def get_image_name(
git_repo = git_repo.downcase registry: 'ghcr.io',
username: nil,
project_name: nil,
sub_image: nil
)
username = username.downcase
project_name = project_name.downcase.gsub(/^docker-/, '')
default_sub_image = File.basename git_repo case registry
container_repo = "#{registry}/#{git_repo}/#{sub_image ? sub_image : default_sub_image}" when 'ghcr.io'
container_repo = "#{registry}/#{username}/#{project_name}/#{sub_image ? sub_image : project_name}"
when 'docker.io'
container_repo = "#{registry}/#{username}/#{project_name}#{sub_image ? "-#{sub_image}" : ''}"
end
end end
Semver = Struct.new('Semver', :major, :minor, :patch, :pre, :build) Semver = Struct.new('Semver', :major, :minor, :patch, :pre, :build)

View File

@ -71,23 +71,53 @@ class TestGetImageName < Test::Unit::TestCase
assert_equal( assert_equal(
'ghcr.io/octocat/hello-world/hello-world', 'ghcr.io/octocat/hello-world/hello-world',
get_image_name( get_image_name(
git_repo: 'Octocat/hello-world', username: 'Octocat',
project_name: 'hello-world',
),
)
assert_equal(
'ghcr.io/octocat/hello-world/hello-world',
get_image_name(
username: 'Octocat',
project_name: 'docker-hello-world',
), ),
) )
assert_equal( assert_equal(
'ghcr.io/octocat/hello-world/foobar', 'ghcr.io/octocat/hello-world/foobar',
get_image_name( get_image_name(
git_repo: 'Octocat/hello-world', username: 'Octocat',
project_name: 'hello-world',
sub_image: 'foobar', sub_image: 'foobar',
), ),
) )
assert_equal( assert_equal(
'docker.io/octocat/hello-world/hello-world', 'ghcr.io/octocat/hello-world/foo',
get_image_name(
username: 'Octocat',
project_name: 'hello-world',
sub_image: 'foo'
),
)
assert_equal(
'docker.io/octocat/hello-world',
get_image_name( get_image_name(
registry: 'docker.io', registry: 'docker.io',
git_repo: 'Octocat/hello-world', username: 'Octocat',
project_name: 'hello-world',
),
)
assert_equal(
'docker.io/octocat/hello-world-foo',
get_image_name(
registry: 'docker.io',
username: 'Octocat',
project_name: 'hello-world',
sub_image: 'foo'
), ),
) )
end end