travis_github_deployer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ Travis to Github Deployer
2
+ ======================
3
+
4
+ The Travis to Github Deployer script helps when you want to deploy files from a Travis CI build into a github repository. This is especially useful when you use Github Pages, so that you can push certain build artifacts to the github pages such as coverage, test results or generated documentation
5
+
6
+ How does it work
7
+ ======================
8
+
9
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'travis_github_deployer.rb'
4
+ TravisGithubDeployer.new.deploy
@@ -0,0 +1,31 @@
1
+
2
+
3
+ class GitCommandLine
4
+
5
+ def clone(repository, destination)
6
+ git("clone " + repository + " " + destination)
7
+ end
8
+
9
+ def config(key, value)
10
+ git("config #{key} '#{value}'")
11
+ end
12
+
13
+ def config_username(username)
14
+ config("user.name", username)
15
+ end
16
+
17
+ def config_email(email)
18
+ config("user.email", email)
19
+ end
20
+
21
+ def config_credential_helper_store_file(filename)
22
+ config("credential.helper", "store --file=#{filename}")
23
+ end
24
+
25
+ def git(command)
26
+ output = `git #{command} 2>&1`
27
+ raise StandardError, "Git command: '#{command}' failed. Message: : " + output unless $?.success?
28
+ output
29
+ end
30
+
31
+ end
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'YAML'
4
+
5
+ class TravisGithubDeployer
6
+
7
+ def initialize
8
+ @git = GitCommandLine.new
9
+ end
10
+
11
+ ## Configuration values
12
+
13
+ def destination_repository
14
+ @destination_repository
15
+ end
16
+
17
+ def destination_repository_dir
18
+ @destination_repository_dir ||= "travis_github_deployer_repository"
19
+ end
20
+
21
+ def files_to_deploy
22
+ @files_to_deploy
23
+ end
24
+
25
+ ## Deployment
26
+
27
+ def deploy
28
+ load_configuration
29
+ clone_destination_repository
30
+ change_current_directory_to_cloned_repository
31
+ prepare_credentials_based_on_environment_variables
32
+ copy_files_in_destination_repository
33
+ commit_and_push_files
34
+ end
35
+
36
+ ## Preparing for deployment
37
+
38
+ def load_configuration
39
+ configuration = YAML.load_file(".travis_github_deployer.yml")
40
+ @destination_repository = configuration["destination_repository"]
41
+ @files_to_deploy = configuration["files_to_deploy"]
42
+ end
43
+
44
+ def clone_destination_repository
45
+ @git.clone(destination_repository, destination_repository_dir)
46
+ end
47
+
48
+ def change_current_directory_to_cloned_repository
49
+ Dir.chdir(destination_repository_dir)
50
+ end
51
+
52
+ def prepare_credentials_based_on_environment_variables
53
+ set_username_based_on_environment_variable
54
+ set_email_based_on_environment_variable
55
+ set_repository_token_based_on_enviroment_variable
56
+ end
57
+
58
+ def set_repository_token_based_on_enviroment_variable
59
+ git_token = environment_variable_value("GIT_TOKEN")
60
+ @git.config_credential_helper_store_file(".git/travis_deploy_credentials")
61
+ File.open(".git/travis_deploy_credentials", "w") { |credential_file|
62
+ credential_file.write("https://#{git_token}:@github.com")
63
+ }
64
+ end
65
+
66
+ def set_username_based_on_environment_variable
67
+ @git.config_username(environment_variable_value("GIT_NAME"))
68
+ end
69
+
70
+ def set_email_based_on_environment_variable
71
+ @git.config_email(environment_variable_value("GIT_EMAIL"))
72
+ end
73
+
74
+ def environment_variable_value (environment_variable_name)
75
+ value = ENV[environment_variable_name]
76
+ raise StandardError.new("The #{environment_variable_name} environment variable wasn't set.") if value.nil?
77
+ value
78
+ end
79
+
80
+ def copy_files_to_deployment_repository
81
+
82
+ files_to_deploy.each { |source_location, destination_location|
83
+ source = Pathname.new(source_location)
84
+ destination = Pathname.new("github_pages")
85
+ destination += destination_location.empty? ? source_location : destination_location
86
+ FileUtils.copy(source, destination)
87
+ }
88
+
89
+ end
90
+
91
+ def commit_and_push_files
92
+ end
93
+
94
+ end
95
+
96
+
97
+ if __FILE__ == $0 then
98
+ GithubPagesDeployer.new.deploy
99
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module TravisGithubDeployerVersion
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,3 @@
1
+
2
+ require 'travis_github_deployer/git_command_line.rb'
3
+ require 'travis_github_deployer/travis_github_deployer.rb'
@@ -0,0 +1,47 @@
1
+
2
+ require 'travis_github_deployer.rb'
3
+
4
+ describe "simple ruby interface around git command line" do
5
+
6
+ subject { GitCommandLine.new}
7
+
8
+ it "can do a git clone" do
9
+ subject.should_receive(:git).with("clone repository destination")
10
+ subject.clone("repository", "destination")
11
+ end
12
+
13
+ it "can do a config" do
14
+ subject.should_receive(:git).with("config key 'value'")
15
+ subject.config("key", "value")
16
+ end
17
+
18
+ it "can configure the username" do
19
+ subject.should_receive(:config).with("user.name", "basvodde")
20
+ subject.config_username("basvodde")
21
+ end
22
+
23
+ it "can configure the email" do
24
+ subject.should_receive(:config).with("user.email", "basv@sokewl.com")
25
+ subject.config_email("basv@sokewl.com")
26
+ end
27
+
28
+ it "can configure the credential helper" do
29
+ subject.should_receive(:config).with("credential.helper", "store --file=filename")
30
+ subject.config_credential_helper_store_file("filename")
31
+ end
32
+
33
+
34
+ context "do_system" do
35
+ it "Should be able to do a successful command" do
36
+ subject.git('version').should start_with("git version")
37
+ end
38
+
39
+ it "Should be able to raise an StandardError on failed commands" do
40
+ expect {
41
+ subject.git('error')
42
+ }.to raise_error(StandardError, "Git command: 'error' failed. Message: : git: 'error' is not a git command. See 'git --help'.\n\nDid you mean this?\n rerere\n")
43
+ end
44
+ end
45
+
46
+
47
+ end
@@ -0,0 +1,107 @@
1
+
2
+ require 'travis_github_deployer.rb'
3
+
4
+ describe "travis github deployer" do
5
+
6
+ subject { TravisGithubDeployer.new}
7
+
8
+ before(:each) do
9
+ @git = mock
10
+ GitCommandLine.should_receive(:new).and_return(@git)
11
+ end
12
+
13
+ it "can deploy to an destination repository" do
14
+ subject.should_receive(:load_configuration)
15
+ subject.should_receive(:clone_destination_repository)
16
+ subject.should_receive(:change_current_directory_to_cloned_repository)
17
+ subject.should_receive(:prepare_credentials_based_on_environment_variables)
18
+ subject.should_receive(:copy_files_in_destination_repository)
19
+ subject.should_receive(:commit_and_push_files)
20
+ subject.deploy
21
+ end
22
+
23
+ context "Prepare repository for being able to commit" do
24
+
25
+ it "can clone the destination repository" do
26
+ subject.should_receive(:destination_repository).and_return("https://github.com/cpputest/cpputest")
27
+ subject.should_receive(:destination_repository_dir).and_return("destdir")
28
+ @git.should_receive(:clone).with("https://github.com/cpputest/cpputest", "destdir")
29
+
30
+ subject.clone_destination_repository
31
+ end
32
+
33
+ it "can change the directory to the cloned directory" do
34
+ subject.should_receive(:destination_repository_dir).and_return("destinationdir")
35
+ Dir.should_receive(:chdir).with("destinationdir")
36
+ subject.change_current_directory_to_cloned_repository
37
+ end
38
+
39
+ it "Should be able to set the credentials for pushing stuff up" do
40
+ subject.should_receive(:set_username_based_on_environment_variable)
41
+ subject.should_receive(:set_email_based_on_environment_variable)
42
+ subject.should_receive(:set_repository_token_based_on_enviroment_variable)
43
+ subject.prepare_credentials_based_on_environment_variables
44
+ end
45
+
46
+ it "Should be able to set the username based on an environment variable" do
47
+ ENV['GIT_NAME'] = "basvodde"
48
+ @git.should_receive(:config_username).with("basvodde")
49
+ subject.set_username_based_on_environment_variable
50
+ end
51
+
52
+ it "Should give an error message when the GIT_NAME isn't set" do
53
+ ENV['GIT_NAME'] = nil
54
+ expect {subject.set_username_based_on_environment_variable}.to raise_error(StandardError, "The GIT_NAME environment variable wasn't set.")
55
+ end
56
+
57
+ it "Should be able to set the password based on an environment variable" do
58
+ ENV['GIT_EMAIL'] = "basv@bestcompanythatexists.com"
59
+ @git.should_receive(:config_email).with("basv@bestcompanythatexists.com")
60
+ subject.set_email_based_on_environment_variable
61
+ end
62
+
63
+ it "Should be able to write the github token based on an environment variable" do
64
+ credential_file = mock
65
+ ENV['GIT_TOKEN'] = "Token"
66
+
67
+ @git.should_receive(:config_credential_helper_store_file).with(".git/travis_deploy_credentials")
68
+ File.should_receive(:open).with(".git/travis_deploy_credentials", "w").and_yield(credential_file)
69
+ credential_file.should_receive(:write).with("https://Token:@github.com")
70
+
71
+ subject.set_repository_token_based_on_enviroment_variable
72
+ end
73
+ end
74
+
75
+ context "Prepare the changes that need to be made commit" do
76
+
77
+ it "should be able to copy a file from the root of the source repository to the root of the destination reportistory" do
78
+ subject.should_receive(:files_to_deploy).and_return( { "sourcefile" => ""})
79
+ FileUtils.should_receive(:copy).with(Pathname.new("sourcefile"), Pathname.new("github_pages/sourcefile"))
80
+ subject.copy_files_to_deployment_repository
81
+ end
82
+
83
+ it "Should be able to copy multiple files" do
84
+ subject.should_receive(:files_to_deploy).and_return({ "dir/onefile" => "destonefile", "twofile" => "dir/desttwofile"})
85
+ FileUtils.should_receive(:copy).with(Pathname.new("dir/onefile"), Pathname.new("github_pages/destonefile"))
86
+ FileUtils.should_receive(:copy).with(Pathname.new("twofile"), Pathname.new("github_pages/dir/desttwofile"))
87
+ subject.copy_files_to_deployment_repository
88
+ end
89
+ end
90
+
91
+ it "can read configuration parameters out of the .travis_github_deployer.yml" do
92
+ configuration = {
93
+ "destination_repository" => "https://github.com/cpputest/cpputest.github.io.git",
94
+ "files_to_deploy" => {
95
+ "source_dir/source_file" => "destination_dir/destination_file"
96
+ }
97
+ }
98
+
99
+ YAML.should_receive(:load_file).with(".travis_github_deployer.yml").and_return(configuration)
100
+ subject.load_configuration
101
+
102
+ subject.destination_repository.should== "https://github.com/cpputest/cpputest.github.io.git"
103
+ subject.files_to_deploy.should== { "source_dir/source_file" => "destination_dir/destination_file" }
104
+
105
+ end
106
+
107
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/travis_github_deployer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'travis_github_deployer'
6
+ gem.version = TravisGithubDeployerVersion::VERSION
7
+ gem.date = Date.today.to_s
8
+ gem.executables = [ "travis_github_deployer" ]
9
+
10
+ gem.summary = "Script to deploy to github from Travis CI"
11
+ gem.description = "A Script and a library that help in deploying files from Travis CI builds to a Github Repository"
12
+
13
+ gem.authors = ['Bas Vodde']
14
+ gem.email = 'basv@odd-e.com'
15
+ gem.homepage = 'https://github.com/basvodde/travis_github_deployer.rb'
16
+
17
+ gem.add_dependency('rake')
18
+ gem.add_development_dependency('rspec', [">= 2.0.0"])
19
+
20
+ gem.files = `git ls-files -- {.,test,spec,lib}/*`.split("\n")
21
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: travis_github_deployer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Bas Vodde
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-07-15 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.0.0
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: A Script and a library that help in deploying files from Travis CI builds to a Github Repository
38
+ email: basv@odd-e.com
39
+ executables:
40
+ - travis_github_deployer
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - bin/travis_github_deployer
49
+ - lib/travis_github_deployer.rb
50
+ - lib/travis_github_deployer/git_command_line.rb
51
+ - lib/travis_github_deployer/travis_github_deployer.rb
52
+ - lib/travis_github_deployer/version.rb
53
+ - spec/git_command_line_spec.rb
54
+ - spec/travis_github_deployer_spec.rb
55
+ - travis_github_deployer.gemfile
56
+ homepage: https://github.com/basvodde/travis_github_deployer.rb
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.24
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Script to deploy to github from Travis CI
83
+ test_files: []
84
+
85
+ has_rdoc: