class DPL::Provider::Pages

Public Class Methods

new(context, options) click to toggle source
Calls superclass method
# File lib/dpl/provider/pages.rb, line 27
def initialize(context, options)
  super

  @build_dir = File.absolute_path(options[:local_dir] || '.', src_dir)
  print_step "The target dir for deployment is '#{@build_dir}'."

  @project_name = options[:project_name] || fqdn || slug
  @target_branch = options[:target_branch] || 'gh-pages'

  @gh_fqdn = fqdn
  @gh_url = options[:url] || options[:github_url] || 'github.com'
  @keep_history = !!keep_history
  @allow_empty_commit = !!allow_empty_commit
  @committer_from_gh = !!committer_from_gh
  @verbose = !!verbose

  @gh_email = options[:email] || 'deploy@travis-ci.org'
  @gh_name = "#{options[:name] || 'Deployment Bot'} (from Travis CI)"

  @deployment_file = !!options[:deployment_file]

  @gh_ref = "#{@gh_url}/#{slug}.git"
  @git_push_opts = @keep_history ? '' : ' --force'
  @git_commit_opts = (@allow_empty_commit and @keep_history) ? ' --allow-empty' : ''

  print_step "The repo is configured to use committer user and email." if @committer_from_gh
end

Public Instance Methods

allow_empty_commit() click to toggle source
# File lib/dpl/provider/pages.rb, line 83
def allow_empty_commit
  options.fetch(:allow_empty_commit, false)
end
api() click to toggle source
# File lib/dpl/provider/pages.rb, line 92
def api  # Borrowed from Releases provider
  error 'gh-token must be provided for Pages provider to work.' unless gh_token

  return @api if @api

  api_opts = { :access_token => gh_token }
  api_opts[:api_endpoint] = @gh_url == 'github.com' ? "https://api.github.com/" : "https://#{@gh_url}/api/v3/"

  @api = Octokit::Client.new(api_opts)
end
check_auth() click to toggle source
# File lib/dpl/provider/pages.rb, line 111
def check_auth
  setup_auth

  unless api.scopes.include? 'public_repo' or api.scopes.include? 'repo'
    error "Dpl does not have permission to access #{@gh_url} using it. Make sure your token contains the repo or public_repo scope."
  end

  log "Logged in as @#{user.login} (#{user.name})"
rescue Octokit::Unauthorized => exc
  error "gh-token is invalid. Details: #{exc}"
end
committer_from_gh() click to toggle source
# File lib/dpl/provider/pages.rb, line 79
def committer_from_gh
  options.fetch(:committer_from_gh, false)
end
fqdn() click to toggle source
# File lib/dpl/provider/pages.rb, line 63
def fqdn
  options.fetch(:fqdn) { nil }
end
gh_remote_url() click to toggle source
# File lib/dpl/provider/pages.rb, line 59
def gh_remote_url
  @gh_remote_url ||= "https://#{gh_token}@#{@gh_ref}"
end
gh_token() click to toggle source
# File lib/dpl/provider/pages.rb, line 55
def gh_token
  @gh_token ||= option(:github_token, :token)
end
github_commit() click to toggle source
# File lib/dpl/provider/pages.rb, line 171
def github_commit
  committer_name, _ = identify_preferred_committer
  print_step "Preparing to deploy #{@target_branch} branch to gh-pages (workdir: #{Dir.pwd})"
  context.shell "touch \"deployed at `date` by #{committer_name}\"" if @deployment_file
  context.shell "echo '#{@gh_fqdn}' > CNAME" if @gh_fqdn
  context.shell 'git add -A .'
  context.shell "git commit#{@git_commit_opts} -qm 'Deploy #{@project_name} to #{@gh_ref}:#{@target_branch}'"
  context.shell 'git show --stat-count=10 HEAD'
end
github_configure() click to toggle source
# File lib/dpl/provider/pages.rb, line 164
def github_configure
  committer_name, committer_email = identify_preferred_committer
  print_step "Configuring git committer to be #{committer_name} <#{committer_email}> (workdir: #{Dir.pwd})"
  context.shell "git config user.email '#{committer_email}'"
  context.shell "git config user.name '#{committer_name}'"
