My First Github Action(s)

2 years ago Github released a new feature called Actions - essentially an API for cause and effect on GitHub allowing you to orchestrate workflows based on events. For example, an Action can be setup to run a test script every time someone creates a pull request. Without going into details, Actions are extremely powerful and I've been seeing more and more great uses of them recently. So I decided to give them a go.

My First Action

I'm a huge fan of code linters - it helps me write consistent code, and they speed up Pull Request reviews by allowing devs to focus on the more complex changes. My first Action runs a Standard JS linter on push or pull_request events. Here's how the Action is configured (defined in a file in the repo at /.github/workflows).

name: Lint
on: [push, pull_request]
jobs:
  StandardJS:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12.x
      - run: npm install standard
      - run: npm run lint

In short, the Action is called StandardJS, it runs on ubuntu-latest, uses several existing Actions to checkout and setup-node(using version 12.x) and then runs npm install standard and npm run lint (ie standard 'src/**/*.js').

The most fulfilling part is seeing the output from the linter as a check in the Pull Request details.

My Second Action

The next Action I built was a bit different - instead of being based on a Github event it's a cron job setup to run once each morning. The Action performs a curl request to an API to kickoff a build process on Netlify.

name: Scheduled Deploy
on:
  schedule:
  - cron: '30 5 * * *'
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Trigger our build webhook on Netlify
      run: curl -s -X POST "https://api.netlify.com/build_hooks/${TOKEN}"
      env:
        TOKEN: ${{ secrets.NETLIFY_BUILD_HOOK_KEY }}

For more inspiration, there's an entire Actions Marketplace with reusable Actions to use on your projects.

Comments