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:
name:PayID Actionon:# Trigger the workflow on push or pull request,# but only for the master branchpush:branches:-masterjobs:deploy:runs-on:ubuntu-lateststeps:# Check out the code-uses:actions/checkout@v2# Set up a python environment to run code in -name:Set up Pythonuses:actions/setup-python@v2with:python-version:'3.x'# Install our python dependancies-name:Install dependenciesrun:|pip install -r requirements.txt# Get the latest commit message-name:get commit messagerun:|echo ::set-env name=commitmsg::$(git log --format=%B -n 1 ${{ github.event.after }})# Debugging: show the commit message-name:show commit messagerun :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 PayIDenv: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:
A Github Action that pays a contributor in XRP for every commit
payid_xrp_action
What?
A Github Action that pays a contributor in XRP for every commit
This means you can define an amount to be paid every time someone pushes
commits to you repository.
The address to send the payment to is looked up via PayIds
in the commit messages.
How to set it up?
An example workflow:
name: Pay contributorson:
# Trigger the workflow on push or pull request,# but only for the master branchpush:
branches:
- masterjobs:
pay:
runs-on: ubuntu-lateststeps:
- name: Checkout codeuses: actions/checkout@v2
- name: get commit messagerun: | echo ::set-env name=commit_log::$(git log --format=%B ${{ github.event.before }}..${{ github.event.after }})
- name: Run PayIDuses: hammertoe/payid_xrp_action@masterwith:
commit_log: ${{ env.commit_log }}wallet_secret: ${{ secrets.PAYID_WALLET_SECRET }}amount: 1000000
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:
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.
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: