Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send commit log between 2 deployments. #50

Open
prigal opened this issue May 18, 2016 · 9 comments
Open

Send commit log between 2 deployments. #50

prigal opened this issue May 18, 2016 · 9 comments

Comments

@prigal
Copy link

prigal commented May 18, 2016

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
Copy link
Owner

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.

@phallstrom phallstrom self-assigned this May 26, 2016
@prigal
Copy link
Author

prigal commented May 31, 2016

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
Copy link
Owner

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

@lcguida
Copy link

lcguida commented Aug 4, 2016

Very basic log would look like:

git log --oneline <new revision hash>..<old revision hash>

@phallstrom phallstrom assigned prigal and unassigned phallstrom Apr 8, 2017
@rkul
Copy link

rkul commented Apr 20, 2017

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
Copy link

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
Copy link
Owner

@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
Copy link

dapi commented Mar 24, 2018

Thanks!

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

@rkul
Copy link

rkul commented Mar 24, 2018

@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

@prigal prigal removed their assignment Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants