Skip to content

Instantly share code, notes, and snippets.

@djpadz
Created April 15, 2020 19:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djpadz/27652124a24f508ce85fdd8f3930a341 to your computer and use it in GitHub Desktop.
Save djpadz/27652124a24f508ce85fdd8f3930a341 to your computer and use it in GitHub Desktop.
Log in to a docker repository in AWS ECR using the current profile
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'base64'
require 'open3'
puts 'Getting authorization token...'
out, status = Open3.capture2 'aws', 'ecr', 'get-authorization-token'
exit status.exitstatus unless status.success?
token_info = JSON.parse(out)['authorizationData'][0]
username, password = Base64.decode64(token_info['authorizationToken']).split(':', 2)
endpoint = token_info['proxyEndpoint']
puts "Authenticating as #{username} to #{endpoint}..."
out, status = Open3.capture2e 'docker', 'login', '-u', username, '--password-stdin', endpoint, stdin_data: password
puts out
exit status.exitstatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment