Automatically Paying Code Contributors with XRP - DEV: Github Actions Hackathon
Tags: showdev, actionshackathon, python, github
Published: 2020-09-18 20:47:58

[A 1080p version of this video is available on Cinnamon](https://cinnamon.video/watch?v=410918564657104239)

This is the recording of a live coding session I did on the [IBM Developer Twitch channel](https://twitch.tv/ibmdeveloper). I was joined by my colleague [Si Metson](https://twitter.com/drsm79). 

We decided to revive [an idea we had for a previous hackathon, to be able to trigger payments to developers when they commit code to Github](https://dev.to/hammertoe/payid-hackathon-live-coding-monetizing-github-commits-585f). We never finished that code or submitted it to the PayID hackathon in the end. So we decided to reformulate it from being a serverless function on [IBM Cloud Functions](https://ibm.biz/BdqQfK) and fired by a Github webhook, to being a Github Action.

This fitted in nicely with the [Github Actions Hackathon](https://dev.to/devteam/announcing-the-github-actions-hackathon-on-dev-3ljn) that was ending soon. 

In the coding session Si and I looked at what Github Actions are, and how they are defined. In short, there is a specific directory in a github repo `/.github/workflows` in which you can put a YAML file defining a workflow to be called.

The workflow outlines an environment on which it will run and defines a number of steps to be run. Each step uses an existing action. So for example the initial workflow we had was this:

```yaml
name: PayID Action

on:
  # Trigger the workflow on push or pull request,
  # but only for the master branch
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    # Check out the code
    - uses: actions/checkout@v2

    # Set up a python environment to run code in  
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'

    # Install our python dependancies
    - name: Install dependencies
      run: |
        pip install -r requirements.txt
    
    # Get the latest commit message
    - name: get commit message
      run: |
        echo ::set-env name=commitmsg::$(git log --format=%B -n 1 ${{ github.event.after }})

    # Debugging: show the commit message
    - name: show commit message
      run : echo $commitmsg

    # Run our python code and set an environment variable
    # with contents of a secret from the Github secret vault
    # for this repo
    - name: Run PayID
      env:
        PAYID_WALLET_SECRET: ${{ secrets.PAYID_WALLET_SECRET }}
      run: |
        python pay_contributor.py
```

One of the key features we liked about creating this functionality as a Github Action is that we can store and access secrets in Github. In this case we need to securely store the secret key for our XRP wallet in order to be able to sign a transaction authorising a payment to be made.

The code for `pay_contributor.py` can be found in the repository:

{% github hammertoe/payid_xrp_action %}

Beyond what we achieved in the video, I went on to convert the workflow to instead of calling the python directly, to refer to the python code as an action itself. The action is then defined in an `action.yaml` file:

```yaml
name: 'Automatically pay Contributors in XRP via PayId'
description: 'Scan commit messages for PayId and make payment to that PayId in XRP on push'
author: 'Matt Hamilton'
branding:
  icon: dollar-sign
  color: green
inputs:
  amount:
    description: 'Amount of XRP in drops to pay each PayId found'
    default: 1000000
  commit_log:
    description: 'The commit message(s) to scan for PayIds'
    required: true
  wallet_secret:
    descrption: 'The secret key of the XRP wallet to pay from'
    required: true
  max_payout: 
    description: 'Maximum number of drops to pay out'
    default: 10000000
  environment:
    description: 'Environment to use, TESTNET or LIVENET'
    default: 'TESTNET'
  server:
    description: 'XRP Ledger server to use'
    default: 'test.xrp.xpring.io:50051'
runs:
  using: 'docker'
  image: 'Dockerfile'
```

This defines the inputs that the action will take, and how it should be run. Github actions supports just two runtimes: javascript and docker. With docker of course you can create whatever runtime you want. So as you see above, on the last line, we refer to a Dockerfile.

In our case the Dockerfile looks like:

```dockerfile
FROM python:3.7-slim

WORKDIR /app

ADD . /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc libgmp3-dev python3-dev \
    && rm -rf /var/lib/apt/lists/* \
    && pip install --trusted-host pypi.python.org -r requirements.txt \
    && apt-get purge -y --auto-remove gcc libgmp3-dev python3-dev
    
ENTRYPOINT ["python"]

CMD ["/app/pay_contributor.py"]
```

As we need to install some python dependencies (the `xpring` library) that need some libraries for cryptographic routines, we need to install a number of system packages and compiler: `gcc libgmp3-dev python3-dev`.

Putting this all together, we can then publish this action on the Github Actions marketplace so it is available for anyone to use:

![Screenshot of entry in Github Actions marketplace](https://dev-to-uploads.s3.amazonaws.com/i/tdvxp6fj1tzxn8n01ydb.png)

https://github.com/marketplace/actions/automatically-pay-contributors-in-xrp-via-payid

## Hackathon Entries

The actual submissions for the Github actions hackathon are:

For the category *Maintainer Must-Haves*:
{% post hammertoe/automatically-pay-contributors-in-xrp-via-payid-4d3e %}

For the category *Wacky Wildcards*:
{% post hammertoe/lord-of-the-pythons-25f7 %}

I hope you enjoyed the video, I stream on Twitch weekly and you can usually find out when on the [IBMDeveloper Twitch stream schedule](https://www.twitch.tv/ibmdeveloper/schedule) or follow me on Twitter: [@hammertoe](https://twitter.com/hammertoe).