Recently I have been spending more time with GitHub Actions. If you don’t know already, GitHub actions allow you to orchestrate workflow (CI/CD) based on events like push, pull-request etc. Not only you can use actions available in the GitHub Marketplace, you also build your own. I wanted to experiment, how easy it is to develop one. So I just did that and built an custom action.

Introduction to action

Use the action from GitHub Marketplace

GitHub Action

The purpose of the action is to patch the content of file (currently only JSON files are supported, but I plan to extend it to support other file types soon) given the patch syntax. This patch operation is different from usual token replacement in the file as this action does not expect you to have tokens (e.g: __TOKEN__) in the file, thus you can keep unaltered in the version control.

At the moment, only JSON files are supported. Plan is to support other file types in the later versions.

Usage

See line #15

name: "test action"
on:
  pull_request:
  push:
    branches:
      - master
      - 'feature/*'

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Patch files
      uses: onlyutkarsh/[email protected]
      env:
        name: utkarsh
      with:
        files: |
          testfiles/**/*.json
        patch-syntax: |
          = /version => "1.0.1"
          + /author => "${{ env.name }}"
          = /bugs/url => "https://www.google.com"
          + /buildId => "${{ github.run_number }}"

Inputs

The action has 2 mandatory inputs and couple of optional parameters.

  • files

    Glob expression. See example action YAML below. Required.

  • patch-syntax

    Use the operation syntax => value syntax to patch the JSON file. Required.

    Supported Operations:

    • + add. Example: + /version => "1.0.0"
    • - remove. Example: - /version Note: No value is passed.
    • = replace. Example: -/version => "1.0.1"

    Example:

    Input JSON:

      {
          "version": "1.0.0",
          "keywords": [],
          "author": "onlyutkarsh",
          "bugs": {
              "url": "http://www.dummy.com"
          }
      }
    

    Patch Syntax:

      patch-syntax: |
            = /version => "1.0.1"
            + /author => "John Smith"
            = /bugs/url => "https://www.mydomain.com"
    

    Output JSON:

      {
          "version": "1.0.1",
          "keywords": [],
          "author": "John Smith",
          "bugs": {
              "url": "https://www.mydomain.com"
          }
      }
    
  • output-patched-file

    If true, the patched content is printed in the logs. Optional. Default is true.

  • fail-if-no-files-patched

    If true, fails the build, if no files are patched. Optional. Default is false.

  • fail-if-error

    If true, failes the build when an error occurrs. Optional. Default is false

Opensource

As ever, the action is open source on GitHub, please feel free check it out and if you have ideas contribute. If you have any issues using the action, please raise it as an issue.

Acknowledgement

The action is inspired from great File Patch Build and Release Tasks extension for Azure Pipelines.


About author
Utkarsh Shigihalli
Utkarsh Shigihalli
Utkarsh is passionate about software development and has experience in the areas of Azure, Azure DevOps, C# and TypeScript. Over the years he has worked as an architect, independent consultant and manager in many countries including India, United States, Netherlands and United Kingdom. He is a Microsoft MVP and has developed numerous extensions for Visual Studio, Visual Studio Code and Azure DevOps.
We Are
  • onlyutkarsh
    Utkarsh Shigihalli
    Microsoft MVP, Technologist & DevOps Coach


  • arora_tarun
    Tarun Arora
    Microsoft MVP, Author & DevOps Coach at Avanade

Do you like our posts? Subscribe to our newsletter!
Our Book