rake-jekyll 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e10e3e33be1203cbf0bf52796a3e1656d21c9d46
4
- data.tar.gz: 82fdf6429da1b35aa3cdfa173a090c6c9acc9346
3
+ metadata.gz: ac699ba1d1df2ed15767446ee842fbe45906c142
4
+ data.tar.gz: be141acdedfa657144ec01b79415a7342c2a8430
5
5
  SHA512:
6
- metadata.gz: b22bf29cf1a9ab34801a273f91db759e34093cf04781ea0381f5dd49b8069601d2a39da462dfb1c23e53cdd957859996e29c2d8404f0a02d04a988f58d0948fb
7
- data.tar.gz: a57ba8f2b4b0d115096700d843ebf3964ee68a85e3b17f76d6fb7757284ae0afbefb90d1263302f5fb6571a19786f07e95cf5b05d5c41098519199979c948b0c
6
+ metadata.gz: b40e103011acf97f5b140c0b99d5934ec97f1eef9ff7450936bdc3998eaa82d58efd43603aec1736811aa0b15ed5edd5b351a9c5039f4975ced980b717fbfb40
7
+ data.tar.gz: 7b21c15052fc052c4f3e23da7d287ba7641e337c94c7e1d91efdd74ba6bf2d601757c425b61451ca940fba6eec7000c88af62da3a9c2f84ff474b78eff71fda0
@@ -72,13 +72,17 @@ Rake::Jekyll::GitDeployTask.new(:deploy) do |t|
72
72
  # user.name is not set in git config.
73
73
  t.committer = 'Jekyll'
74
74
 
