gha_config

This gem will process a templated file and output a GitHub Action workflow file specific to Flipp workflows. The idea is that you can maintain a small, lean config file without a lot of repetition and copy/pasting, and process it to export a "real" workflow file for GitHub to use.

Installation

You will need Ruby 2.3 or greater to run this (you can install this via Homebrew if you don't have it). Make sure you have the latest version of Rubygems as well.

Install using gem install gha_config.

Usage

Create a file called .github/workflow-src/CI.yml in the base directory of your app. You can think of this file as a regular GitHub Action workflow file, except for two differences:

  1. Certain "global / always needed" steps and settings do not need to be added, as they will be auto-added after processing.
  2. The file supports special template keys which can be replaced later on.

Run the command with gha_config - it will output the file into .github/workflows/CI.yml.

Template keys

Template keys are very similar to YAML anchors. Unfortunately GitHub does not support anchors, and in addition anchors have a weakness in that you cannot use them to extend arrays/lists.

Template keys all begin and end with underscores: _. You define template keys in a special _defaults_ section in your config, and you can use them elsewhere.

Here's an example of a templated GitHub Action workflow file.

on:
  pull_request:
  push:
    branches:
    - master
    - develop

defaults_:
  _container_:
    image: ghcr.io/wishabi/ci-build-environment:ruby-3.0-buster-node
    credentials:
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GHCR_TOKEN }}
  _cache_:
    - name: Bundle cache
      uses: actions/cache@v2
      with:
        path: vendor/bundle
        key: rails-${{ hashFiles('Gemfile.lock') }}
        restore-keys: rails-
  _teardown_:
    - name: Cleanup
    - run: docker cleanup
  _setup_:
    - _cache_
    - name: Bundle install
      run: bundle install --jobs=4

jobs:
  build:
    container: _container_
    steps:
      - _setup_
      - name: Print build
        run: ls dist/
      - _teardown_

The output of this file will look like this (you can see that it auto-creates the checkout and "Flipp global" steps):

name: CI

on:
  pull_request:
  push:
    branches:
    - master
    - develop

env:
  HOME: /home/circleci
  AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
  AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
  AWS_REGION: ${{ secrets.AWS_REGION }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  DEPLOYMENT_TYPE: ${{ secrets.DEPLOYMENT_TYPE }}
  ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
  ECS_CLUSTER_PROD: ${{ secrets.ECS_CLUSTER_PROD }}
  ECS_CLUSTER_STG: ${{ secrets.ECS_CLUSTER_STG }}
  ECS_SERVICE_PROD: ${{ secrets.ECS_SERVICE_PROD }}
  ECS_SERVICE_STG: ${{ secrets.ECS_SERVICE_STG }}
  SERVICE_NAME: ${{ secrets.SERVICE_NAME }}
  SERVICE_PROFILE: ${{ secrets.SERVICE_PROFILE }}
  SERVICE_TOKEN: ${{ secrets.SERVICE_TOKEN }}
  WISHABI_ENVIRONMENT: ${{ secrets.WISHABI_ENVIRONMENT }}

jobs:

  build:
    runs-on: [ubuntu, runner-fleet]
    container:
      image: ghcr.io/wishabi/ci-build-environment:ruby-3.0-buster-node
      credentials:
        username: "${{ github.repository_owner }}"
        password: "${{ secrets.GHCR_TOKEN }}"
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Flipp global
      uses: wishabi/[email protected]
      env:
        SLACK_BOT_TOKEN: "${{ secrets.SLACK_BOT_TOKEN }}"
      timeout-minutes: 10
      with:
        slack_channel: "${{env.SLACK_CHANNEL }}"
        job_status: "${{ job.status }}"
    - name: Bundle cache
      uses: actions/cache@v2
      with:
        path: vendor/bundle
        key: rails-${{ hashFiles('Gemfile.lock') }}
        restore-keys: rails-
    - name: Bundle install
      run: bundle install --jobs=4
    - name: Print build
      run: ls dist/
    - name: Cleanup
      run: docker cleanup

Templates get expanded into their contents whenever they are used. Templates can also include templates (as you can see that the _setup_ template includes the _cache_ template). Finally, if a template is used inside a list of steps, the expansion will continue the list rather than nest it.

If you're using Ruby, you can add this gem to your app Gemfile. Otherwise, you can install it locally. Either way, you can re-run it whenever your "source workflow" changes.

Contributing

Pull requests are welcome!