Class: NanocGit::Extra::Deployers::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc-git/extra/deployers/git.rb

Overview

A class for deploying a site using git.

Instance Method Summary collapse

Constructor Details

#initializeGit

Creates a new deployer that uses git. The deployment configurations will be read from the configuration file of the site (which is assumed to exist).

The deployment configurations are stored like this in the site’s configuration file:

deploy:
  NAME:
    dst_remote: origin
    dst_branch: master
    src_branch: source

‘NAME` is a unique name for the deployment configuration. By default, the deployer will use the deployment configuration named `“default”`.

‘DST_REMOTE` is the remote repository to which you are deploying your site.

‘DST_BRANCH` is the branch to which you are deploying your compiled site.

‘SRC_BRANCH` is the branch that contains the source files for your site.



31
32
33
34
# File 'lib/nanoc-git/extra/deployers/git.rb', line 31

def initialize
  error 'No site configuration found' unless File.file?('config.yaml')
  @site = Nanoc3::Site.new('.')
end

Instance Method Details

#run(params = {}) ⇒ void

This method returns an undefined value.

Runs the task. Possible params:

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :config_name (String) — default: :default

    The name of the deployment configuration to use.



42
43
44
45
46
47
48
49
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
# File 'lib/nanoc-git/extra/deployers/git.rb', line 42

def run(params={})
  config_name = params.has_key?(:config_name) ? params[:config_name].to_sym : :default

  # Validate config
  error 'No deploy configuration found'                    if @site.config[:deploy].nil?
  error "No deploy configuration found for #{config_name}" if @site.config[:deploy][config_name].nil?
  
  src_branch = @site.config[:deploy][config_name][:src_branch]
  dst_branch = @site.config[:deploy][config_name][:dst_branch]
  dst_remote = @site.config[:deploy][config_name][:dst_remote]
  
  error 'No source branch found in deployment configuration' if src_branch.nil?
  error 'No destination branch found in deployment configuration' if dst_branch.nil?
  error 'No destination remote found in deployment configuration' if dst_remote.nil?

  git = ::Git::Base.open('.')
  
  # Compile the site from scratch
  Nanoc3::Tasks::Clean.new(@site).run
  
  # Check out the source branch
  puts "Checking out #{src_branch}."
  git.checkout(src_branch)
  
  # Compile the site from scratch
  puts "Compiling site."
  @site.load_data
  @site.compiler.run
  
  # Check out the destination branch
  puts "Checking out destination branch."
  git.checkout(dst_branch)
  
  # Copy output files recursively into the current directory
  puts "Copying files."
  FileUtils.cp_r(@site.config[:output_dir].chomp('/') + '/.', '.')
  
  # Automatically add and commit changes
  puts "Committing changes."
  git.add
  git.commit("updated #{Time.now.to_s}", :add_all => true)
  
  # Push changes to the destination repo/branch
  puts "Pushing to #{dst_remote} #{dst_branch}."
  git.push(dst_remote, dst_branch)
  
  # Switch back to the source branch
  puts "Checking out #{src_branch}."
  git.checkout(src_branch)
end