75
- # Deploy the built site into remote branch named 'gh-pages'. It will be
76
- # automatically created if not exist yet.
77
- t.deploy_branch = 'gh-pages'
78
-
75
+ # Deploy the built site into remote branch named 'gh-pages', or 'master' if
76
+ # the remote repository URL matches `#{gh_user}.github.io.git`.
77
+ # It will be automatically created if not exist yet.
78
+ t.deploy_branch = -> {
79
+ gh_user = ENV['TRAVIS_REPO_SLUG'].to_s.split('/').first
80
+ remote_url.match(/[:\/]#{gh_user}\.github\.io\.git$/) ? 'master' : 'gh-pages'
81
+ }
79
82
  # Run this command to build the site.
80
- t.jekyll_build = ->(dest_dir) {
81
- Rake.sh "bundle exec jekyll build --destination #{dest_dir}"
83
+ t.build_script = ->(dest_dir) {
84
+ puts "\nRunning Jekyll..."
85
+ sh "bundle exec jekyll build --destination #{dest_dir}"
82
86
  }
83
87
  # Use the default committer (configured in git) when available.
84
88
  t.override_committer = false
@@ -86,16 +90,22 @@ Rake::Jekyll::GitDeployTask.new(:deploy) do |t|
86
90
  # Use URL of the 'origin' remote to fetch/push the built site into. If env.
87
91
  # variable GH_TOKEN is set, then it adds it as a userinfo to the URL.
88
92
  t.remote_url = -> {
89
- `git config remote.origin.url`.strip.gsub(/^git:/, 'https:').tap do |url|
90
- url.gsub!(%r{^https://}, "https://#{ENV['GH_TOKEN']}@") if ENV.key? 'GH_TOKEN'
91
- end
93
+ url = `git config remote.origin.url`.strip.gsub(/^git:/, 'https:')
94
+ next url.gsub(%r{^https://([^/]+)/(.*)$}, 'git@\1:\2') if ssh_key_file?
95
+ next url.gsub(%r{^https://}, "https://#{ENV['GH_TOKEN']}@") if ENV.key? 'GH_TOKEN'
96
+ next url
92
97
  }
93
- # Skip commit and push when building a pull request or env. variable
94
- # SKIP_COMMIT represents truthy.
95
- t.skip_commit = -> {
98
+ # Skip commit and push when building a pull request, env. variable
99
+ # SKIP_DEPLOY represents truthy, or env. variable SOURCE_BRANCH is set, but
100
+ # does not match TRAVIS_BRANCH.
101
+ t.skip_deploy = -> {
96
102
  ENV['TRAVIS_PULL_REQUEST'].to_i > 0 ||
97
- %w[yes y true 1].include?(ENV['SKIP_COMMIT'].to_s.downcase)
103
+ %w[yes y true 1].include?(ENV['SKIP_DEPLOY'].to_s.downcase) ||
104
+ (ENV['SOURCE_BRANCH'] && ENV['SOURCE_BRANCH'] != ENV['TRAVIS_BRANCH'])
98
105
  }
106
+ # Path of the private SSH key to be used for communication with the
107
+ # repository defined by remote_url.
108
+ t.ssh_key_file = '.deploy_key'
99
109
  end
100
110
  ----
101
111
 
@@ -147,6 +157,12 @@ script: bundle exec rake deploy
147
157
  .. open your https://travis-ci.org/profile/[profile page] on Travis,
148
158
  .. find the repository and turn on the switch,
149
159
  .. then click on repository settings (next to the switch) and enable “Build only if .travis.yml is present.”
160
+
161
+ Now you can choose if you want to use GitHub token (an easier way), or a deploy key (more secure way).
162
+
163
+ ===== A. Use GitHub token
164
+
165
+ [start=6]
150
166
  . Generate a new personal access token on GitHub:
151
167
  .. open https://github.com/settings/tokens/new[this page] to generate a new personal access token,
152
168
  .. select the scope _public_repo_, fill some description and confirm.
@@ -163,6 +179,46 @@ env:
163
179
  global:
164
180
  secure: YOUR-ENCRYPTED-TOKEN
165
181
  ----
182
+ +
183
+ . Commit changes, push to GitHub and check that Travis has started the job and finished it successfully.
184
+
185
+ ===== B. Use SSH deploy key
186
+
187
+ [start=6]
188
+ . Generate new RSA key pair and write it to file `.deploy_key` (and `.deploy_key.pub`) in the root of your Jekyll repository:
189
+ +
190
+ $ ssh-keygen -N '' -f .deploy_key
191
+ +
192
+ . Encrypt the private key and add it to your `.travis.yml`:
193
+ .. encrypt the key:
194
+ +
195
+ $ travis encrypt-file .deploy_key --add
196
+ +
197
+ .. check that it created file `.deploy_key.enc` and added something like the following to `.travis.yml`:
198
+ +
199
+ [source, yaml]
200
+ ----
201
+ before_install:
202
+ - openssl aes-256-cbc -K $encrypted_e18dd77852c2_key -iv $encrypted_e18dd77852c2_iv -in .deploy_key.enc -out .deploy_key -d
203
+ ----
204
+ +
205
+ .. and add command `chmod 600 .deploy_key` to `.travis.yml` after the `openssl` command, so you will end with something like:
206
+ +
207
+ [source, yaml]
208
+ ----
209
+ before_install:
210
+ - openssl aes-256-cbc -K $encrypted_e18dd77852c2_key -iv $encrypted_e18dd77852c2_iv -in .deploy_key.enc -out .deploy_key -d
211
+ - chmod 600 .deploy_key
212
+ ----
213
+ +
214
+ . Add `.deploy_key` to `.gitignore` (this is unencrypted private key, keep it in secret!):
215
+ +
216
+ $ echo '.deploy_key' >> .gitignore
217
+ +
218
+ . Register the generated key as a deploy key in your GitHub repository:
219
+ .. open `https://github.com/<username>/<reponame>/settings/keys` and click on _Add deploy key_,
220
+ .. paste content of the `.deploy_key.pub` file to the textbox,
221
+ .. select “Allow write access” and confirm.
166
222
  . Commit changes, push to GitHub and check that Travis has started the job and finished it successfully.
167
223
 
168
224
 
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ if [ -z "$SSH_PRIVATE_KEY" ]; then
4
+ ssh "$@"
5
+ else
6
+ ssh -i "$SSH_PRIVATE_KEY" "$@"
7
+ fi
@@ -1,2 +1,8 @@
1
+ module Rake
2
+ module Jekyll
3
+ BIN_DIR = File.expand_path('../bin', __dir__)
4
+ end
5
+ end
6
+
1
7
  require 'rake-jekyll/version'
2
8
  require 'rake-jekyll/git_deploy_task'
@@ -95,14 +95,16 @@ module Rake::Jekyll
95
95
  # @param attr_name [#to_s] name of the attribute to define.
96
96
  # @param default_value [Object] the default value (optional).
97
97
  # @yield When the block is given, then it's used as a default value.
98
- # It takes precedence over +default_value+.
98
+ # It takes precedence over the +default_value+. It's evaluated in an
99
+ # instance context.
99
100
  def self.callable_attr(attr_name, default_value = nil, &default_block)
100
101
  var_name = "@#{attr_name}".sub('?', '').to_sym
101
102
 
102
103
  define_method attr_name do
103
104
  value = instance_variable_get(var_name)
105
+
104
106
  if value.nil? && default_block
105
- do_in_working_dir &default_block
107
+ do_in_working_dir { instance_eval &default_block }
106
108
  elsif value.nil?
107
109
  default_value
108
110
  elsif value.is_a?(Proc) && value.arity.zero?
@@ -171,23 +173,33 @@ module Rake::Jekyll
171
173
  # If the remote branch doesn't exist yet, then it's automatically created
172
174
  # as an orphan branch.
173
175
  #
174
- # @return [String, Proc] name of the remote branch (default: +gh-pages+).
176
+ # @return [String, Proc] name of the remote branch. Defaults to +gh-page+,
177
+ # or +master+ if the _remote_url_ matches `#{gh_user}.github.io.git`.
175
178
  #
176
- callable_attr :deploy_branch, 'gh-pages'
179
+ callable_attr :deploy_branch do
180
+ gh_user = ENV['TRAVIS_REPO_SLUG'].to_s.split('/').first
181
+ remote_url.match(/[:\/]#{gh_user}\.github\.io\.git$/) ? 'master' : 'gh-pages'
182
+ end
177
183
 
178
184
  ##
179
- # @!attribute jekyll_build
185
+ # @!attribute build_script
180
186
  # Defines a function that executes Jekyll to build the site.
181
187
  # Defaults to:
188
+ # puts "\nRunning Jekyll..."
182
189
  # Rake.sh "bundle exec jekyll build --destination #{dest_dir}"
183
190
  #
184
191
  # @return [Proc] a Proc that accepts one argument; the destination
185
192
  # directory to generate the site into.
186
193
  #
187
- callable_attr :jekyll_build, ->(dest_dir) {
194
+ callable_attr :build_script, ->(dest_dir) {
195
+ puts "\nRunning Jekyll..."
188
196
  Rake.sh "bundle exec jekyll build --destination #{dest_dir}"
189
197
  }
190
198
 
199
+ # For backward compatibility, remove in 2.x.
200
+ alias_method :jekyll_build, :build_script
201
+ alias_method :jekyll_build=, :build_script=
202
+
191
203
  ##
192
204
  # @!attribute override_committer?
193
205
  # @return [Boolean, Proc] +true+ to always use {#committer}, +false+ to use
@@ -196,30 +208,56 @@ module Rake::Jekyll
196
208
 
197
209
  ##
198
210
  # @!attribute remote_url
199
- # @return [String, Proc] URL of the remote git repository to fetch and push
200
- # the built site into. The default is to use URL of the +origin+ remote,
201
- # replace +git:+ schema with +https:+ and add environment variable
202
- # +GH_TOKEN+ as an userinfo (if exists).
211
+ # Defines URL of the remote git repository to pull and push the built site
212
+ # into. The default is to use URL of the +origin+ remote and:
213
+ #
214
+ # a. if {#ssh_key_file} is readable, then convert URL to SSH address with
215
+ # user +git+;
216
+ # b. or if environment variable +GT_TOKEN+ is set, then replace +git:+
217
+ # schema with +https:+ and add +GH_TOKEN+ as an userinfo.
218
+ # c. else use remote URL as is.
219
+ #
220
+ # @return [String, Proc] URL of the target git repository.
221
+ #
203
222
  callable_attr :remote_url do
204
- `git config remote.origin.url`.strip.gsub(/^git:/, 'https:').tap do |url|
205
- url.gsub!(%r{^https://}, "https://#{ENV['GH_TOKEN']}@") if ENV.key? 'GH_TOKEN'
206
- end
223
+ url = `git config remote.origin.url`.strip.gsub(/^git:/, 'https:')
224
+ next url.gsub(%r{^https://([^/]+)/(.*)$}, 'git@\1:\2') if ssh_key_file?
225
+ next url.gsub(%r{^https://}, "https://#{ENV['GH_TOKEN']}@") if ENV.key? 'GH_TOKEN'
226
+ next url
207
227
  end
208
228
 
209
229
  ##
210
- # @!attribute [w] skip_commit
230
+ # @!attribute [w] skip_deploy
211
231
  # Whether to skip the commit and push phase.
212
232
  # Default is to return +true+ when env variable +TRAVIS_PULL_REQUEST+
213
- # is an integer value greater than 0 or +SKIP_COMMIT+ represents truthy
214
- # (i.e. contains yes, y, true, or 1).
233
+ # is an integer value greater than 0, +SKIP_DEPLOY+ represents truthy
234
+ # (i.e. contains yes, y, true, or 1), or +SOURCE_BRANCH+ is set and does
235
+ # not match +TRAVIS_BRANCH+.
215
236
  #
216
- # @return [Boolean, Proc]
237
+ # @return [Boolean, Proc] skip deploy?
217
238
  #
218
- callable_attr :skip_commit? do
239
+ callable_attr :skip_deploy? do
219
240
  ENV['TRAVIS_PULL_REQUEST'].to_i > 0 ||
220
- %w[yes y true 1].include?(ENV['SKIP_COMMIT'].to_s.downcase)
241
+ %w[yes y true 1].include?((ENV['SKIP_DEPLOY'] || ENV['SKIP_COMMIT']).to_s.downcase) ||
242
+ (ENV['SOURCE_BRANCH'] && ENV['SOURCE_BRANCH'] != ENV['TRAVIS_BRANCH'])
221
243
  end
222
244
 
245
+ # For backward compatibility, remove in 2.x.
246
+ alias_method :skip_commit?, :skip_deploy?
247
+ alias_method :skip_commit=, :skip_deploy=
248
+
249
+ ##
250
+ # @!attribute ssh_key_file
251
+ # Defines path of the private SSH key to be used for communication with
252
+ # {#remote_url}. This is optional; when the file doesn't exist, then it's
253
+ # ignored.
254
+ #
255
+ # @note NEVER STORE YOUR PRIVATE SSH KEY IN THE REPOSITORY UNENCRYPTED!
256
+ #
257
+ # @return [String, Proc] path of the private SSH key (default: +.deploy_key+).
258
+ #
259
+ callable_attr :ssh_key_file, '.deploy_key'
260
+
223
261
 
224
262
  ##
225
263
  # @param name [#to_sym] name of the task to define.
@@ -231,6 +269,11 @@ module Rake::Jekyll
231
269
 
232
270
  yield self if block_given?
233
271
 
272
+ if ssh_key_file?
273
+ ENV['SSH_PRIVATE_KEY'] = "#{@working_dir}/#{ssh_key_file}"
274
+ ENV['GIT_SSH'] = "#{BIN_DIR}/git-ssh-wrapper"
275
+ end
276
+
234
277
  define_task!
235
278
  end
236
279
 
@@ -256,16 +299,15 @@ module Rake::Jekyll
256
299
  end
257
300
  end
258
301
 
259
- puts "\nRunning Jekyll..."
260
- jekyll_build[temp_dir]
302
+ build_script.call(temp_dir)
261
303
 
262
304
  Dir.chdir temp_dir do
263
305
  unless any_changes?
264
- puts 'Nothing to commit.'; next
306
+ puts 'Nothing to commit and deploy.'; next
265
307
  end
266
308
 
267
- if skip_commit?
268
- puts 'Skipping commit.'; next
309
+ if skip_deploy?
310
+ puts 'Skipping deploy.'; next
269
311
  end
270
312
 
271
313
  if override_committer? || !config_set?('user.name')
@@ -284,5 +326,9 @@ module Rake::Jekyll
284
326
  yield
285
327
  end
286
328
  end
329
+
330
+ def ssh_key_file?
331
+ File.readable?(ssh_key_file)
332
+ end
287
333
  end
288
334
  end
@@ -1,5 +1,5 @@
1
1
  module Rake
2
2
  module Jekyll
3
- VERSION = '1.0.4'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake-jekyll
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Jirutka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-16 00:00:00.000000000 Z
11
+ date: 2015-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -61,6 +61,7 @@ files:
61
61
  - LICENSE
62
62
  - README.adoc
63
63
  - Rakefile
64
+ - bin/git-ssh-wrapper
64
65
  - lib/rake-jekyll.rb
65
66
  - lib/rake-jekyll/git_deploy_task.rb
66
67
  - lib/rake-jekyll/version.rb