ruby-on-railsgitnginxgit-post-receive

Rails Git post-receive hooks doesn't start Puma


I have two almost identical post-hooks script that behaves differently. This is the one that doesn't work, Rails 7 API-only, Debian 11:

#!/bin/bash

GIT_DIR=/home/deploy/api.mydomain.com
WORK_TREE=/home/deploy/apps/api.mydomain.com

. ~/.profile

while read oldrev newrev ref
do
    if [[ $ref =~ .*/main$ ]];
    then
        echo "🚀 Main ref received. Deploying main branch to production..."
        mkdir -p $WORK_TREE
        git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f main
        mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log

        # start deploy tasks
        cd $WORK_TREE

        bundle install
        rake db:migrate

        echo "🚀 Killing existing process.."
        kill $(lsof -t -i:3002)
        echo "🚀 Starting puma..."
        bundle exec puma -C config/puma.rb -e production > /dev/null 2>&1 &
        echo "🚀 Puma is running"

        #echo "Restarting sidekiq..."
        #sudo systemctl restart sidekiq.service
        #echo "Sidekiq is running"
        # end deploy tasks

        echo "✅ Git hooks deploy complete"
    else
        echo "Ref $ref successfully received.  Doing nothing: only the main branch may be deployed on this server."
    fi
done

It doesn't work because I end up always need to run bundle exec puma -C config/puma.rb -e production > /dev/null 2>&1 & manually from the WORK_TREE.

However, this is the one that works, Rails 6, Debian 11:

#!/bin/bash

GIT_DIR=/home/deploy/myproject-production
WORK_TREE=/home/deploy/apps/myproject-web

. ~/.profile

while read oldrev newrev ref
do
    if [[ $ref =~ .*/main$ ]];
    then
        echo "Main ref received.  Deploying main branch to production..."
        mkdir -p $WORK_TREE
        git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f main
        mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log

        # start deploy tasks
        cd $WORK_TREE

    echo "--> Selecting Node version in the .nvmrc"
    . $HOME/.nvm/nvm.sh
    nvm use
    yarn install

    bundle install
        rake db:migrate

        bin/rails webpacker:clobber
        bin/rails assets:precompile

    echo "Killing existing process.."
    kill $(lsof -t -i:3000)
    echo "Starting puma..."
        bundle exec puma -C config/puma.rb -e production > /dev/null 2>&1 &
    echo "Puma is running"

    echo "Generating sitemap..."
    rake sitemap:refresh
    echo "Sitemap done"

    echo "Restarting sidekiq..."
    sudo systemctl restart sidekiq.service
    echo "Sidekiq is running"
    # end deploy tasks

    echo "Git hooks deploy complete"
    else
        echo "Ref $ref successfully received.  Doing nothing: only the main branch may be deployed on this server."
    fi
done

It kills the running puma correctly, then restart it from the WORK_TREE.

It is worth mentioning that I run nginx to route the request to puma. This is what I get every time I push to the production branch:

enter image description here

I'm aware it might be an environment issue in which they run, but it should work the same. I notice one strange behavior though, whenever I ssh to the first box, it loads the ˜/.profile directly, judging by the colorful $user in the console, but in the second box, it's not.


Solution

  • I solved this problem myself. My suspicion is right, the environment variables in the box are messed up. The reason being I use the box to host more than one project, so some environment variables ended up in ~/.bashrc and some in ~/.profile

    I simply move all the environment variables that are needed for the app to run to the ~/.profile

    Worth mentioning that I also changed the file permission to be executable.

    -rwxr-xr-x 1 deploy deploy 1107 Mar 11 19:08 hooks/post-receive