end
github_deploy() click to toggle source
# File lib/dpl/provider/pages.rb, line 181
def github_deploy
  print_step "Doing the git push (workdir: #{Dir.pwd})..."
  unless context.shell "git push#{@git_push_opts} --quiet '#{gh_remote_url}' '#{@target_branch}':'#{@target_branch}' > /dev/null 2>&1"
    error "Couldn't push the build to #{@gh_ref}:#{@target_branch}"
  end
end
github_init(target_dir) click to toggle source
# File lib/dpl/provider/pages.rb, line 147
def github_init(target_dir)
  FileUtils.cd(target_dir, :verbose => true) do
    print_step "Creating a brand new local repo from scratch in dir #{Dir.pwd}..."
    context.shell "git init" or raise 'Could not create new git repo'
    print_step 'Repo created successfully'
    context.shell "git checkout --orphan '#{@target_branch}'" or raise 'Could not create an orphan git branch'
    print_step "An orphan branch #{@target_branch} created successfully"
  end
end
github_pull_or_init(target_dir) click to toggle source
# File lib/dpl/provider/pages.rb, line 131
def github_pull_or_init(target_dir)
  unless @keep_history
    github_init(target_dir)
    return
  end

  print_step "Trying to clone a single branch #{@target_branch} from existing repo..."
  unless context.shell "git clone --quiet --branch='#{@target_branch}' --depth=1 '#{gh_remote_url}' '#{target_dir}' > /dev/null 2>&1"
    # if such branch doesn't exist at remote, init it from scratch
    print_step "Cloning #{@target_branch} branch failed"
    FileUtils.rm_rf(target_dir)
    Dir.mkdir(target_dir)  # Restore dir destroyed by failed `git clone`
    github_init(target_dir)
  end
end
identify_preferred_committer() click to toggle source
# File lib/dpl/provider/pages.rb, line 157
def identify_preferred_committer
  if @committer_from_gh and gh_token
    return (user.name or @gh_name), (user.email or @gh_email)
  end
  return @gh_name, @gh_email
end
keep_history() click to toggle source
# File lib/dpl/provider/pages.rb, line 75
def keep_history
  options.fetch(:keep_history, false)
end
needs_key?() click to toggle source
# File lib/dpl/provider/pages.rb, line 123
def needs_key?
  false
end
print_step(msg) click to toggle source
push_app() click to toggle source
# File lib/dpl/provider/pages.rb, line 188
def push_app
  print_step "Starting deployment of #{@target_branch} branch to GitHub Pages..."
  print_step "The deployment is configured to preserve the target branch if it exists on remote" if @keep_history
  Dir.mktmpdir do |tmpdir|
      workdir = "#{tmpdir}/work"
      Dir.mkdir(workdir)
      print_step "Created a temporary work directory #{workdir}"

      github_pull_or_init(workdir)

      FileUtils.cd(workdir, :verbose => true) do
        print_step "Copying #{@build_dir} contents to #{workdir} (workdir: #{Dir.pwd})..."
        context.shell "rsync -rl --exclude .git --delete '#{@build_dir}/' '#{workdir}'" or error "Could not copy #{@build_dir}."

        github_configure
        github_commit
        github_deploy
        context.shell "git status" if @verbose
      end
  end
  print_step "App has been pushed"
end
setup_auth() click to toggle source
# File lib/dpl/provider/pages.rb, line 107
def setup_auth
  user.login
end
slug() click to toggle source
# File lib/dpl/provider/pages.rb, line 67
def slug
  options.fetch(:repo) { context.env['TRAVIS_REPO_SLUG'] }
end
src_dir() click to toggle source
# File lib/dpl/provider/pages.rb, line 71
def src_dir
  context.env['TRAVIS_BUILD_DIR'] or Dir.pwd
end
user() click to toggle source
# File lib/dpl/provider/pages.rb, line 103
def user
  @user ||= api.user
end
verbose() click to toggle source
# File lib/dpl/provider/pages.rb, line 87
def verbose
  # Achtung! Never verbosify git, since it may expose user's token.
  options.fetch(:verbose, false)
end