capistrano-slackify, 通过传入webhooks集成将部署通知发布到 Slack

分享于 

6分钟阅读

GitHub

  繁體 雙語
Publish deployment notifications to Slack via the incoming webhooks integration
  • 源代码名称:capistrano-slackify
  • 源代码网址:http://www.github.com/onthebeach/capistrano-slackify
  • capistrano-slackify源代码文档
  • capistrano-slackify源代码下载
  • Git URL:
    git://www.github.com/onthebeach/capistrano-slackify.git
    Git Clone代码到本地:
    git clone http://www.github.com/onthebeach/capistrano-slackify
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/onthebeach/capistrano-slackify
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    Capistrano Slackify Build StatusCode ClimateGem Version

    将部署通知发布到 Slack for Capistrano。

    安装

    将此行添加到你的应用程序的Gemfile中:

    
    gem 'capistrano-slackify', require: false
    
    
    
    

    然后执行:

     
    $ bundle
    
    
    
     

    或者将它的自己安装为:

    
    $ gem install capistrano-slackify
    
    
    
    

    用法

    在你的Capfile 中需要 gem:

    
    require 'capistrano/slackify'
    
    
    
    

    然后在 config/deploy.rb 中设置所需的变量:

    
    set :slack_url, 'https://hooks.slack.com/services/your/webhook/url'
    
    
    
    

    确保已经启用了传入的webhooks集成插件- 复制/粘贴安装指南中提供的url。

    任务将在部署时自动运行。 或者,你也可以使用以下方法手动通知部署:

    
    bundle exec cap production slack:notify_started
    
    
    
    

    或者通知完成的部署:

    
    bundle exec cap production slack:notify_finished
    
    
    
    

    默认情况下,这将沿以下各行发布某些内容:

    
    Revision 64a3c1de of my_app deployed to production by seenmyfate in 333 seconds.
    
    
    
    

    如果部署失败,则默认情况下将发布以下消息:

    
    production deploy of my_app with revision/branch 64a3c1de failed
    
    
    
    

    其他任务一样,也可以手动通知故障:

    
    bundle exec cap production slack:notify_failed
    
    
    
    

    定制

    任何默认值都可以在 config/deploy.rb 中通过骑乘:

    
    set :slack_channel, '#devops'
    
    
    set :slack_username, 'Deploybot'
    
    
    set :slack_emoji, ':trollface:'
    
    
    set :slack_user, ENV['GIT_AUTHOR_NAME']
    
    
    set :slack_fields, ['status', 'stage', 'branch', 'revision', 'hosts']
    
    
    set :slack_mrkdwn_in, ['pretext', 'text', 'fields']
    
    
    set :slack_hosts, -> { release_roles(:all).map(&:hostname).join("n") }
    
    
    set :slack_text, -> {
    
    
     elapsed = Integer(fetch(:time_finished) - fetch(:time_started))
    
    
    "Revision #{fetch(:current_revision, fetch(:branch))} of" 
    
    
    "#{fetch(:application)} deployed to #{fetch(:stage)} by #{fetch(:slack_user)}" 
    
    
    "in #{elapsed} seconds."
    
    
    }
    
    
    set :slack_deploy_starting_text, -> {
    
    
    "#{fetch(:stage)} deploy starting with revision/branch #{fetch(:current_revision, fetch(:branch))} for #{fetch(:application)}"
    
    
    }
    
    
    set :slack_deploy_failed_text, -> {
    
    
    "#{fetch(:stage)} deploy of #{fetch(:application)} with revision/branch #{fetch(:current_revision, fetch(:branch))} failed"
    
    
    }
    
    
    set :slack_deploy_finished_color, 'good'
    
    
    set :slack_deploy_failed_color, 'danger'
    
    
    set :slack_notify_events, [:started, :finished, :failed]
    
    
    
    

    可以通过定义如何以 Slack 方式显示映射来设置自定义字段:

    
    set :slack_fields, ['status', 'environment', 'docker_image']
    
    
    # You could also add your custom mappings to the defaults with:
    
    
    # set :slack_fields, fetch(:slack_fields).push('environment', 'docker_image')
    
    
    set :slack_custom_field_mapping, -> {
    
    
     {
    
    
     'environment' => {
    
    
     title: 'Environment',
    
    
     value: -> {
    
    
     if fetch(:stage) == :production
    
    
     'production'
    
    
     else
    
    
    "staging-#{fetch(:staging_name)}"
    
    
     end
    
    
     },
    
    
     short: true,
    
    
     },
    
    
     'docker_image' => {
    
    
     title: 'Docker image',
    
    
     value: fetch(:docker_image),
    
    
     short: false,
    
    
     },
    
    
     }
    
    
    }
    
    
    
    

    要配置slack解析消息( 请参见'解析模式'在 https://api.slack.com/docs/formatting ) use使用 :slack_parse 设置:

    
    set :slack_parse, 'none' # available options: 'default', 'none', 'full'
    
    
    
    

    版权

    版权所有( c ) 2014 OnTheBeach有限公司。 有关详细信息,请参阅 LICENSE.txt。


    WEB  INT  DEP  整合  notification  部署  
    相关文章