This is the RubyHack 2018 presentation on automating your common tasks using git
The main idea is to use the tools you already have to improve your day to day work
4. 5 ways git can help deploy your rails
application
1 – check every commit follow company standards
2 – identify where each commit come from
3 – make sure at least the changed files tests run before pushing code
4 – start CI build after pushing the code
5 – 2 different approaches to git push your deploy after the ci build succeded
5. There is a lot of boring manual work to
do before each deploy!
Check code against
standards
Pretty format commit
messages
Run tests for the files
you changed to speed
up development
Start CI builds Start deployments Compile assets
Run migrations when
needed
Install npm modules
Reload application
servers
7. Git can help!
Commit Check
standards
Format
message
Push
code
Run tests
(locally)
Start CI
process
Pull
code
Bundle
install
Yarn
install
Start
deploy
Npm
install
Bundle
install
Db
migrate
Restart
service(s)
8. Putting it to
work!
Codding
standards
Git pre-commit hook
Formatting
commit
message
Git prepare-commit-msg hook
Running
tests
Git pre-push hook
Firing CI
build
Git post-receive hook
Automatic
deployment
Git post-receive hook
Or Git post-merge hook
9. I had one (more than one) evil coworker
that didn’t follow any company standards
This Photo by Unknown Author is licensed under CC BY
10. How to fight the
Evil Coworker
who does not
follow good
practices?
This Photo by Unknown Author is licensed under CC BY-SA
11. rubocode + .git/hooks/pre-commit
#!/bin/sh
echo "Checking commit"
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# If you want to allow non-ASCII filenames set this variable to true.
rubocop -R `git diff --cached --name-only $against`
if [ "$?" != "0" ]; then
cat <<EOF
pretty Failed!! message!!
EOF
exit 1
fi
12. I found a commit and did not know
where it came from
This Photo by Unknown Author is licensed under CC BY
13. Formatting commit messages
#!/bin/bash
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_IN_COMMIT=$(grep -c "[$BRANCH_NAME]" $1)
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
fi
14. Life is too short
to test before
sharing my code!
15. Run your tests before pushing the code!
#!/usr/bin/env ruby
branch_name=`git symbolic-ref -q HEAD`
branch_name = branch_name.gsub('refs/heads/', '').strip
changed_files = `git diff --name-only #{ARGV[0]}/#{branch_name}`.split("n").map(&:strip).select{|n| n
=~ /.rb$/}
#run the file specific test
test_files = changed_files.map do |name|
if name =~ /^app/
name.gsub('app/', 'test/').gsub(/.rb$/, '_test.rb')
elsif name =~ /^test/
name
else
nil
end
end.reject(&:nil?).uniq
#abort if there is a test missing
if !test_files.reject{|n| File.exists?(n)}.empty?
puts %Q{
Aborting push because there are missing test files: #{test_files.reject{|n| File.exists?(n)}}
}
exit 1
end
#if there are config files changes, run all tests
if !changed_files.select{|n| n =~ /^config/}.empty?
test_files = ''
end
system('git stash')
result = system("rails test #{test_files.join(' ')}")
system('git stash apply')
unless result
puts %Q{
Aborting push due to failed tests
}
exit 1
end
17. And the evil trick!
npm i shared-git-hooks --save-dev or yarn add shared-git-hooks
Add your git hooks (with or without extension) to the hooks
directory in your project
19. Found out you
need another gem
or npm module
when you decide
to do some work
in the airplane?
This Photo by Unknown Author is licensed under CC BY-NC-SA
30. My goal with this approach is to do this
git remote add production user@server:myapp.git
Git push production master
This Just Deployed my code to production!
31. Git Bare Repository
mkdir myapp.git
cd myapp.git && git init --bare
vi hooks/post-receive
34. Do you feel you need to track all that
deploys?
This Photo by Unknown Author is licensed under CC BY-SA
35. git-deploy
https://github.com/git-deploy/git-deploy
git deploy start (you can git deploy abort if needed)
Merge all feature branches that need deployment
git deploy sync
Push that to all your servers (maybe using our Heroku strategy?)
git deploy finish
Now you can use the git-deploy script to list all deployments, or go back to
any one of them!
37. Rodrigo Urubatan
I help ruby developers to use the
best tools for each job so they can
solve hard problems, with less bugs
and have more free time.
rodrigo@urubatan.com.br
http://twitter.com/urubatan
http://bit.ly/weekly_rails_tips
http://github.com/urubatan
http://linkedin.com/in/urubatan
http://sobrecodigo.com
38. Whats next? A git ebook!
http://bit.ly/weekly_rails_tips
Editor's Notes
Ask audience, has anyone already caused a bug or a broken build just by forgetting one of these tasks?
Is any of these tasks related to the business of your application?And the worse, is that rails should let you focus on your business logic instead of those boring repetitive tasks!
Talk a little about past projects and alternative options
Start talking about git hooks and the difference between client and server hooks