0

As part of a Capistrano Ruby task that deploys to a server, I'd like it to output the Git message and commit of the code it just deployed.

With this:

git_message = `git log -1 HEAD --pretty=format:%s`
git_commit = `git rev-parse HEAD`
git_commit = "https://example.com/myorg/myrepo/commit/" + git_commit

execute "echo \"Deployed \\\"#{git_message}\\\": #{git_commit}\" | a_command_that_posts_to_slack"

It's outputting something like this:

Deployed "Merge branch 'feature/some-feature' into develop": https://example.com/myorg/myrepo/commit/0fdfa09fbfe012649fb0a998aa2e99cb5fd7c8b3;

Note that semicolon at the very end of the commit hash. I've confirmed using puts that git_commit doesn't end with a semicolon, and git_message doesn't have one, and doesn't have one after it either.

What's adding the semicolon and how can I remove it?

Hugo
  • 27,885
  • 8
  • 82
  • 98
  • When you say its outputting, do you mean to slack? I see you have `a_command_that_posts_to_slack` perhaps this command is adding the semicolon? Try redirecting to a file instead of piping to that command. – Derek Wright Apr 26 '17 at 13:05
  • I see the semicolon in Slack, but I also see it in the terminal. The same semicolon appears without piping: `execute "echo \"Deployed \\\"#{git_message}\\\": #{git_commit}\""` – Hugo Apr 26 '17 at 15:13

2 Answers2

1

It is because you have newlines in your command, which Capistrano is translating into semicolons, thinking you want to execute multiple commands (one per line).

Backticks in Ruby capture the entire stdout of the process, including trailing newlines. Use chomp to remove them.

git_message = `git log -1 HEAD --pretty=format:%s`.chomp
git_commit = `git rev-parse HEAD`.chomp
git_commit = "https://example.com/myorg/myrepo/commit/" + git_commit

execute "echo \"Deployed \\\"#{git_message}\\\": #{git_commit}\" | a_command_that_posts_to_slack"
Matt Brictson
  • 10,904
  • 1
  • 38
  • 43
0

Perhaps its executing : as the Bash shell builtin and so its attempting to terminate that command with a semi-colon? Try removing the :. I also worry about inserting double quotes as a commit message containing double quotes might cause you some headache.

Reference on the Bash builtin : What is the purpose of the : (colon) GNU Bash builtin?

Community
  • 1
  • 1
Derek Wright
  • 1,452
  • 9
  • 11