phallstrom / slackistrano

Slack integration for Capistrano deployments.
MIT License
374 stars 74 forks source link

Send commit log between 2 deployments. #50

Open prigal opened 8 years ago

prigal commented 8 years ago

Hello,

Could you include the commit history log in the slack output between 2 revisions to help developers (and non developpers on channels) to understand what has been sent with each deploy.

You can find previous revision number in {current_path}/REVISION file in capistrano.

Thx !

phallstrom commented 8 years ago

Maybe. I have grand visions of re-working the messaging sent to Slack to make it easier for folks to do this sort of thing themselves. I don't want to get into a situation of having more settings than we already have if I can help it.

Do you really want the log output? That could be a ton of information... just curious.

prigal commented 8 years ago

Yes It could be really helpfull for us. We deploy everyday, so logs between deploy are small, and it will help teams on slack to understand what has been sent to production.

You're right, it should be an option for teams who deploy every month (log is useless in this case).

I can probably already get it by myself with a ruby function in capistrano and send the output to the message sent via slackistrano, I post the issue because I thought it could make sense in slakistrano config (commit_log => true/false).

Thanks !

phallstrom commented 8 years ago

@pierrerigal Assuming you've got the two revisions, what is the git command you run to generate the commit log?

lcguida commented 8 years ago

Very basic log would look like:

git log --oneline <new revision hash>..<old revision hash>
rkul commented 7 years ago

I have no idea is it still relevant, but I've solved this as following. I've added custom task to the deployment process as defined here http://capistranorb.com/documentation/advanced-features/custom-scm/ In lib/capistrano/my.rb:

module Capistrano
  class My < ::Capistrano::SCM::Plugin
    def define_tasks
      eval_rakefile File.expand_path("../tasks/my.rake", __FILE__)
    end

    def register_hooks
      after "deploy:published", "My:set_changelog"
    end
end

In lib/capistano/tasks/my.rake:

  desc "Get changelog between releases"
  task :set_changelog do
    on release_roles(:all) do
      set(:changelog, capture(:diff,
        '--unchanged-line-format=""',
        '--new-line-format="%L"',
        '--text',
        File.join(fetch(:release_path), "CHANGELOG"),
        File.join(fetch(:previous_release_path), "CHANGELOG"),
        "2>/dev/null; true" # diff exits with 1 if there is a difference found
      ).force_encoding(Encoding::UTF_8))
    end
  end

In custom_slack_messaging.rb:

    def payload_for_updated
      {
        attachments: [{
          color: 'good',
          title: 'My Backend deployment finished',
          fields: [{
            title: 'Environment',
            value: stage,
            short: true
          }, {
            title: 'Branch',
            value: branch,
            short: true
          },{
            title: 'Deployed revision',
            value: fetch(:current_revision),
            short: true
          }, {
            title: 'Previous revision',
            value: fetch(:previous_revision),
            short: true            
          }, {
            title: 'Deployer',
            value: deployer,
            short: true
          }, {
            title: 'Time',
            value: elapsed_time,
            short: true
          }, {
            title: 'Changelog',
            value: fetch(:changelog).gsub(/\(#(\d+)\)/, "(<%{repo_url}/pull/%{pr}|#%{pr}>)" % {repo_url: fetch(:repo_url), pr: '\1'})
          }],
          footer: "<#{fetch(:repo_url)}/compare/#{fetch(:previous_revision)}...#{fetch(:current_revision)}|See the diff on github.com>",
          footer_icon: "https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png",
          fallback: super[:text]
        }]
      }
    end

The regexp you can see in Changelog value will substitute Github pullrequest numbers withing links to related pullrequest on Github (if repo_url set correctly). Like (#1234) at the end of line.

martijnhartlief commented 7 years ago

Is this something that can be picked up?

Notifying in Slack is extremely useful when someone does not have access to the repo. Thus actually showing the git log --oneline <new revision hash>..<old revision hash> is required to know what has been deployed.

phallstrom commented 7 years ago

@martijnhartlief Absolutely! If you want to work on this and submit a PR that would be great. I would encourage you to do it as a custom messaging class people can leverage instead of building it in directly. I can bring the code in, but it keeps it a bit more isolated.

dapi commented 6 years ago

Thanks!

How and where did set :previous_release_path? It's empty for me

rkul commented 6 years ago

@dapi I added custom task after built-in step named deploy:set_previous_revision:

def register_hooks
  after "deploy:set_previous_revision", "My:set_previous_release_path"
end
namespace :My do
  desc "Set previous release path"
  task :set_previous_release_path do
    on release_roles(:all) do
      if test("[ -d #{release_path} ]")
        set(:previous_release_path, capture(:readlink, release_path))
      else
        warn "There is no previous release directory found, maybe the first deployment"
        set(:previous_release_path, release_path)
      end
        info "Previous release path: #{fetch(:previous_release_path)}"
    end
  end
end