Prepend logs with their machine name in deploy setups

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the web-development category.

Last Updated: 2024-11-21

I had a deploy script ./bin/deploy that went something like this:

echo "> Ensuring remote git repo has the right code to send to the server"
git push origin

echo "> Pulling latest code onto server then building containers"
ssh $server "(cd $deploy_dir && git pull -s recursive -X theirs && make build_production)"

When I ran this script, I got a bunch of error output from git, something along the lines of "git doesn't know who you are. Please enter in your email and name".

The problem was that I couldn't tell if this was git on my machine or git on the server - both were plausible based on what the script did.

Therefore, there should have been a way to prepend any output from the server with "Server: " in order to differentiate it.

Here's how:

# Using "ls -a" as a dummy command to run on the server

$ ssh $server "ls -a" | sed "s/^/Server: /"
Server: ..
Server: .bash_history
Server: .bashrc
Server: .cache