Patch files using GitHub Actions

Apr 6, 2020 | Utkarsh Shigihalli Utkarsh Shigihalli | 2 min read

github


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
name: "test action"
on:
  pull_request:
  push:
    branches:
      - master
      - 'feature/*'

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/[email protected]

    - 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:

    1
    2
    3
    4
    5
    6
    7
    8
    
      {
          "version": "1.0.0",
          "keywords": [],
          "author": "onlyutkarsh",
          "bugs": {
              "url": "http://www.dummy.com"
          }
      }
    

    Patch Syntax:

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

    Output JSON:

    1
    2
    3
    4
    5
    6
    7
    8
    
      {
          "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.


Related Posts
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 and Azure DevOps.
Comments