Class: TravisGithubDeployer

Inherits:
Object
  • Object
show all
Defined in:
lib/travis_github_deployer/travis_github_deployer.rb

Instance Method Summary collapse

Constructor Details

#initializeTravisGithubDeployer

Returns a new instance of TravisGithubDeployer.



9
10
11
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 9

def initialize
  @git = GitCommandLine.new
end

Instance Method Details

#change_current_directory_to_cloned_repositoryObject



108
109
110
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 108

def change_current_directory_to_cloned_repository
  Dir.chdir(destination_repository_dir)
end

#clone_destination_repositoryObject



104
105
106
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 104

def clone_destination_repository
  git.clone(destination_repository, destination_repository_dir)
end

#command_line_arguments(arguments) ⇒ Object



98
99
100
101
102
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 98

def command_line_arguments(arguments)
  if (arguments[0] == "-v")
    self.verbose = true
  end
end

#commit_and_push_filesObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 155

def commit_and_push_files
  files_to_deploy.each { |source_location, destination_location|
    git.add(Pathname.new(destination_location))
  }
  git.commit("File deployed with Travis Github Deployer")
  if(files_to_purge.empty?) 
    git.push
  else
    git.force_push
  end
end

#copy_files_in_destination_repositoryObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 140

def copy_files_in_destination_repository
  
  files_to_deploy.each { |source_location, destination_location|
    source = Pathname.new(source_location)
    destination = Pathname.new(destination_repository_dir)
    destination += destination_location
    FileUtils.cp_r(source, destination)
  }
  
end

#deployObject

Deployment



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 46

def deploy
  if (environment_variable_value('TRAVIS_PULL_REQUEST') != "false")
    puts "In pull request and won't be deploying"
    return
  end
  
  if (ENV['GIT_NAME'].nil?)
    puts "In fork and won't be deploying"
    return
  end
  
  load_configuration
  clone_destination_repository
  copy_files_in_destination_repository
  change_current_directory_to_cloned_repository
  prepare_credentials_based_on_environment_variables
  purge_files_from_history if not files_to_purge.empty?
  commit_and_push_files
end

#destination_repositoryObject

Configuration values



15
16
17
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 15

def destination_repository
  @destination_repository
end

#destination_repository_dirObject



19
20
21
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 19

def destination_repository_dir
  @destination_repository_dir ||= "travis_github_deployer_repository"
end

#environment_variable_value(environment_variable_name) ⇒ Object

Raises:

  • (StandardError)


134
135
136
137
138
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 134

def environment_variable_value (environment_variable_name)
  value = ENV[environment_variable_name]
  raise StandardError.new("The #{environment_variable_name} environment variable wasn't set.") if value.nil?
  value
end

#files_to_deployObject



36
37
38
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 36

def files_to_deploy
  @files_to_deploy ||= {}
end

#files_to_purgeObject



40
41
42
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 40

def files_to_purge
  @files_to_purge ||= []
end

#get_destination_and_add_file_to_purge(source, target_or_hash) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 74

def get_destination_and_add_file_to_purge source, target_or_hash
    if target_or_hash.instance_of?(Hash)
      files_to_purge << source if target_or_hash["purge"]   
      destination_file = target_or_hash["destination"]
    else
      destination_file = target_or_hash
    end
end

#gitObject



23
24
25
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 23

def git
  @git
end

#load_configurationObject

Preparing for deployment



68
69
70
71
72
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 68

def load_configuration
  configuration = YAML.load_file(".travis_github_deployer.yml")
  @destination_repository = configuration["destination_repository"]
  prepare_files_to_deploy(configuration["files_to_deploy"])
end

#prepare_credentials_based_on_environment_variablesObject



112
113
114
115
116
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 112

def prepare_credentials_based_on_environment_variables
  set_username_based_on_environment_variable
  set_email_based_on_environment_variable
  set_repository_token_based_on_enviroment_variable
end

#prepare_files_to_deploy(files_hash) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 83

def prepare_files_to_deploy files_hash
  files_hash.each { |source, values|
    
    source_files = Dir.glob(source)      
    if source_files.empty?
      raise StandardError.new("File: '#{source}' found in the configuration didn't exist. Deploy failed.") 
    end
    
    destination_file = get_destination_and_add_file_to_purge(source, values)  
    source_files.each { |source_file|
      files_to_deploy[source_file] = destination_file
    }
  }
end

#purge_files_from_historyObject



151
152
153
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 151

def purge_files_from_history
   git.filter_branch(files_to_purge.join(" "))
end

#set_email_based_on_environment_variableObject



130
131
132
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 130

def set_email_based_on_environment_variable
  git.config_email(environment_variable_value("GIT_EMAIL"))
end

#set_repository_token_based_on_enviroment_variableObject



118
119
120
121
122
123
124
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 118

def set_repository_token_based_on_enviroment_variable
  git_token = environment_variable_value("GIT_TOKEN")
  git.config_credential_helper_store_file(".git/travis_deploy_credentials")
  File.open(".git/travis_deploy_credentials", "w") { |credential_file|
    credential_file.write("https://#{git_token}:@github.com")
  }
end

#set_username_based_on_environment_variableObject



126
127
128
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 126

def set_username_based_on_environment_variable
  git.config_username(environment_variable_value("GIT_NAME"))
end

#verboseObject



27
28
29
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 27

def verbose
  @verbose ||= false
end

#verbose=(value) ⇒ Object



31
32
33
34
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 31

def verbose=(value)
  git.verbose = value
  @verbose = value
end