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



89
90
91
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 89

def change_current_directory_to_cloned_repository
  Dir.chdir(destination_repository_dir)
end

#clone_destination_repositoryObject



85
86
87
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 85

def clone_destination_repository
  git.clone(destination_repository, destination_repository_dir)
end

#command_line_arguments(arguments) ⇒ Object



79
80
81
82
83
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 79

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

#commit_and_push_filesObject



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

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")
  git.push
end

#copy_files_in_destination_repositoryObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 121

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.copy(source, destination)
  }
  
end

#deployObject

Deployment



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 42

def deploy
  if (environment_variable_value('TRAVIS_PULL_REQUEST') != "false")
    puts "In pull request 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
  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)


115
116
117
118
119
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 115

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

#gitObject



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

def git
  @git
end

#load_configurationObject

Preparing for deployment



58
59
60
61
62
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 58

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



93
94
95
96
97
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 93

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 64

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

#set_email_based_on_environment_variableObject



111
112
113
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 111

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

#set_repository_token_based_on_enviroment_variableObject



99
100
101
102
103
104
105
# File 'lib/travis_github_deployer/travis_github_deployer.rb', line 99

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



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

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