Skip to content

Commit

Permalink
Feat: Implement action to run a RegEx on a file with grep
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Watkins <[email protected]>
  • Loading branch information
ModeSevenIndustrialSolutions committed Jan 21, 2025
1 parent d08813e commit 84606d0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/actions/file-grep-regex-action/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!--
[comment]: # SPDX-License-Identifier: Apache-2.0
[comment]: # SPDX-FileCopyrightText: 2024 The Linux Foundation
-->

# 📦 Action to run RegEx against a file using GREP

Returns values from a text file using the command-line tool, grep.

## file-grep-regex-action

## Usage Example

Call as a step in a larger composite action or workflow.

<!-- markdownlint-disable MD013 -->

```yaml
steps:
- uses: lfit/releng-reusable-workflows/.github/actions/file-grep-regex-action@main # v1.0.0
id: grep-file
with:
# https://regex101.com/r/axPzef/1
flags: "-oP -m1"
regex: '(?<=^\[testenv:docs\])*basepython = python\K(.*)'
filename: "docs/tox.ini"
no_fail: "true"
```
<!-- markdownlint-enable MD013 -->
## Inputs
<!-- markdownlint-disable MD013 -->
| Variable Name | Required | Default | Description |
| ------------- | -------- | ------- | ----------------------------------------------------- |
| REGEX | True | N/A | The regular expression to use |
| FILENAME | True | N/A | The text file to search with GNU grep |
| FLAGS | True | N/A | The flags passed to grep on the command line |
| NO_FAIL | False | False | Do not exit (if file not found or no string returned) |
<!-- markdownlint-enable MD013 -->
## Outputs
<!-- markdownlint-disable MD013 -->
| Variable Name | Mandatory | Description |
| ---------------- | --------- | ---------------------------------------------- |
| EXTRACTED_STRING | No | The string extracted by the regular expression |
<!-- markdownlint-enable MD013 -->
76 changes: 76 additions & 0 deletions .github/actions/file-grep-regex-action/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2024 The Linux Foundation

name: "Extract string from file using GREP"
description: "Uses grep and a regular expression to extract text from files"

inputs:
# Mandatory
FLAGS:
description: "Flags to use with grep command"
required: false
default: "-E"
REGEX:
description: "Regular expression used to extract string"
required: true
FILENAME:
description: "File containing text to extract"
required: true
# Optional
NO_FAIL:
description: "Override errors if file not found or no value returned"
required: false
default: "false"

outputs:
EXTRACTED_STRING:
description: "The string extracted by the regular expression"
value: ${{ steps.grep.outputs.extracted_string }}

runs:
using: "composite"
steps:
- name: "Extract text/string from file"
id: grep
shell: bash
run: |
# Extract text/string from file
set -o pipefail
set -o noglob
FLAGS="${{ inputs.FLAGS }}"
REGEX='${{ inputs.REGEX }}'
FILENAME="${{ inputs.FILENAME }}"
set +o noglob
if [ ! -f "$FILENAME" ]; then
echo "Error: file not found [$FILENAME]"
if [ "${{ inputs.NO_FAIL }}" = "true" ]; then
exit 0
else
exit 1
fi
fi
set -o noglob
echo "Flags to grep command: $FLAGS"
echo "Regular expression to use: $REGEX"
echo "File to search: $FILENAME"
# Extract the string using grep and regex
STRING=$(grep $FLAGS '${{ inputs.REGEX }}' $FILENAME)
set +o noglob
if [ -z "$STRING" ]; then
echo "String extraction failed; no matching value found ❌"
if [ "${{ inputs.NO_FAIL }}" = "true" ]; then
exit 0
else
exit 1
fi
else
# Make string available as action output
echo "extracted_string=$STRING" >> "$GITHUB_OUTPUT"
echo "Extracted string: $STRING ✅"
fi

0 comments on commit 84606d0

Please sign in to comment.