Module: AppRb::Util::Build

Defined in:
lib/app-rb/util/build.rb

Class Method Summary collapse

Class Method Details

.build(user, host, repo, ssh_key, target, registry, image_name, pre_build_cmds = []) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/app-rb/util/build.rb', line 2

def self.build(user, host, repo, ssh_key, target, registry, image_name, pre_build_cmds = [])
  AppRb::Util.do_it "ssh #{user}@#{host} bash <<EOF
    set -e
    tmpfile=\\$(mktemp /tmp/git-ssh-#{repo_cache(repo)}.XXXXXX)
    echo '#!/bin/sh' > \\$tmpfile
    echo 'exec /usr/bin/ssh -o StrictHostKeyChecking=no -i #{ssh_key} \"\\$@\"' >> \\$tmpfile
    chmod +x \\$tmpfile
    export GIT_SSH=\\$tmpfile
    if [ -d #{repo_cache(repo)} ]; then
      echo update cache...
      cd #{repo_cache(repo)}
      git checkout . && git clean -dfx && git checkout master && git pull
      git branch | grep -v master | xargs -r git branch -D
    else
      echo clone...
      git clone [email protected]:#{repo} #{repo_cache(repo)} && cd #{repo_cache(repo)}
    fi
    git checkout #{target}
    rm \\$tmpfile\nEOF"

  hash = AppRb::Util.just_cmd("ssh #{user}@#{host} 'cd #{repo_cache(repo)} && git rev-parse HEAD'")
  tags = AppRb::Util::Registry.tags_list(registry, image_name)
  puts "hash: #{hash}"
  puts "tags: #{tags.inspect}"

  unless tags.index(hash)
    puts AppRb::Util.blue("+++ BUILD image")
    AppRb::Util.do_it "ssh #{user}@#{host} bash <<EOF
      set -e
      cd #{repo_cache(repo)}
      #{pre_build_cmds.join("\n")}
      docker build -t #{registry}/#{image_name}:#{hash} .
      docker push #{registry}/#{image_name}:#{hash}
      echo Done.\nEOF"
  end

  return hash
end