diff --git a/.github/workflows/build-publish.yaml b/.github/workflows/build-publish.yaml index 17a4a03..7f3e199 100644 --- a/.github/workflows/build-publish.yaml +++ b/.github/workflows/build-publish.yaml @@ -29,7 +29,8 @@ jobs: run: | echo "image_tags=$( .github/workflows/scripts/get-image-tags.rb \ - "${{ github.repository }}" \ + "${{ github.repository_owner }}" \ + "${{ github.event.repository.name }}" \ "${{ github.ref_name }}" \ "${{ github.ref_type }}" \ "${{ github.event.repository.default_branch }}" \ diff --git a/.github/workflows/scripts/get-image-tags.rb b/.github/workflows/scripts/get-image-tags.rb index 50746e3..1d7454c 100755 --- a/.github/workflows/scripts/get-image-tags.rb +++ b/.github/workflows/scripts/get-image-tags.rb @@ -4,15 +4,16 @@ require 'json' require_relative 'lib' def main - git_repo = ARGV[0] - git_ref_name = ARGV[1] - git_ref_type = ARGV[2] - git_default_branch = ARGV[3] + repo_owner = ARGV[0] + repo_name = ARGV[1] + git_ref_name = ARGV[2] + git_ref_type = ARGV[3] + git_default_branch = ARGV[4] # 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 = get_image_tags( diff --git a/.github/workflows/scripts/lib.rb b/.github/workflows/scripts/lib.rb index 56dc625..443fe32 100644 --- a/.github/workflows/scripts/lib.rb +++ b/.github/workflows/scripts/lib.rb @@ -40,14 +40,24 @@ def get_image_tags( end # @param registry [String] -# @param git_repo [String] +# @param username [String] # @param sub_image [String?] # @return String -def get_image_name(registry: 'ghcr.io', git_repo: nil, sub_image: nil) - git_repo = git_repo.downcase +def get_image_name( + 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 - container_repo = "#{registry}/#{git_repo}/#{sub_image ? sub_image : default_sub_image}" + case registry + 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 Semver = Struct.new('Semver', :major, :minor, :patch, :pre, :build) diff --git a/.github/workflows/scripts/test/get-image-tags.unit.rb b/.github/workflows/scripts/test/get-image-tags.unit.rb index 15536b4..f971d6a 100755 --- a/.github/workflows/scripts/test/get-image-tags.unit.rb +++ b/.github/workflows/scripts/test/get-image-tags.unit.rb @@ -71,23 +71,53 @@ class TestGetImageName < Test::Unit::TestCase assert_equal( 'ghcr.io/octocat/hello-world/hello-world', 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( 'ghcr.io/octocat/hello-world/foobar', get_image_name( - git_repo: 'Octocat/hello-world', + username: 'Octocat', + project_name: 'hello-world', sub_image: 'foobar', ), ) 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( 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