module AppRb::Util::Build

Public Class Methods

build(user, host, repo, ssh_key, target, registry, image_name, pre_build_cmds = []) click to toggle source
# 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 git@github.com:#{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

Private Class Methods

repo_cache(repo) click to toggle source
# File lib/app-rb/util/build.rb, line 43
def self.repo_cache(repo)
  repo.gsub("/", "-") + "-cache"
end