Github issues to Slack with Fabric

At selltag we are using Fabric for deploying, we started using Capifony, but it gave us some problems (not returning prompt when necessary input among other things), so we switched to Fabric and now It's working flawlessly.

We use Github for development. During iterations when issues are reviewed and are ready to be deployed, we add the tag deploy to them, so we know what's gonna be deployed for that iteration. We need to have good issue descriptions so non-technical mates could easily understand the new goodies we are deploying to production.

With Fabric and Python is so easy to integrate several of the services we use every day, saving time in the process. Every time we upload something to our environments, we publish in Slack so the team knows we are updating the server. When we push to production we also publish in our global chat room with the changes deployed. A very easy way of doing that is using pyslack and libsaas.

You could install them using pip:

pip install pyslack
pip install libsaas

We get github issues with label=deploy and status=open:

from libsaas.services import github

# ...
GH_TOKEN = ''
GH_USER = 'selltag'
REPO = 'web'


def get_issues():
    gh_client = github.Github(GH_TOKEN)
    issues_gh = gh_client.repo(GH_USER, REPO).issues().get(
        labels='deploy',
        state='open'
    )
    issues = [issue['title'] for issue in issues_gh]

    return issues

We use slack to publish a message, using the username selltagbot:

import slack
import slack.chat

# ...

def publish(msg, channel='web'):
    slack.api_token = SLACK_TOKEN
    slack.chat.post_message(
        ''.join(['#', channel],
        msg,
        username='selltagbot'
    )

Then when we execute deploy we could have something like this



# ...

def prod():
    env.label = 'PROD'
    env.host = '...'
    env.root = '...'
    # ...

def dev():
    env.label = 'DEV'
    env.host = '...'
    env.root = '...'
    # ...

def deploy(install=False):
"""Run the actual deployment steps: $ fab dev deploy"""
    with cd(env.root):
        publish('Deploying to %s in %s' % (env.label, env.host))

        # ...
        # Only publish message with issues if production
        if env.label is 'PROD':
            publish('\n'.join(get_issues()))

When we do:


fab prod deploy

We publish a message in the general slack channel with the issues deployed. :-)

To know more about how Fabric works I recommend checking the Fabric website.

Javier Aguirre

Read more posts by this author.

comments powered by Disqus