Skip to content

Commit

Permalink
Set up JenkinsFile and Rakefile
Browse files Browse the repository at this point in the history
  • Loading branch information
kwkwan committed Jul 25, 2018
1 parent 06ba830 commit fb8bdb8
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
node("${env.NODE}") {
currentBuild.result = "SUCCESS"

try {
stage('Clean up') {
deleteDir()
}

stage('Checkout') {
checkout scm
}

stage('D/L dependencies') {
sh 'gem install bundler'
sh 'bundle'
}

stage('Test') {
sh 'rake test:production'
}

stage('Build production') {
sh 'rake build:production'
}

stage('Deploy') {
def files = findFiles(glob: '_site/**/*')
files.each {
def file = it
def dst = file.path.replace('_site/', '')
println "file: ${file}"
println "name: ${file.name}"
println "path: ${file.path}"
println "dst: ${dst}"

withAWS(region:"${env.REGION}",credentials:"${env.CREDENTIAL}") {
s3Upload(
bucket: "${env.HOST}",
file: "${file.path}",
path: "${dst}",
)
}
}
}

stage('Invalidate Cloudfront') {
withAWS(region:"${env.REGION}",credentials:"${env.CREDENTIAL}") {
cfInvalidate(distribution: "${env.CF_DIST_ID}", paths: ['/*'])
}
}
} catch (err) {
currentBuild.result = 'FAILURE'
throw err
}
}
88 changes: 88 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'html-proofer'

$sourceDir = "./source"
$outputDir = "./output"
$testOpts = {
# Ignore errors "linking to internal hash # that does not exist"
:url_ignore => ["#"],
# Allow empty alt tags (e.g. alt="") as these represent presentational images
:empty_alt_ignore => true
}

hostname = ENV['SITE_HOSTNAME']
region = ENV['SITE_REGION']

task :default => ["serve:development"]

desc "cleans the output directory"
task :clean do
sh "jekyll clean"
end

namespace :deploy do

desc "upload to S3"
task :s3upload => [] do
bucket = `aws s3api list-buckets --query "Buckets[?contains(Name, '#{hostname}')] | [0].Name" | jq -r '.'`

p "Uploading to #{bucket} in #{region}"

htmls = File.join("**", "*.html")

# Remove extension from HTML files
Dir.glob(htmls, base: "_site") do |filename|
# Skip index.html
next if filename == "index.html"

filename_noext = filename.sub('.html', '')
p "Will move #{filename} to #{filename_noext}"
FileUtils.mv(filename, filename_noext)
end

p "Will upload non-HTML files"
sh %{
aws s3 sync _site/ s3://#{bucket} --region #{region} --delete \
--size-only --exclude "*" --include "*.*"
}

p "Will upload HTML files"
sh %{
aws s3 sync _site/ s3://#{bucket} --region #{region} --delete \
--content-type "text/html; charset=utf-8" --exclude "*.*"
}
end
end

namespace :build do

desc "build development site"
task :development => [:clean] do
sh "jekyll build --drafts"
end

desc "build production site"
task :production => [:clean] do
sh "JEKYLL_ENV=production jekyll build --config=_config.yml"
end
end

namespace :serve do

desc "serve development site"
task :development => [:clean] do
sh "jekyll serve --drafts"
end

desc "serve production site"
task :production => [:clean] do
sh "JEKYLL_ENV=production jekyll serve --config=_config.yml"
end
end

namespace :test do

desc "test production build"
task :production => ["build:production"] do
HTMLProofer.check_directory($outputDir, $testOpts).run
end
end

0 comments on commit fb8bdb8

Please sign in to comment.