Greg Dziemidowicz's Blog

software development related topics

Conditional Commit When Deploying to Heroku With Semaphore CI

Let’s say that before deploying to Heroku we want to check for any assets changes, and in case we find some we want to do some work.

If there are changes, we want to precompile assets and push any changes back to repo.

Here is example deploy script that does it:

ci_semaphore_deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

git fetch heroku

[ $(git diff master heroku/master app/assets | wc -l) -gt 0 ] || ASSET_CHANGE=1

if [ -z "$ASSET_CHANGE" ];
then
    git reset --hard
    git config --global user.email "hi@example.com"
    git config --global user.name "semaphoreapp "

    RAILS_GROUPS=assets RAILS_ENV=production bundle exec rake assets:precompile
    git add --all public/assets
    git commit -m "Update asset manifest before deploying [ci skip]"
    git push origin master
else
    echo "No asset changes detected..."
fi

git push --force heroku $BRANCH_NAME:master

Interesting bits:

  • Line 3 and 5: To see if there are any changes we need to fetch branches from heroku.
  • Line 9: Semaphore CI is rewritting config/database.yml which was problematic combined with RAILS_ENV=production
  • Line 10,11: We need to do this to make git push work. Also, you will need to add extra ssh key to be able to push.

Comments