Method: DockerSpec#push

Defined in:
lib/docker/spec.rb

#pushObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/docker/spec.rb', line 50

def push
  @config[:push_container] = get_config(:push_container, 'DOCKER_SPEC_PUSH_CONTAINER',
                                                   'Push new tag? ')
  if @config[:push_container]

    @config[:tag_db] ||
      fail('tag_db is not defined in docker_spec.yml')

    # Load credentials from config file, or default docker config
    if @config[:dockerhub]
      @config[:dockerhub][:username] ||
        fail('dockerhub->username is not defined in docker_spec.yml')
      @config[:dockerhub][:password] ||
        fail('dockerhub->password is not defined in docker_spec.yml')
      @config[:dockerhub][:email] ||
        fail('dockerhub->email is not defined in docker_spec.yml')
    else
      @config[:dockerhub] = Hash.new
      docker_auth = JSON.parse(File.read(File.expand_path(DOCKER_AUTH_FILE)))
      auth_base64 = docker_auth['auths']['https://index.docker.io/v1/']['auth']
      @config[:dockerhub][:username] = Base64.decode64(auth_base64).split(':').first
      @config[:dockerhub][:password] = Base64.decode64(auth_base64).split(':').last
      @config[:dockerhub][:email] = docker_auth['auths']['https://index.docker.io/v1/']['email']
    end

    # Open key value store and get the current tag for this repo
    store = Moneta.new(:YAML, file: File.expand_path(@config[:tag_db]))
    current_tag = store.key?(@config[:image_name]) ? store[@config[:image_name]].to_i : 0
    new_tag = current_tag + 1

    image = Docker::Image.all.detect do |i|
      i.info['RepoTags'].include?(@config[:image_name] + ':latest')
    end

    # Login to docker hub and push latest and new_tag
    Docker.authenticate! username: @config[:dockerhub][:username],
                         password: @config[:dockerhub][:password],
                         email: @config[:dockerhub][:email]

    image.tag repo: @config[:image_name], tag: new_tag, force: true
    puts "\nINFO: pushing #{@config[:image_name]}:#{new_tag} to DockerHub"
    image.push nil, tag: new_tag

    image.tag repo: @config[:image_name], tag: 'latest', force: true
    puts "INFO: pushing #{@config[:image_name]}:latest to DockerHub"
    image.push nil, tag: 'latest'

    # Store the new tag in the tag_db
    store[@config[:image_name]] = new_tag
    store.close
  end
end